Pages

Saturday, 30 July 2016

Chapter 4 Exercise // 8 & 9 - Principles & Practice Using C++

In all these exercises I am using Visual Studio Community 2015 and the header file "std_lib_facilities.h" which can be found here:


http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


My version is spelt differently so adjust the code accordingly if copying and pasting.


Chapter 4 Exercise // 4.8 

There is an old story that the emperor wanted tot hank the inventor of the game of chess and asked the inventor to name his reward. The inventor asked for one grain of rice for the first square, 2 for the second, 4 for the third, and so on, doubling for each of the 64 squares. That may sound modest, but there wasn't that much rice in the empire! Write a program to calculate how many squares are required to give the inventor at least 1000 grains of rice, at least 1,000,000 grains, ad at least 1,000,000,000 grains. You'll need a loop, of course, and probably and int to keep track of which square you are at, an int to keep the number of grains on the current square and an int to keep track of the grains on all the previous squares. We suggest that you write out the value of all your variables for each iteration of the loop so that you can see what's going on.


#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;


int main()
{
int currentSquare = 1;
int allGrains = 1;

for (int grainsCS = 1; grainsCS < 1000000000; ++currentSquare)
{
cout << "You are on square number: " << currentSquare << endl;
cout << "The number of grains on this square is: " << grainsCS << endl;
cout << "The total number of grains so far is: " << allGrains << '\n' << endl;

grainsCS = grainsCS * 2;
allGrains += grainsCS;
}

keep_window_open();

return 0;
}

To get at least 1000 you need 10 squares.
To get at least 1,000,000 you need 20 squares.
Te get at least 1,000,000,000 you need 30 squares.

When I first read this exercise I honestly thought I wouldn't be able to solve it. But after having done all the previous exercises and becoming for more comfortable with for and while loops, this took me around an hour to solve. 


Chapter 4 Exercise // 4.9

Try to calculate the number of rice grains that the inventor asked for in exercise 8 above. You'll find that the number is so large that it won't fit in an int or a double. Observe what happens when the number gets too large to represent exactly as an int and as a double. What is the largest number of squares for which you can calculate the exact number of grains (using an int)? What is the largest number of squares for which you can calculate the approximate number of grains (using a double)?


#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;


int main()
{
int currentSquare = 1;
double allGrains = 1;

for (double grainsCS = 1; currentSquare <= 64; ++currentSquare)
{
cout << "You are on square number: " << currentSquare << endl;
cout << "The number of grains on this square is: " << grainsCS << endl;
cout << "The total number of grains so far is: " << allGrains << '\n' << endl;

grainsCS = grainsCS * 2;
allGrains += grainsCS;
}

keep_window_open();

return 0;
}

You can get up to square 32 on an int.
You can get to square 20 on a double before it starts showing it in notation form.



No comments:

Post a Comment