Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming language is C++. The code for problem 3 is shown below. Show code an

ID: 3784152 • Letter: P

Question

Programming language is C++. The code for problem 3 is shown below. Show code and an example of the output on PuTTY when done.

#include<stdio.h>
int main()
{

   int sq = 1;
   long int gr = 1;
   long int tot = 1;
   long int count = 0;

   for (sq = 2; sq <= 64; sq++)
   {
       gr = gr * 2;
       tot = tot + gr;

       if (tot >= 1000 && count<1)
       {
           count++;
           printf("%d squares are required to get at least 1000 grains ", sq);

       }
       if (tot >= 1000000 && count <2)
       {
           count++;

           printf("%d squares are required to get at least 1000000 grains ", sq);

       }
       if (tot >= 1000000000 && count <3)
       {
           count++;
           printf("%d squares are required to get at least 1000000000 grains ", sq);
           break;
       }
   }

}

4. (10 points) Modify problem 3 to print the exact number of grains on all 64 squares Use unsigned long long instead of int (Note This works on our two compilers but may not work on other C++ compilers Name your program hw1pr4.cpp

Explanation / Answer

#include<stdio.h>
int main()
{

   int sq = 1;
   long int gr = 1;
   long int tot = 1;
   long int count = 0;

   for (sq = 2; sq <= 64; sq++)
   {
       gr = gr * 2;
       tot = tot + gr;

       if (tot >= 1000 && count<1)
       {
           count++;
           printf("%d squares are required to get at least 1000 grains ", sq);

       }
       if (tot >= 1000000 && count <2)
       {
           count++;

           printf("%d squares are required to get at least 1000000 grains ", sq);

       }
       if (tot >= 1000000000 && count <3)
       {
           count++;
           printf("%d squares are required to get at least 1000000000 grains ", sq);
           break;
       }
   }

}