This is the code and the exercise question requires that the local variable of sqrt_of_n needs to be declared to be double data type, not integer and it must be used in the for loop of the function definition. I have been trying to convert them into double and then integer but I am at the end of my wit. I am just at a loss about data type conversion.
This is the question from the EXERCISE: Optimize the prime-number function by calculating the square root of n only once during each function call. Declare a local variable sqrt_of_n of type double. Then use it in the loop condition.
#include <iostream>
#include <cmath>
usingnamespace std;
// Function Prototype. integer argument and return an int value
int prime(int number);
int main()
{
int prime_num;
// Prompt user for input
cout << "Please enter a number and 0 to exit: " << endl;
cin >> prime_num;
while(1){
if(prime == 0) // If user enters 0, the program exits.
break;
if(prime(prime_num))
cout << prime_num << " is a prime number." << endl;
else
cout << prime_num << " is not a prime number.";
}
system("pause");
return 0;
}
int prime(int number){
// Declare a local variable sqrt_of_n to be used in the for loop calculation
double sqrt_of_n;
double loop_calculation = sqrt(number);
for(sqrt_of_n = 2.0; sqrt_of_n <= loop_calculation; sqrt_of_n++){
if(loop_calculation % sqrt_of_n == 0)
returnfalse;
}
returntrue;
}
int main()
{
int prime_num;
// Prompt user for input
do
{
cout << "Please enter a number and 0 to exit: " << endl;
cin >> prime_num;
if(prime_num == 0) // If user enters 0, the program exits.
break;
if(is_prime(prime_num))
cout << prime_num << " is a prime number.\n" << endl;
else
cout << prime_num << " is not a prime number.\n";
} while (prime_num !=0);
system("pause");
return 0;
}
But this is the another question related to the first...I have been thinking about it. But I come up with something.
This is the question: rewrite the main so that it tests all the number from 2 to 20 and prints out the results. Each on a separate line (Hints: use the for loop, with i running from 2 to 20).
What I know is that I have to have one function and two for loops. One for loop in the main function, and another one in the user-defined function. If I am not mistaken, the result should be [b]test from 2 to 20, and print out each result from each of them(the number from 2 to 20). For example:
2, the result of which is ???
3, the result of which is ???
4, the result of which is ???
5, the result of which is ???
So far, I have learned if-statement, for and while loop as well as functions. That's all. So how I can utilize all these concepts for this exercise?
int main()
{
int prime_num;
for (prime_num = 2;prime_num<=20;prime_num++)
{
if(is_prime(prime_num))
cout << prime_num << " is a prime number.\n" << endl;
else
cout << prime_num << " is not a prime number.\n";
}
system("pause");
return 0;
}
@egregory: Don't just post code, it usually doesn't help. Instead, give a hint or something so they can do it themselves. It makes them learn better and they get a sense of accomplishment.