main function

Rewrite main so that it tests all the numbers from 2 to 20 and prints out the results, each on a separate line.

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
#include <iostream>
#include <cmath>

using namespace std;

bool prime(int n);

int main() {
	int i;

	while (true) { 

		cout << "Enter num (0 = exit) and press ENTER: ";
		cin >> i; 

		if (i == 0) 
			break;

		if (prime(i)) 
			cout << i << " is prime" << endl;
		else
			cout << i << " is not prime" << endl;
	}

	system("PAUSE");
	return 0;
}

bool prime(int n) {
	int i;
	double sqrt_of_n = sqrt(n);
	for (i = 2; i <= sqrt_of_n; i++) { // We can see sqrt(n) is calculated every iteration here in the check statement
		if (n % i == 0) 
		return false;
	}

	return true; 
}


Never have I rewritten main before but a for loop is a hint so I'm guessing:
 
for (i = 2; i <= sqrt_of_n; i++)
Looks like instead of requesting input from cin in a while loop, it should generate the values with a for loop.
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
#include <iostream>
#include <cmath>

using namespace std;

bool prime(int n);

int main() {
	int i;

	for (int n = 20; n <= i; n++)  {

		cout << "Enter num (0 = exit) and press ENTER: ";
		cin >> i; 

		if (i <= 2) 
		break;

		if (prime(i)) 
			cout << i << " is prime" << endl;
		else
			cout << i << " is not prime" << endl;
	}

	system("PAUSE");
	return 0;
}

bool prime(int n) {
	int i;
	double sqrt_of_n = sqrt(n);
	for (i = 2; i <= sqrt_of_n; i++) { // We can see sqrt(n) is calculated every iteration here in the check statement
		if (n % i == 0) 
		return false;
	}

	return true; 
}


Won't compile, also this isn't changing the main??
Last edited on
In this version, you should delete lines 13 to 17, they aren't needed here.


I've no idea what n is at line 11.
Since your loop is counting down from 20, you need to test for i being greater than some limiting value (in this case you want the last value to be 2).

Though I thought from the original description that the loop should count upwards, starting at 2, and end with 20.
Last edited on
Yes, it should start at 2...
2 to 20, not counting down... sorry about the confusion...

2 to 20.

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
#include <iostream>
#include <cmath>

using namespace std;

bool prime(int n);

int main() {
	int i;

	for (int i = 20; 2 <= i; i++)  {

		cin >> i; 

		if (i <= 2) 

		if (prime(i)) 
			cout << i << " is prime" << endl;
		else
			cout << i << " is not prime" << endl;
	}

	system("PAUSE");
	return 0;
}

bool prime(int n) {
	int i;
	double sqrt_of_n = sqrt(n);
	for (i = 2; i <= sqrt_of_n; i++) { // We can see sqrt(n) is calculated every iteration here in the check statement
		if (n % i == 0) 
		return false;
	}

	return true; 
}
Last edited on
The cin at line 13 is not needed.
The for loop should start with the first value, in this case 2, you have the 20 and 2 the wrong way around.

Looks like you need to think carefully about the comparison condition in the for loop. You need the loop to continue while this condition is true, and stop as soon as it become false.

Last edited on
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
#include <iostream>
#include <cmath>

using namespace std;

bool prime(int n);

int main() {
	int i;

	for (int i = 2; i <= 20; i++)  {

		if (prime(i)) 
			cout << i << " is prime" << endl;
		else
			cout << i << " is not prime" << endl;
	}

	system("PAUSE");
	return 0;
}

bool prime(int n) {
	int i;
	double sqrt_of_n = sqrt(n);
	for (i = 2; i <= sqrt_of_n; i++) { // We can see sqrt(n) is calculated every iteration here in the check statement
		if (n % i == 0) 
		return false;
	}

	return true; 
}


Not sure what to do about line 13-18..
Last edited on
I think you're missing something here. A high-level understanding of what it is your code is supposed to do, and the purpose of each line.

Let's take the original program and strip the main() down to the bare minimum.
1
2
3
4
5
6
7
8
9
int main() 
{
	int i = 7;

	if (prime(i)) 
		cout << i << " is prime" << endl;
	else
		cout << i << " is not prime" << endl;
}


At line 3 an arbitrary value of 7 is assigned to the variable i. (You could choose any positive value).

All the other lines in the original were to help get different values for i, by asking the user to type in the value.

Now we need to use a for loop to supply those different values. So all that is needed inside the loop is simply those four lines of the if-else statement.

The for loop is still not quite right, at the start i will be equal to 2.

Now, is 20 <= 2 true or false? Well, 20 isn't less than 2. And 20 is not equal to 2. So it is false.

You need to change this condition so it is true for the range 2 to 20.
Last edited on
1
2
3
4
5
6
7
8
9
10
11

int main() {
	int i;

	for (int i = 2; i <= 20; i++)  {

		if (prime(i)) 
			cout << i << " is prime" << endl;
		else
			cout << i << " is not prime" << endl;


i = 2
2 <= 20 is true.
Ok, that looks like it should work. So did you get the whole program to work properly now? I assume yes.

(by the way, line 3 int i; is not needed because at line 5 int i is declared inside the for loop.)
@chervil it did! thank you :D

mind helping me out with print_out statements?
If you have another question, then go ahead and post it. Though there are lots of others here able to help, if I'm not available.
Topic archived. No new replies allowed.