#include <iostream> #include "simpio.h" using namespace std; int main() { int p2n = 1; int n = getInteger("n?"); for (int i = 0; i < n; i++) { int tmp = p2n; cout << "2^" << i << " = " << tmp << endl; p2n = tmp * 2; } cout << "2^n = " << p2n << endl; }For this program above, what is printed for the following input values of
n
: 4, 7, 9?
Now, consider the following program:
#include <iostream> #include "simpio.h" using namespace std; int main() { int p2n = 1; int n = getInteger("n?"); for (int i = 0; i < n; i++) { int tmp = p2n; cout << "2^" << i << " = " << tmp << endl; int p2n = tmp * 2; } cout << "2^" << n << " = " << p2n << endl; }For this program above, what is printed for the following input values of
n
: 4, 7, 9?
What is the difference between the two programs? Why are the outputs of the two programs so different?
Now consider the following program:
#include <iostream> #include "simpio.h" using namespace std; int main() { int p2n = 1; int n = getInteger("n?"); for (int i = 0; i < n; i++) int p2n = p2n * 2; cout << "2^" << n << " = " << p2n << endl; }Notice that there are no curly braces
{ }
in the body of the for loop. What is the output that you get for different input values of n
? Explain.
***** * * * * *****Think about what additional helper functions you would like to write to facilitate the above operation. For instance, you can write two additional functions drawLine(width) which draws a line of given width, and drawSide(width) which draws (a part of) the sides of the box for a given width, and then call these functions multiple times.