How to display prime #'s using function calls

hey guys, im a total noobie at this. started class maybe 1 month ago and am really confused on how to make this program that was asked during class. Thank you in advance to those who help with this.

the question that was asked is as follows:
using this program--
<
#include <iostream>
#include <string>
using namespace std;
int AskPosNumber();
bool IsItPrime(int number);
bool CheckIt(int number);
int main ()
{
int n;
string answer;
do
{
n=AskPosNumber();
bool result=IsItPrime(n);
if(result==true)
{
cout<<endl<<"the value "<< n << " is prime.";
}
else
{
cout<<endl<<" the value " << n << " is NOT prime.";
}
cout<<endl<<"do another number? y/n";
getline(cin,answer);
}while(answer==y);
return 0;
}

int AskPosNumber()
{
int num;
bool OK;

do
{
cout<<endl<<"please enter a positive interger==> ";
cin >>num;
cin.ignore();
OK=CheckIt(num);
if(OK==false)
{
cout<<endl<<"Value is not positive, please re-enter";
}
}while (OK==false);
return num;
}

bool CheckIt(int number)
{
if (number <=0)
return false;
else
return true;
}

bool IsItPrime(int number)
{
int remainder, ctr=2;
while(ctr<number)
{
remainder=number%ctr;
if(remainder==0)
{
return false;
}
ctr++
}
return true;
}

>

--make it do this:
borrow the IsItPrime function and use it to find the user-requested number of prime numbers. for this program, write a ShowPrimes function that is passed an integer. This value is the user's requested number of primes. Inside this function is a while loop which continues to call the IsItPrime, until it ahs found that number of prime numbers. The ShowPrimes function writes them neatly in the output window, showing rows of 10 primes. your main funciton should call a HowManyPrimes function and them the ShowPrimes



Again i really appreciate all those who decide to help me on this. Hope to be enlightened soon.
Hey there!
Look, I won't do your homework for you, but I can give you some pointers to help you do it. :)

Well, lets go on a logic sequence:
int HowManyPrimes();
Ok, this is a simple one, it is supposed to keep on asking the user for a positive integer until he actually inputs one. It will be just like the AskPosNumber() function.

void ShowPrimes(int primeNum)
Clearly this will be the main function of your program. It will do most of the work: getting prime numbers and priniting them out on the console on rows of 10. You will need two variables:
1
2
int numOfPrimes;
int currNum;

The while loop's body should be executed while numOfPrimes is smaller than the function argument primeNum.

I'd recommend you to also create a NUM_PER_ROW [or something like that] constant to keep track of how many numbers you want per row, in your case, 10. It makes your code cleaner and more flexible ;).

Ok, so, numOfPrimes should be incremented whenever a number is actually printed and currNum will serve as your test number to pass to the IsItPrime(int number).

Moving on, in order to format the ouput you need to have the assistance of your numOfPrimes. I assume you have already learnt about formating the output in terms of rows but anyway, when this numOfPrimes has reached 10, 20, 30, 40... it should print a new line('\n') character, right?. What do these numbers have in common? They are all multiples of 10, this means that when numOfPrimes%NUM_PER_ROW == 0
you should print a '\n' character.

I hope I could be of help,
Good luck on your work and best regards,
~Deimos

PS: Post back if you still have any questions, but please don't ask us to write the actual program.

Deimos

Thanks for the help. I wasn't expecting a full written program, but i did expect exactly what u did for me. Thanks again, Im going to test it out and see how it works out. Ill def come back if i run into any problems.
Topic archived. No new replies allowed.