My assignment is to find and output all the prime numbers between two user inputted values. Also, the program is required to contain two functions with the following guidelines:
Function 1: A function that determines whether or not a particular integer is prime. This function has a single integer parameter which is pass by value and returns a Boolean value.
Function 2: A function that writes to an output file all of the prime numbers between two specified integers. This function has three parameters, the two specified integers which are pass by value and the output file object that is pass by reference. This function does not return a value.
//Outside function that determines if the number is prime
bool IsItPrime(int isprime)
{
for(int i=number1;i<number2;i++)
{
if(number1%i==0)
{
isprime=0;
}
else
{
isprime=1;
}
}
return isprime;
}
//Outside function that outputs the prime numbers between 2 values
void ShowPrimes(int number1,int number2,ofstream fout)
{
for(x=number1;x<=number2;x++)
{
bool IsItPrime(int isprime) = 1
fout<<x<<endl;
}
}
This line: fout<<ShowPrimes(showprimes);
will only write the only result ( return <something>; ) , not all of them.
From your question:
Function 1: A function that determines whether or not a particular integer is prime. This function has a single integer parameter which is pass by value and returns a Boolean value.
But, your function was: bool IsItPrime(bool isprime);
You should send another type to your function. It's stated on the question.
Another one:
Function 2: A function that writes to an output file all of the prime numbers between two specified integers. This function has three parameters, the two specified integers which are pass by value and the output file object that is pass by reference. This function does not return a value.
- You should know what to do. It's clear that you should use 3 parameters instead of one as in your current functions.
- Also change the function type.
For your loop: for(int i=2;i<number1;i++)
It should based on the 2 numbers from user input, not from 2 to number1. Your first function is working, but it's not working based on given condition.
I recommend also to use the screen ( cout ) to print the result. It'd be much easier to debug. When you're ready, use file.
Alright, updated the code above. I think I fixed all the issues you addressed. Now I'm stuck on what to actually put into the second function to have it achieve it's purpose.