# Thread: FOR cycle problem

1. ## FOR cycle problem

Hi folks. I have written a code. I got 4 data (int) in a txt file: an operator, tho numbers from an interval and a power.

For example: + 2 5 2

I want a FOR cycle to do this:

Power all members of the interval and add them like this:

res=2^2+3^2+4^2+5^2=54

My code is:

Code:
```#include <stdio.h>

#include <math.h>

int main(void)
{

int num1, num2, kitevo, res;
char op;
int i;

FILE* fp = fopen ("be.txt", "r");

while (fscanf(fp, "%c%d%d%d\n", &op, &num1, &num2, &kitevo) == 4) {

switch (op)
{

case '+' :

for (int i = num1; i <= num2; i++);
{
res += pow(i, kitevo);
}
case '*' :

for (int i = num1; i <= num2; i++);
{
res *= pow(i, kitevo);
}
case '-' :

for (int i = num1; i <= num2; i++);
{
res = 0-(res += pow(i, kitevo));
}
break;

}

FILE*  outfile = fopen("ki.txt", "w");
fprintf(outfile, "%d\n", res);
}

return 0;

}```
It gives me wrong result, but i dont see any mistake.

2. When you get to line 24 (i.e., just before your while loop starts) what value does the variable res have? What value do you want it to have?

3. It has to be 0, i guess. Its only get value, when the cycle starts.

4. Originally Posted by gyilki
It has to be 0, i guess. Its only get value, when the cycle starts.
Why not make line 24
Code:
`printf("%d\n", res);`
and see whether you're right?

5. Originally Posted by gyilki
It has to be 0, i guess. Its only get value, when the cycle starts.
It's not "0" because the variable is uninitialized and its value is random.

6. It's not "0" because the variable is uninitialized and its value is random.
What? It could be zero. It could be any other number as well, that's what "random" means.

7. Still gives me wrong result (always 96) and yes, i had to write an own power, cuz cannot use math.h:

Code:
```#include <stdio.h>

int hatvany(int alap, int exp)
{
int res = 1;
for (;;)
{
if (exp & 1)
res *= alap;
exp >>= 1;
if (!exp)
break;
alap *= alap;
}

return res;
}

int main(void) {

char op;
int num1, num2, kitevo;
int res;
int i;

FILE* fp = fopen ("be.txt", "r");
FILE*  outfile = fopen("ki.txt", "w");
fprintf(outfile, "%d\n", res);

while (fscanf(fp, "%c%d%d%d\n", &op, &num1, &num2, &kitevo) == 4) {

switch (op) {

case '+' :

for (int i = num1; i <= num2; i++);
{
res += hatvany(i, kitevo);

}
break;
case '*' :

for (int i = num1; i <= num2; i++);
{
res *= hatvany(i, kitevo);
}

break;

}

}

return 0;
}```

8. I'm pretty sure you wrote hatvany() wrong. alap *= alap; doesn't really help much.

You want to do, for example:
res = 1;
alap = 2;
exp = 3;
res *= alap;
--exp;
res *= alap;
--exp;
res *= alap;
--exp;

By now res is 8, as you'd expect, and exp is 0. Perhaps a for loop that tests exp repeatedly is what you actually want.

Also you still need to initialize the answer to the whole calculation to 0 or 1 (depending on if you add or multiply, respectively).

9. Originally Posted by jimblumberg
What? It could be zero. It could be any other number as well, that's what "random" means.
I just want to point out that the value of an uninitialized variable is indeterminate, not "random". I'm making that distinction because it may always be zero or it may never be zero. You know this of course, I guess you were being pedantic about their use of the word.

10. Originally Posted by whiteflags
I'm pretty sure you wrote hatvany() wrong. alap *= alap; doesn't really help much.
Well, but it's inside the loop -- this is binary exponentiation. So x^9 = x^8 * x (since that's the binary representation of 9), and the repeated squaring of alap gives alap^2, alap^4, alap^8, etc., so that when the right-most bit of the power is one the correct power of alap gets multiplied in.

11. Originally Posted by gyilki
. . . i had to write an own power, cuz cannot use math.h
Why? The function exp() need "math.h". You use the name of a C function for a variable (int exp). . . that's not well.

C library function exp()

12. Originally Posted by Kernelpanic
Why?
Teachers are weird. (But yes, using a reserved name for a function parameter is a Bad Idea.)

13. Solved, i rewrote the whole code and it works! Thanks for helping me guys!

14. Little bit late, but you should check if the files are realy opened before you use it.
Code:
```…
FILE *fp = NULL, *outfile = NULL;

if ((fp = fopen("be.txt", "r")) == NULL) {
fprintf(stderr, "could not open file 'be.txt'! exit.\n");
return 1;
}
if ((outfile = fopen("ki.txt", "w")) == NULL) {
fprintf(stderr, "could not open file 'ki.txt'! exit.\n");
fclose(fp);
return 1;
}
…```
And dont forget to close it if you dont need it anymore.

Popular pages Recent additions