Nested For Loops

Feb 22, 2011 at 4:17am
Ok, so its back to EECS 168 Lab work. right now we are working on nested for loops and we have a pretty simple lab assignment but for some reason (maybe im just tired) it is rattling my brain. here is the instructions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Refer to the wheat and chessboard problem given in Exercise One. Display the number of grains of wheat per square as if they were laid out on the board. Format the number of grains in exponent form with a base of two. Your output should look similar to below, but with the i replaced with the appropriate exponent in each square. For example, the fifth square should show 2^4.

You must use two nested for loops to complete this assignment with full credit.
Example output:

[2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i]
[2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i]
[2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i]
[2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i]
[2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i]
[2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i]
[2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i]
[2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i] [2^i]


The wheat chessboard problem is this:

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
According to legend, when the creator of the game of chess showed his invention to the ruler of the country, the ruler was so pleased that he gave the inventor the right to name his prize for the invention. The man, who was very wise, asked the king this: that for the first square of the chessboard, he would receive one grain of wheat, two for the second one, four on the third one, and so forth, doubling the amount each time. The ruler, arithmetically unaware, quickly accepted the inventor's offer, even getting offended by his perceived notion that the inventor was asking for such a low price, and ordered the treasurer to count and hand over the wheat to the inventor.

How many grains of wheat would be on the chessboard at the finish?

To solve this, observe that a chessboard is an 8×8 square, containing 64 squares (shown below).

[01] [02] [03] [04] [05] [06] [07] [08]
[09] [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] [40]
[41] [42] [43] [44] [45] [46] [47] [48]
[49] [50] [51] [52] [53] [54] [55] [56]
[57] [58] [59] [60] [61] [62] [63] [64]

If the amount doubles on successive squares, then the sum of grains on all 64 squares is:

Image:Wheat.png

Show the number of grains of wheat on each square of the board as well as the total grains of wheat for the board. Your output should look similar to this:

Square 1: 1 grain
Square 2: 2 grains
Square 3: 4 grains
.
.
.
Square 64: ??? grains

Total Grains of Wheat = ???

Save your source code for Exercise One as wheat.cpp.  


Ok, so here is my code for the first exercise (the wheat exercise one)
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
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{

double sum = 1;
double number;

for (number = 1; number <= 64; number++)
{
	sum = pow(2,number);

cout << "Square ";
cout << number;
cout << ": ";
cout << sum;
cout << " grain";
cout << endl;
}

cout << "\nTotal Grains of Wheat = ";
cout << sum << endl;

return 0;

}


I cant figure out how exactly they want me to use 2 for loops. I have thought of mutiple ways to do it without using a second for loop but i cant really figure out how to do it with 2. here is my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{

double sum = 1;
double number;

for (number = 1; number<=64; number++)
{
	sum = pow(2,number);
	cout << "[2^";
	cout << number;
	cout << "] " << endl;
}

cout << "\nTotal Grains of Wheat = ";
cout << sum << endl;

return 0;

}


Any help or ideas is much appreciated.
Feb 22, 2011 at 2:10pm
firstly, in your solution the first square has 2 grains. also the board is not formated.

this indeed could be done with a single for loop, but I think they want
1
2
3
4
for(int y = 0; y < 8; y++)
for(int x = 0; x < 8; x++){
   //you're on the square number x+y*8
}
Feb 22, 2011 at 3:47pm
Shouldn't line 13 be sum+=pow(2,number)?
Topic archived. No new replies allowed.