Thread: Why am I getting an infinite loop?

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    45

    Why am I getting an infinite loop?

    Its a program to print the prime factors of a positive integer.
    For e.g. the prime factors of 2520 are 2,2,2,3,3,5,7 and 1.
    Code:
    #include<stdio.h>
    int prime(int x);
    int main(void)
    {
         int a;
         printf("Enter a positive integer");
         scanf("%d",a);
         prime(a);
    }
    int prime(int x)
    {
         while(x%2==0)
         {
           printf("2");
           x=x/2;
         }
         while(x%3==0)
         {
           printf("3");
           x=x/3;
         }
         while(x%5==0)
         {
           printf("5");
           x=x/5;
         }
         while(x%7==0)
         {
           printf("7");
           x=x/7;
         }
         while(x%9==0)
         {
           printf("9");
           x=x/9;
         }
         while(x%11==0)
         {
           printf("11");
           x=x/11;
         }
         printf("x");
    }
    The logic seems to be right,
    For e.g. say if the integer is 1232 then it passes the first while loop and prints 2 four times until the value of x becomes 77 then it passes the while loop of 3 but as x%3 is not 0,it leaves the while loop of 3 and enters the while loop of 5,as x%5 is not 0,it leaves the loop and enters the while loop of 7 and prints 7 one time then the value of x becomes 11 and the control enters the while loop of 11 and 11 is printed one time then x becomes 1 and 1 is printed one time.So I should get the output as 2,2,2,2,7,11,1 but I am getting an infinite loop of 2?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    scanf("%d",&a);

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may want to start with these errors/warnings:
    main.c||In function ‘main’:|
    main.c|7|warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]|
    main.c||In function ‘prime’:|
    main.c|43|warning: control reaches end of non-void function [-Wreturn-type]|
    main.c||In function ‘main’:|
    main.c|7|warning: ‘a’ is used uninitialized in this function [-Wuninitialized]|
    ||=== Build finished: 0 errors, 3 warnings ===|
    Jim

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Thank you matticus,one silly mistake wastes so much time.
    Thank You jimblumberg,I don't know which compiler you're using but I got the desired output with matticus's correction.
    C enlightened

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sameertelkar
    I don't know which compiler you're using but I got the desired output with matticus's correction.
    You should compile at a higher warning level.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Quote Originally Posted by laserlight View Post
    You should compile at a higher warning level.
    You mean to say that we should get rid of all warnings?But does that matter as long as we get the output?
    C enlightened

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sameertelkar
    You mean to say that we should get rid of all warnings?But does that matter as long as we get the output?
    Yes. It matters because warnings indicate potential bugs. Sometimes, a warning really is just due to an overzealous or buggy compiler, in which case it does not matter, but until you have the experience to determine when that is the case, you should properly get rid of all warnings.

    In your case the first warning noted by jimblumberg indicates exactly what Matticus pointed out. The problem indicated by second warning could become apparent if you actually use the return value of the prime function. The third warning is a consequence of the first warning. Hence, if you had compiled at a higher warning level and fixed the warnings, you would have fixed your problem and would not have needed to start this thread in the first place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well you'll definitely get an infinite loop if you pass in zero, which will happen if you enter a non-integer
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    What's the purpose of the "while(x%9==0)" loop? It will never execute, as all factors of 3 have already been removed by a previous loop (and at any rate, 9 isn't prime).
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    lol i liked this program, so i modded it!!!
    added the first 120 primes to an array

    next i might just have the program make the primes to check against! ok yeah i will!

    Code:
    //Crossfire Softwarez//
    
    #include<stdio.h>
    void prime(int x);
    int main(void)
    {     
     int a;     
     printf("Enter a positive integer: ");     
     scanf("%d",&a);     
     prime(a);
     return 0;
    }
    void prime(int x)
    {     
     
     int Primes[120]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
         73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 
         179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
         283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
         419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
         547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659};
     
     for(int counter = 0; counter <=120;counter++)
      {
       while(x%Primes[counter]==0)     
        {       
         printf("%i ",Primes[counter]);       
         x=x/Primes[counter];     
        }
      }     
     
     printf("\n");
     
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is a mistake: counter <=120
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    Quote Originally Posted by laserlight View Post
    This is a mistake: counter <=120
    yeah cought that, i went 1 too many!!!

    i like this better!!!
    Code:
    //Crossfire Softwarez// 
    #include<stdio.h>
    
    int main(void)
    {
     int found;    
     int num;
        printf("Enter a positive integer: ");
        scanf("%d",&num);
     
     for(int factor = 2; factor <= 10000;factor++)
      {
       found = 0;
       for(int counter = 2; counter <= factor;counter++)
        {
         if(factor%counter==0)
          {
           found++;
          }
        }
       if (found < 2)
        {
         while(num%factor==0)         
          {            
           printf("%i ",factor);
                    num=num/factor;
                 }
        }
     
      }
     printf("\nall factors found\n");
     return 0;
    }

  13. #13
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    i think i am having more fun with other peoples codes then my own!!! :P

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm... that looks like a pretty inefficient way to go about finding prime factors. After all, a composite factor consists of prime factors that would already have been found and used to "reduce" the number through the repeated division. As such, there is no need to determine if a potential factor is prime.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In other words, if you are building up a list of primes, you need only test each new potential prime against items from the list you have built so far, up to list items not greater than the square root of the item you are testing of course.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. infinite loop
    By rishanth12 in forum C Programming
    Replies: 6
    Last Post: 09-28-2011, 06:59 PM
  3. Why is this an infinite loop!?
    By AmbliKai in forum C Programming
    Replies: 7
    Last Post: 10-31-2007, 09:44 PM
  4. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  5. almost infinite loop
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 06:01 PM