Does this book suck? Or just me..

Hi, I've been reading C++ without fear (2nd edition) and at the beginning it's proved quite resourceful and helpful. However at later exercises it started to be a little careless. About 90 pages through everything got so hard so quick with no helps, hint or answers or anything. I mean look at these questions..

Exercise 4.2.1. 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. (Hint: A variable is local if it is declared inside the function.) Then
use this variable in the loop condition.
Exercise 4.2.2. Rewrite main so that it tests all the numbers from 2 to 20 and prints
out the results, each on a separate line. (Hint: Use a for loop, with i running from
2 to 20.)


How the hell am I supposed to know that? How exactly do I rewrite main? It doesn't even explain. Anyway here is the source

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;// Function must be declared before being used.
bool prime(int n);
int main() {
int i;
// Set up an infinite loop; break if user enters 0.
// Otherwise, evaluate n from prime-ness.
while (true) {
cout << "Enter num (0 = exit) and press ENTER: ";
cin >> i;
if (i == 0) // If user entered 0, EXIT
break;
if (prime(i)) // Call prime(i)
cout << i << " is prime" << endl;
else
cout << i << " is not prime" << endl;
}
system("PAUSE");
return 0;
}
// Prime-number function. Test divisors from
// 2 to sqrt of n. Return false if a divisor
// found; otherwise, return true.
bool prime(int n) {
int i;
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) // If i divides n evenly,
return false; // n is not prime.
}
return true; // If no divisor found, n is prime.
}



ALSO can anyone reccomend me good updated C++ books? Preferably from experience and that's very well written and popular/good.
Indenting is the most important thing when you are trying this kind of stuff:

This is the code with indenting and spacing. It is now much easier to read
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) { //This takes an input and checks the results.  Just automate the input

		cout << "Enter num (0 = exit) and press ENTER: ";
		cin >> i; //This is the input stuff you'll have to automate.

		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;

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

	return true; 
}


How do we do this stuff?
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 (i = 2; i <= 20; i++) { // Cycle through the numbers 2 to 20 and remove that input statement stuff.

		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;
	int sqrt_of_n = sqrt(n); //Now we calculate it once
	for (i = 2; i <= sqrt_of_n; i++) { //and use the result here
		if (n % i == 0) 
		return false;
	}

	return true; 
}


How are you supposed to know this stuff? Take your time. 90 pages is not much if you are reading a history book, but in programming every line needs to be analyzed. The questions in the book are puzzles for you to figure out. You will not find examples prior because you are not supposed to know the answer, you are supposed to figure out the answer. The puzzles will be presented in increasing difficulty and are designed to help you to develop this critical thinking side of your brain.

Make sure you try each example out. Don't just think about how you would do it and then skip the testing phase. If you have a question that interests you, make sure you try out some things to answer your questions. Experiment! If you do this, you'll progress as designed and you'll understand how to answer these questions.
Last edited on
Last question first: good C++ books? Maybe you should use sites such as these ones instead, which tend to be more straight to the point:
http://www.cplusplus.com/doc/
http://www.programmerprogramming.com/learn_cpp_programming.html

Exercise 4.2.1: it refers to the line
for (i = 2; i <= sqrt(n); i++)
Do you see the sqrt(n)? That thing computes the square root of n, but it will do so for as long as the for() loop runs. This means you'll waste time and power computing the same thing over and over again.
So the author suggests that you put it into a variable, like this:
1
2
3
4
5
// local variable
double sqrt_of_n = sqrt(n); // only do the computation once, then store the value

// and then use it instead of the function sqrt()
for (i = 2; i <= sqrt_of_n; i++)


Exercise 4.2.2: come on, put some effort into it. You just need to write a simple for() loop, replacing the whole while() structure from main() and keep the original if (prime(i)) structure.

Edit: Stewbond, my archenemy!
Last edited on
Topic archived. No new replies allowed.