# Thread: Newbie help with expontential program

1. ## Newbie help with expontential program

Code:
```
#include <stdio.h>
2
3 int main(void)
4 {
5 float base, exp, result;
6
7
8
9 printf("Enter base\n");
10 scanf("%f",&base);
11 printf("Enter exponent\n");
12 scanf("%f",&exp);
13 base=0;
14 for (exp = -1000, exp <= 1000, exp++){
15  base = exp;
16  result = base * exp;
17 }
18 printf("Result is %f\n",result);
19 return 0;
20 }
~```
warning: value computed is not used
exp.c:14: warning: value computed is not used
exp.c:14: error: expected â;â before â)â token
exp.c:14: error: expected expression before â)â token

Thanks guys.

2. You need two semicolons to sepearate the statements [that is one semicolon per statement] in a for-loop.

--
Mats

3. My book doesn't have semi colons after this example for loop. What the heck? I will try that.

4. Originally Posted by BSmith4740
My book doesn't have semi colons after this example for loop. What the heck? I will try that.
Not after - IN the statements of the for-loop itself, e.g.:

Code:
`for (exp = -1000; exp <= 1000; exp++)`
--
Mats

5. Code:
```#include <stdio.h>
2
3    int main(void)
4    {
5   float base, exp, result;
6
7 printf("Enter base\n");
8  scanf("%f",&base);
9  printf("Enter exponent\n");
10  scanf("%f",&exp);
11
12  for (exp = 0; exp < 1000; exp++)
13  {
14
15  result = base * exp;
16  }
17  printf("Result is %f\n",result);
18  return 0;
19  }
20
~```

A base of four and exponent of four gives me
3996

What the heck?

6. Follow the programs logic; you are reading a value into exp and then initializing exp to 0 inside the loop.

Among some other things, take note that you are assigning a new value to result each time. You want to keep multiplying the base number exp - 1 times.

Good luck.

7. I thought you had to ininitalize the variable in the first part of the for statement?

8. Yes, but the fact that you initialize the float exp to 0 after reading in the input from the user.

Code:
```/* stores a float typed in by the user in exp */
scanf("&#37;f", &exp);

/* iterates a loop 1000 times, but this makes the value you typed in as the exponent completely useless. You are overwriting its value with 0. */
for (exp = 0; exp < 1000; exp++)```
What you want to do is read in exp, and use it as a bound in the loop. You are using it as the index. Use another variable as the index. You seemingly pulled the number 1000 out of thin air.

Remember what you need to do: you need to multiply the base number exp times.

i.e. 2^3 = 2 * 2 * 2 = 8

9. I don't understand what you mean by index.

What the heck is a nan?

Code:
```#include <stdio.h>
2
3    int main(void)
4    {
5   float base, exp, result, sum;
6
7 printf("Enter base\n");
8  scanf("%f",&base);
9  printf("Enter exponent\n");
10  scanf("%f",&exp);
11
12  for (exp = -100; exp < 100; exp++)
13
14  {
15  sum *= exp;
16  result = base * sum;
17  }
18  printf("Result is %f\n",result);
19  return 0;
20  }
21
~```
Enter base
2
Enter exponent
2
Result is nan

10. Maybe you should look back at some unrelated examples of for loops and such. When i say index I mean the variable you are using to increment the loop. There are a number of things wrong, but notably you are declaring a float sum. Then before giving a value to sum you are storing in it the value of sum * exp.

For example:
Code:
```float sum;  /* sum has a 'garbage' value, you don't know whats there */
...
sum *= exp; /* you are taking that garbage value and multiplying it by some number, this is surely not what yo want */```
You need to know that simply declaring a variable does not give it a value. Supposing the loop was setup correctly, you would want something like this:

Code:
```float sum = base;

...
/* inside the for loop */
sum *= base;```
Also, don't forget that x ^ 0 is 1.

11. > What the heck is a nan?
NaN is Not A Number.
Which, considering sum is uninitialised at the point you do
sum *= exp;

Other special floats you may see are INF (infinites - really big), IND (indefinites)

Oh, and can you stop numbering your posts with line numbers. We can't copy the code and try it without lots of code.

If you need to point out specific lines, then use a comment.
Eg.
Code:
`for (exp = -100; exp < 100; exp++) /* line 12 */`

12. Do yourself a favor: READ your code to yourself or post it here so you know what it does. Then write out in words what the logic should be.
Compare the two, fix the logic, translate to code again.
Part of programming is writing the correct syntax and part is to use the correct logic.
The first is now fine, the second is not fine.

13. ## I am a little confused.

Guys,
I have been working on this for two days. I think I need to say
base = exp somewhere but if I do it after the scanf it going to override the scanf will it not. If I do it before the scanf, the scanf will override the values. Needless to say I am thoroughly confused.

Code:
```#include <stdio.h>
2
3    int main(void)
4    {
5   float base, exp, product, a;
6   double result;
7
8
9
10
11 printf("Enter base\n");
12  scanf("%f",&base);
13  printf("Enter exponent\n");
14  scanf("%f",&exp);
15
16  printf("The exponent is %f\n",exp);
17  printf("The base is %f\n",base);
18  result = 0;
19 product = 1;
20  for( a = 1; a <= exp; a++)
21  {
22  product *= exp;
23  printf("The product is %f\n",product);
24
25
26  printf("The variable A is equal to %f\n",a);
27
28
29
30  result = base *  product;
31  printf("Result is %f\n",result);
32  if ( 0 == product - base)
33  return base;
34
35  }
36
37  return 0;
38  }```
The exponent is 3.000000
The base is 5.000000
The product is 3.000000
The variable A is equal to 1.000000
Result is 15.000000
The product is 9.000000
The variable A is equal to 2.000000
Result is 45.000000
The product is 27.000000
The variable A is equal to 3.000000
Result is 135.000000

14. Think for a minute about multiplying 5^3. You need 5 * 5 * 5. There are no multilplying threes by itself. There is no multiplying 5's by 3's. The only use of the three is to count how many multiplies there are. The only thing that should be multiplying is 5. No 3. Just 5.

15. Tabstop I have realized that I am missing it. C does not understand ^. It would be nice if it did.

I want to say base = exp
for statement
base*=exp

What am I missing?