Data conversion

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.

This is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cmath>

using namespace 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)                              
            return false;                                                              
    }
    return true;
}

The % works for integral types.
If you want to know something about type casting, read http://www.cplusplus.com/doc/tutorial/typecasting.html
But I think you should also read http://www.cplusplus.com/forum/beginner/6381/
How about this?

bool is_prime(int number);

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;
}

bool is_prime(int number)
{
double sqrt_of_n = sqrt(number);
bool prime = true;
for(int divisor = 2; divisor <= sqrt_of_n; divisor++)
{
if(number % divisor == 0)
{
prime = false;
break;
}
}
return prime;
}
Yes I have landed success of executing this code...I managed to get this code work in the end. Thanks so much!
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?
Last edited on
Just modify the main function this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}
Last edited on
@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.
Ok - sorry. Bad habit.
Topic archived. No new replies allowed.