C++, PRogram homework, need a doublecheck

Discussion in 'Software' started by iamien, Feb 14, 2004.

  1. iamien

    iamien Cptn "Eh!"

    Hey all. Simple program for homework, find all the Primes then figure out which ones are twins <come one after another>
    its works but just wanted to see if anyone can spot any logical erros that migth crop up

    Code:
    /* Due Date:             February ??, 2004
       Software Designer:    Stephen Fritz
       Course:               420-DBF
       Deliverable:          Assignment 1, Flow logic.
       Description:          This program takes two numbers
                             as input, and calculates all
                             prime numbers in between
                             those two numbers.
      Version:               3
    */
    
    
    
    #include <iostream>
    
    using std::cout;
    
    bool CheckPrime(unsigned long);
    
    /*************************************************
    Takes the Start and End, runs threw all the numbers
    between them, finds out the primes, stores them in
    the array and then size the number of primes back to main
    ***************************************************/
    void CalcPrimes(unsigned long start, unsigned long end, unsigned long *PrimeArray, unsigned long *size) {
    
    
        long unsigned pos = 0;
        for( unsigned long x = start; x <= end; x += 2) {
    
             if(CheckPrime(x)) {
                   *PrimeArray = x;
    
                   pos++;
                   PrimeArray++;
                   }
           }
    
           *size = pos - 1;
          return;
         }
    
    /******************
    Checks numbers to
    see if they are
    prime or not
    ****************/
    
    bool CheckPrime(unsigned long Pnum) {
    
         int x  = 3;
    
         if( Pnum % 2 == 0) // if number divisible by 2, its not prime
         return false;
    
         else {
          while (x < (Pnum)) {
            if(Pnum % x == 0) // number divisible by x? if so then no prime
                    return false;
            x += 2;
            }
            }
         return true; // if i get here then its prime
    }
    
    
    /***************************************
    Function Simply checks if two
    numbers are two apart. if so,
    they are twins. Must only be given primes
    *****************************************/
    
    bool TwinPrime(unsigned long x, unsigned long y) { // x must come first
         if ( (x + 2) == y)
            return true;
         else
             return false;
    
        }
    
    
    
    
    
    
    /**********************************
    Main. Takes two digits as input.
    Finds all primes between those numbers
    Inclusivly. Then takes those primes
    and finds out which ones are Pairs
    and prints out a list
    *********************************/
    
    
    int main(int argc, char *argv[])
    {
      unsigned long start, end, temp, size;
      size =0;
      cout<<"Please input enter the range from the numbers you wish to input\n";
      cin>>start;
      cin>>end;
    
      if (start > end) { // If the numbers are put in out of order, switch them.
      temp = start;
      start = end;
      end = temp;
      }
      size = end - start;
    
      unsigned long *primes = new unsigned long[size];
    
    
      // Find those primes
      CalcPrimes(start, end, primes, &size);
    
      temp = 0;
      unsigned long TempX, TempY;
      cout<<"List of twin primes\n";
     //Printing the List of Twin Primes
    
      while ( temp != size) {
      TempX = primes[temp];
      TempY = primes[temp + 1];
      if (TwinPrime(TempX, TempY))
    
         cout<<TempX<<","<<TempY<<endl;
        temp++;
      }
    
    
      cin>>temp;
      return 0;
    }
    
    
     
  2. Ciz

    Ciz Corporal

    Dont know if this happend with you(or even matters), but it seems that unless the first number the user enters is prime you get a error. I tried a few for example:
    11, 70 (worked fine)
    10, 70 (got an error)
    2, 61 (got an error)
    1, 61 (worked fine)
    If you already knew about this oops on my part... but just incase thought I would write something...
     
  3. iamien

    iamien Cptn "Eh!"

    Thanks, i'd forgotten about this
    I figure that out to be and eror in my logic. Which was easy to fix. Thanks for input ;)

    Friend had found it other day. the problem was that i was taking start, and adding two too it to cycle threw all odd numbers, which would have worked fine, accept when start is even, so i'm only cycling threw even numbers :p
     
  4. Ciz

    Ciz Corporal

    glad to be of help...
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds