C++/c++ help with loops
Expert: Zlatko - 3/7/2010
QuestionQUESTION: Write a C++ program asks the user for some integer number, N. The program will display the first N
prime numbers. Remember a prime number is an integer that is only divided by 1 and itself.
So for example, if the user enters 5, the program will print
1, 2, 3, 5, 7
If the user enters 9, the program will print
1, 2, 3, 5, 7, 11, 13, 17, 19.
After the list is printed, the program will ask the user to enter y (for yes) if they want to run through it
again. If the user enters a 'y' or a 'Y', the program repeats. If anything else is entered, the program
exits.
this is what i have but it is not outputting any numbers. after that i know i have to write another loop so it does it for every number and checks if its prime. can you get me starting on the right way please? i know its a hw problem but im not asking you to completely answer it. just help me with loops
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,t,p,final,count;
cout<< "Enter how many prime numbers you want:" << endl;
cin>>final;
cout<< " these are your prime numbers:"<< endl;
for(n=2;n<final;n++)
{
t=0;
for ( p=2;p<=n/2;p++)
{
t=1;
{
if (n%p==0)
return 0;
}
}
}
}
ANSWER: Hello Juan.
I would say that your outer loop is supposed to run until the desired number of primes has been printed. It is a counting loop and should start at 0. The number that the user enters, which you call final, is not the final prime number, but it is the number of primes to be printed.
It seems to me that you are trying to do everything in main. You are trying to generate possible primes, and you are trying to test if the number is prime, and on top of that, you want to format your output. Later you will want to ask the user for another input. This is too much to do and leads to confusing code, especially if you are a beginner.
You will have an easier time if you break the program up into small functions. The main program starts out pretty good, but lets change the word "final" to "count" like this:
int main()
{
int count; // the number of primes to print
cout<< "Enter how many prime numbers you want:" << endl;
cin>>count;
cout<< "these are your prime numbers:"<< endl;
}
Now, if we had one wish, we would wish for a function like this:
// function nextPrime returns the next prime number greater than number passed in
int nextPrime(int number)
{
}
If our wish were granted, we could make a simple counting loop in main and use the nextPrime function
So here is main now:
int main()
{
int count;
cout<< "Enter how many prime numbers you want:" << endl;
cin>>count;
cout<< "these are your prime numbers:"<< endl;
int prime = 0;
for(int n = 0; n < count; n++)
{
prime = nextPrime(prime);
if (n > 0) cout << ',' // make the output comma separated
cout << prime;
}
cout << endl;
}
In reality, no-one grants our wishes. We have to make a nextPrime function. But it is pretty simple if we can make another wish. Lets wish for a function that tells us if a number is a prime number. It would look like this:
// isPrime returns true if a number is prime, and false otherwise.
bool isPrime(int number)
{
}
If we had an isPrime function, then our nextPrime function is really simple to write. The nextPrime has a simple loop start starts by incrementing number and testing it with the isPrime function. The loop ends when isPrime returns true and the function returns the new number. So nextPrime is only 4 lines long.
Next we actually have to implement the isPrime function. I'll let you try that.
Do you see how we've divided the problem into smaller problems, and solved each small problem by its self instead of trying to put everything into 3 or 4 nested loops.
Give it a try and let me know how it goes.
Oh, remember, the first prime number is 2, not 1.
See
http://en.wikipedia.org/wiki/Prime_number
and
http://www.lessonplanspage.com/MathPrimeVsCompositeNumbers7HS.htm
Best regards
Zlatko
---------- FOLLOW-UP ----------
QUESTION: what do you mean by nextprime? because when i tried to compile it said it was not declared in scope. also what do you mean by isPrime. i have never heard of that before. thank you
AnswerJuan, You have to read through all of my answer. You cannot just copy my code into your editor. My answer explains that your problem needs to be broken up into smaller and easier problems. Then each of the smaller problems has to be solved by you. You need to write the nextPrime and isPrime functions according to the specifications and instructions in my previous answer.
Best regards
Zlatko