Thread: FOR cycle problem

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    5

    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.

    Thanks in advance!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    5
    It has to be 0, i guess. Its only get value, when the cycle starts.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by gyilki View Post
    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. #5
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by gyilki View Post
    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.

    FOR cycle problem-resx-jpg

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    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. #7
    Registered User
    Join Date
    Nov 2018
    Posts
    5
    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. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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).
    Last edited by whiteflags; 11-12-2018 at 01:45 PM.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by jimblumberg View Post
    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.
    Devoted my life to programming...

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by whiteflags View Post
    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. #11
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote 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. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Kernelpanic View Post
    Why?
    Teachers are weird. (But yes, using a reserved name for a function parameter is a Bad Idea.)

  13. #13
    Registered User
    Join Date
    Nov 2018
    Posts
    5
    Solved, i rewrote the whole code and it works! Thanks for helping me guys!

  14. #14
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    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.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with while cycle
    By Lima in forum C Programming
    Replies: 5
    Last Post: 10-29-2010, 09:57 AM
  2. Cycle
    By Gordon in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2007, 10:58 AM
  3. Cycle counters
    By cosmo1996 in forum C Programming
    Replies: 3
    Last Post: 08-14-2007, 09:33 AM
  4. How to Cycle Through Structs
    By Grantyt3 in forum C++ Programming
    Replies: 13
    Last Post: 08-27-2005, 04:29 PM
  5. Euler Cycle
    By nickwokabi in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2005, 09:54 AM

Tags for this Thread