A loop that contains no inner or nested loop represents a si
A loop that contains no inner (or nested) loop represents a single Repetition Control Structure, and is related to brandling in that a boolean expression will govern the behavior of the loop. Loop 0 above is an example of a singular loop, as are the following problems. Once we\'ve practiced with a few individual loops, we\'ll experiment further with nested loops (loops inside loops, just like the loop 2 above). Starting with one grain of rice, double the number of grains of rice you receive each day. If a king paid you for 64 days, how many grains of rice would you have? Build a short, standalone program with the class name GrainCounter that calculates and prints the number of grains you earn each day and the running sum of all the grains to date. Day 1 and you got 1 grain(s) of rice for a total of 1 grain(s). Day 2 and you got 2 grain(s) of rice for a total of 3 grain(s). Day 3 and you got 4 grain(s) of rice for a total of 7 grain(s). Day 30 and you got X grain(s) of rice for a total of Y grain(s). Day 31 and you got X grain(s) of rice for a total of Y grain(s). Day 32 and you got X grain(s) of rice for a total of Y grain(s).
Solution
Following program illustrates the use of class by creating an object.
#include <iostream>
#include <string>
using namespace std;
class GrainCounter
{
private : int total,days;
public :
GrainCounter(int n)
{
total=1;
days=n;
}
int TotalRice()
{
int current=1;
for(int i=1;i<=days;i++)
{
cout<<\"\ Day \"<<i<<\" and you got \"<<current<<\" grain of rice an d total of \"<<total<<\" grains.\";
current=current*2;
total+=current;
}
}
};
int main()
{
int n;
cout<<\"Enter number of days : \";
cin>>n;
GrainCounter grain(n);
grain.TotalRice();
return 0;
}
