Nested Loops
/* Write a program that prints the stars of the flag of the
United States of America on the screen:
* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
The stars are arranged in 9 rows and 11 columns (another reason for the 911 date?).
Write a more versatile program so that the user can input any 2
positive odd numbers as the number of rows and columns,
You must take appropriate precautions to assure the correctness of the input data:
the number of rows and columns must be positive and odd.
Do not use any kind of strings.
You can only print a character at a time.
A possible screen output is shown below:
You must enter two positive and odd numbers
to be the number of rows and columns: 4 7
You must enter two positive and odd numbers
to be the number of rows and columns: 9 11
* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
*/
#include <iostream>
using namespace std;
int main ( )
{
int maxrows, maxcols;
char ch = '*';
do
{
cout << "You must enter two positive and odd numbers\n"
<< "to be the number of rows and columns: ";
cin >> maxrows >> maxcols;
cout << endl;
} while ( (maxrows%2 ==0) ||(maxcols%2 == 0) || (maxrows < 0)||(maxcols < 0) );
for (int row = 1; row <= maxrows; row++)
{
for(int col = 1; col <= maxcols; col++)
{
cout << ch;
if (ch == '*')
ch = ' ';
else
ch = '*';
}
cout << endl; // go to the next row
}
cout << endl;
return 0;
}