Thread: why isn't my loop working properly?

  1. #1
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55

    why isn't my loop working properly?

    im trying to make a simple program that generates 10 random prime numbers and displays them onto the screen. heres the code...

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    
    using namespace std;
    
    bool isprime(int num);
    
    int main()
    {
        int randP = 0,i = 22;
        srand( (unsigned)time( NULL ) );
        for (int x = 0; x<10; x++)
        {
            randP = rand() % i; // generate a random number between 0 and the variable 'i'
            if (isprime(randP) == true) // if the random number is a prime then display it
            cout << randP << endl;            
        } 
        cout << endl;
        system("Pause");
        return 0;
    }
           
    bool isprime(int num)
    {
         bool ya = false;
         if (num % 2 == 0)
         ya = true;
         else
         ya = false;
         return ya;
    }
    im not asking 'how do i generate 10 random prime numbers', i just simply want to know why my loop doesn't repeat 10 times like i want it to! to get an idea of what im talking about you have to try the code out for yourself. please help.

  2. #2
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    You need to decrement x whenever isprime == false;

  3. #3
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    wow thanks! that worked perfectly! could you explain why i needed to decrement x whenever isprime == false?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should have been incrementing x only when the random number you got was actually prime. Since the for loop was incrementing x automatically whether or not you had a prime, decrementing x when the number wasn't prime counteracted that so that x would stay the same until the next prime number was found.

    Another way to do it would have been to use a while loop and only increment x if the random number is prime.

  5. #5
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Code:
    bool isprime(int num)
    {
         bool ya = false;
         if (num % 2 == 0)
         ya = true;
         else
         ya = false;
         return ya;
    }
    Your code is overly verbose. It's also misleading because you're not testing for primality, but whether the number is even or not. That's only a first step for a primality test, and you can make it shorter like this:
    Code:
    bool is_even(int num) {
      return num % 2 == 0;
    }
    Testing for a prime number is harder. Especially if you make it fast.
    Just because I don't care doesn't mean I don't understand.

  6. #6
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    Quote Originally Posted by Daved
    Another way to do it would have been to use a while loop and only increment x if the random number is prime.
    something like this.....???

    Code:
    int x = 0;
    while (isprime(randP) == true) && x<10)
    {
          x++;
          cout << randP << endl;
    }
    Quote Originally Posted by Narf

    Your code is overly verbose. It's also misleading because you're not testing for primality, but whether the number is even or not. That's only a first step for a primality test, and you can make it shorter like this:
    Code:
    bool is_even(int num) {
      return num % 2 == 0;
    }
    Testing for a prime number is harder. Especially if you make it fast.
    yeah i know my isprime function is pretty.....n00bish. do you know if there is a more efficient function in the C++ STL to test whether a number is a prime or not? because im pretty sure my isprime function wouldn't be able to calculate large numbers like 1.92749293503

  7. #7
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    because im pretty sure my isprime function wouldn't be able to calculate large numbers like 1.92749293503
    I'm very sure that your isprime function won't be able to calculate primes at all. Unless you're confused as to what a prime is. The primes lower than 10 are 2, 3, 5, and 7. Let's run the equivalent of your code through all positive numbers less than 10:
    Code:
    #include <iostream>
    
    using namespace std;
    
    bool isprime(int num)
    {
      return num % 2 == 0;
    }
    
    int main()
    {
      for (int x = 1; x < 10; x++) {
        if (isprime(x))
          cout << x << endl;            
      }
    
      return 0;
    }
    isprime() failed to find all but one of the primes because isprime() doesn't test for primality, it tests for divisibility by 2. A prime number is a number that is only evenly divisible by itself and 1, which rules out most of the even numbers. That makes your isprime() horribly broken if you think it finds primes. A simple test for primality is slightly more difficult than just testing divisibility by 2, but it's dreadfully slow for larger numbers:
    Code:
    bool isprime(int num)
    {
      if (num < 2) return 0;
      for (int x = num - 1; x > 1; x--) {
        if (num % x == 0) return false;
      }
    
      return true;
    }
    Just because I don't care doesn't mean I don't understand.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> something like this.....???

    Not exactly. In your new version, if isprime returns false, then the loop will be broken. You only want the loop broken when x < 10 is not true. So remove the isprime part from the control of the while loop. Instead, it should go inside the loop in an if statement like you had it before. The only change would be to also increment x if isprime returned true.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reducing rational numbers - code not working properly
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 07:42 AM
  2. switch loop not working
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 10-29-2008, 12:54 PM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. Message loop not working...
    By Hunter2 in forum Windows Programming
    Replies: 24
    Last Post: 07-03-2003, 02:17 PM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM