A grateful king told the man who saved the kingdom to choose

A grateful king told the man who saved the kingdom to choose any reward he wanted. The man asked that he be given one grain of wheat for the first square on a chessboard, 2 grains of wheat for the second square, 4 grains for the third square, and so on, doubling each time. The king, who was not a mathematician, agreed. There are 64 squares on a chessboard. Write a program to list, for each square, the number of grains of wheat on that square. Do the program in 2 ways: Use an integer variable for the number of grains. Use a double variable for the number of grains. Then observe your output (which is to be in a file) to find out whether your results are accurate. If they are not accurate, which square is the first bad one (doesn\'t have the correct number of grains on it)? Put your observations in a comment in the program source file. Turn in the source file and the output file.

Solution

C++ code:

#include <bits/stdc++.h>
using namespace std;

int main()
{

ofstream myfile;
myfile.open(\"output.txt\");
myfile << \"Using Integer Variable\ \";
myfile << \"Square Number\" << \'\\t\' << \"Number of Grains\" << endl;   
int Grains;
for (int i = 0; i < 64; ++i)
{
   Grains = pow(2,i);
   myfile << i+1 << \"\\t\\t\\t\\t\" << Grains << endl;    
}
myfile << \"Using Integer Variable\ \" ;
myfile << \"Square Number\" << \"\\t\" << \"Number of Grains\" << endl;   
double Grainsd;
for (int i = 0; i < 64; ++i)
{
   Grainsd = pow(2,i);
   myfile << i+1 << \"\\t\\t\\t\\t\" << Grainsd << endl;    
}

myfile.close();
return 0;
}

Output.txt

Using Integer Variable
Square Number   Number of Grains
1               1
2               2
3               4
4               8
5               16
6               32
7               64
8               128
9               256
10               512
11               1024
12               2048
13               4096
14               8192
15               16384
16               32768
17               65536
18               131072
19               262144
20               524288
21               1048576
22               2097152
23               4194304
24               8388608
25               16777216
26               33554432
27               67108864
28               134217728
29               268435456
30               536870912
31               1073741824
32               -2147483648
33               -2147483648
34               -2147483648
35               -2147483648
36               -2147483648
37               -2147483648
38               -2147483648
39               -2147483648
40               -2147483648
41               -2147483648
42               -2147483648
43               -2147483648
44               -2147483648
45               -2147483648
46               -2147483648
47               -2147483648
48               -2147483648
49               -2147483648
50               -2147483648
51               -2147483648
52               -2147483648
53               -2147483648
54               -2147483648
55               -2147483648
56               -2147483648
57               -2147483648
58               -2147483648
59               -2147483648
60               -2147483648
61               -2147483648
62               -2147483648
63               -2147483648
64               -2147483648
Using Integer Variable
Square Number   Number of Grains
1               1
2               2
3               4
4               8
5               16
6               32
7               64
8               128
9               256
10               512
11               1024
12               2048
13               4096
14               8192
15               16384
16               32768
17               65536
18               131072
19               262144
20               524288
21               1.04858e+06
22               2.09715e+06
23               4.1943e+06
24               8.38861e+06
25               1.67772e+07
26               3.35544e+07
27               6.71089e+07
28               1.34218e+08
29               2.68435e+08
30               5.36871e+08
31               1.07374e+09
32               2.14748e+09
33               4.29497e+09
34               8.58993e+09
35               1.71799e+10
36               3.43597e+10
37               6.87195e+10
38               1.37439e+11
39               2.74878e+11
40               5.49756e+11
41               1.09951e+12
42               2.19902e+12
43               4.39805e+12
44               8.79609e+12
45               1.75922e+13
46               3.51844e+13
47               7.03687e+13
48               1.40737e+14
49               2.81475e+14
50               5.6295e+14
51               1.1259e+15
52               2.2518e+15
53               4.5036e+15
54               9.0072e+15
55               1.80144e+16
56               3.60288e+16
57               7.20576e+16
58               1.44115e+17
59               2.8823e+17
60               5.76461e+17
61               1.15292e+18
62               2.30584e+18
63               4.61169e+18
64               9.22337e+18

From the output file, we can see that in case of integer, results are accurate upto 31st square places.

And in case of double, results are accurate for all 64 sqaures.

Reason behind this is size of int variable is 32 bits and double variable is 64 bits.

 A grateful king told the man who saved the kingdom to choose any reward he wanted. The man asked that he be given one grain of wheat for the first square on a
 A grateful king told the man who saved the kingdom to choose any reward he wanted. The man asked that he be given one grain of wheat for the first square on a
 A grateful king told the man who saved the kingdom to choose any reward he wanted. The man asked that he be given one grain of wheat for the first square on a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site