I tried to make a simple program in C which gives you the Exponent of a certain number. I used a for loop to multiply a number by itself n number of times but i keep getting the same error when i assign the answer to a variable to display it. Please help.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
//Taking the input from the user
float b = get_float("Please enter the base number ");
int n = get_int("Please enter the exponent ");
//Making a for loop to get the exponent
for (int i = 0; i < n; i = i 1)
float expo = b * b ;
printf ("The answer is %f", expo);
}
Here the error i keep getting --
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow power.c -lcrypt -lcs50 -lm -o power
power.c:12:5: error: expected expression
float expo = b * b ;
^
power.c:13:33: error: use of undeclared identifier 'expo'
printf ("The answer is %f", expo);
^
2 errors generated.
make: *** [<builtin>: power] Error 1
P.S- i'm sorry if this seems like a stupid question but i just started learning.
CodePudding user response:
Looking at your loop:
for (int i = 0; i < n; i = i 1)
float expo = b * b ;
You're not using a compound statement for the body, so the body must consist of a single statement. However, you instead have a declaration for the body of the loop. This isn't allowed by the syntax.
Even if this worked, all it would do is give you the value of b2, not bn.
What you want instead is to declare expo
outside of the loop an initialize it to 1, then multiply b
by the current value of expo making that the new value.
float expo = 1;
for (int i = 0; i < n; i = i 1)
expo *= b ;
CodePudding user response:
You may not use the declaration of the variable expo as a statement of the for loop because declarations in C are not statements and the for loop expects a statement.
Moreover if such a declaration would be allowed then the variable expo will not be visible outside the for loop. That is if to rewrite this code snippet
for (int i = 0; i < n; i = i 1)
float expo = b * b ;
printf ("The answer is %f", expo);
like
for (int i = 0; i < n; i = i 1)
{
float expo = b * b ;
}
printf ("The answer is %f", expo);
then the variable expo will not be visible outside the compound statement of the for loop.
So to make the variable expo visible outside the for loop you need to declare it before the for loop like for example
float expo = 0.0f;
for (int i = 0; i < n; i = i 1)
expo = b * b ;
printf ("The answer is %f", expo);
Pay attention to that the for loop does not make a great sense because within the for loop the variable expo is assigned with the same expression
expo = b * b ;
That is actually it is not being changed within the for loop.
If you want to output the value of the variable expo in the loop n times then you can write
for (int i = 0; i < n; i = i 1)
{
float expo = b * b ;
printf ("The answer is %f\n", expo);
}
If you want to produce a power of the variable b then you need to write
float expo = 1.0f;
for (int i = 0; i < n; i = i 1)
{
expo *= b ;
}
printf ("The answer is %f\n", expo);