CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2015
    Posts
    1

    need help finishing IsaPrimeDivisor function

    I am attempting to create a program that reads numbers and either classifies them as a prime, or prints their prime divisors. I have most of the program done, however, I have been stuck on the final 2 parts for a long time now and my professor doesnt have office hours today, so I cant ask him.

    Code:
    //-----------------------------------------------------------------
    // DO: 1. Fill in your name and section
    // Name:   
    // 
    // Course:  
    //
    // Purpose: This program reads numbers and either classifies them as
    //          prime or prints their prime divisors.  The program quits 
    //          when the user enters a number less than the First Prime, 
    //          which is 2.
    //
    // Input:   An integer
    //
    // Output:  For each input number
    //              Display a message if the input number is a prime.
    //              Print all prime divisors if the input number is not
    //              prime.
    //
    //---------------------------------------------------------------------
    #include <string>
    #include <iostream>
    #include <cmath>	// cmath is needed for the sqrt() function
    using namespace std;
    
    const int FIRST_PRIME = 2;
    
    // DO: 2. Complete the prototype for IsPrime.
    //        HINT: See function definition below.
    bool IsPrime( int num );
    
    bool IsAPrimeDivisor( int candidate, int number );
    
    // DO: 3. Complete the prototoype for PrintPrimeDivisors.
    //        HINT: See the function definition below and 
    //              the call to the function in main().
    void PrintPrimeDivisors( int number );
    
    int main()
    {
       int number;
    
       cout << "Enter a number: ";
       cin  >> number;
       cout << endl;
       
       while( number >= FIRST_PRIME )
       {
          // DO: 4. Complete the NEXT line by adding a call to
          //        the IsPrime function.
          //        HINT:  Look at the function definition for
          //               the parameter. Also look at the return
          //               for IsPrime() for use in the If 
          //               conditional.    
          if( IsPrime(number)                )
             cout << number << " is prime." << endl;
          else 
          {
             cout << "Prime divisors of " << number << ":" << endl;
             PrintPrimeDivisors( number );
          }
    
          cout << "\nEnter a number: ";
          cin  >> number;
    	  cout << endl;
       }  // End of while loop
    
       cout << "\nCompleted processing!";
    
       return 0;
    } // End of main()
    
    
    // IsPrime: returns true if given number is prime, false otherwise.
    //          Note: a number is prime if no value between 2 and
    //                the square root of the number is a factor.
    // Parameter: (in)
    bool IsPrime( int num )
    {
       int limit;
    
       // DO: 5. Complete the next line by calling the sqrt() function 
       //		 from <cmath> to find the largest number that needs to
       //		 be examined.  
       limit = int (sqrt(num));
    
       int count = FIRST_PRIME;
       while( count <= limit )
       {
          if( num % count == 0 )
             return false;  // num is not prime!
          count++;
       }
       return true;			// num is prime!
    } // end of IsPrime()
    
    
    // DO: 6. Complete the following function definition 
    //        so it matches its specification.
    //
    // IsAPrimeDivisor: returns IF candidate divides number evenly
    //                             AND candidate is prime, then return true;
    //                          OTHERWISE the candidate is not prime 
    //                             then the function returns false.
    // Parameters: (in, in)
    bool IsAPrimeDivisor(    int candidate        ,   int number          )
    {
    	if ( candidate % 2 == 0 )                                           
       return (  true);
    	else
    		return ( false );
    
    } // end of IsAPrimeDivisor()
    
    
    // DO: 7. Complete the following function definition 
    //        using the following pseudocode:
    // 
    //        for each value between FIRST_PRIME and half of the
    //              <target>, which we refer to as the <limit>, (inclusive)
    //
    //           if the value Is A Prime Divisor of the target
    //              print the value followed by a space
    //        print an endl (end of line marker)
    //
    // Note that this is a count-controlled loop problem.
    // Be sure you call IsAPrimeDivisor() in the if statement.
    //
    // PrintPrimeDivisors: find and print the prime divisors of target
    // Parameter: (in)
    void PrintPrimeDivisors(int target)
    {
       const int limit = target / 2;
    
       int primeCandidate  = FIRST_PRIME;
       
       while( primeCandidate  <= limit )
       {
          // DO: 7A. Complete the if condition as described above 	
          if( IsAPrimeDivisor()            )
          {
             // DO: 7B. Complete the cout as described above.
             cout <<         << " ";
          }
          // DO: 7C increment the loop counter.
          
    
       } // end of for loop
    
       cout << endl;
    
    } // end of PrintPimeDivisors() function
    I am particularly confused about the below code. I have read the textbook chapters on the subject and played around with various code. I have next to no idea how to solve this last part and looking at various examples online have provided little to no help to me.
    Code:
    // DO: 6. Complete the following function definition 
    //        so it matches its specification.
    //
    // IsAPrimeDivisor: returns IF candidate divides number evenly
    //                             AND candidate is prime, then return true;
    //                          OTHERWISE the candidate is not prime 
    //                             then the function returns false.
    // Parameters: (in, in)
    bool IsAPrimeDivisor(       ,       )
    {
    	
    
    } // end of IsAPrimeDivisor()
    
    
    // DO: 7. Complete the following function definition 
    //        using the following pseudocode:
    // 
    //        for each value between FIRST_PRIME and half of the
    //              <target>, which we refer to as the <limit>, (inclusive)
    //
    //           if the value Is A Prime Divisor of the target
    //              print the value followed by a space
    //        print an endl (end of line marker)
    //
    // Note that this is a count-controlled loop problem.
    // Be sure you call IsAPrimeDivisor() in the if statement.
    //
    // PrintPrimeDivisors: find and print the prime divisors of target
    // Parameter: (in)
    void PrintPrimeDivisors(int target)
    {
       const int limit = target / 2;
    
       int primeCandidate  = FIRST_PRIME;
       
       while( primeCandidate  <= limit )
       {
          // DO: 7A. Complete the if condition as described above 	
          if( IsAPrimeDivisor()            )
          {
             // DO: 7B. Complete the cout as described above.
             cout <<         << " ";
          }
          // DO: 7C increment the loop counter.
          
    
       } // end of for loop
    
       cout << endl;
    
    } // end of PrintPimeDivisors() function
    Any help or examples anyone could point me to would be greatly appreciated. thank you.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: need help finishing IsaPrimeDivisor function

    Code:
    candidate % 2 == 0
    HINT: How does you code (which does not even reference "number") determine "IF candidate divides number evenly"....

    EXTRA HINT: "Evenly" does not have anything to do with "even" or "odd"....

    So close.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured