Write a Program: Print a table of stars and zeroes. Your goal is to print a square table of alternating stars and zeroes. Ask the user to enter a size of the table. This number should be odd. Since the table is square the number of rows and columns will be the same and should be equal to the entered size. The valid range for the size is from 3 to 15, inclusive. Use a loop to make sure the entered size is odd and within the range. Output a descriptive error message if an invalid size is entered. The error message should indicate if the size is even, or less than the minimum, or larger than the maximum. Do not create separate output strings for all combinations of error conditions – your program should build the output.
Once a valid size is entered print the table to the console. Each row of the table should have alternating ‘*’ and ‘0’ symbols separated by a space. Each column of the table should also have alternating ‘*’ and ‘0’ symbols. All corners should have the ‘*’ symbol.
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int n;
cin >> n;
if (n%2 == 0) {
cout << "n is not odd" << endl;
exit(1);
}
char ch;
for (int i=0; i<n; i++) {
if (i%2 == 0) {
ch = '*';
}
else {
ch = '0';
}
for (int j=0; j<n; j++) {
cout << ch << ' ';
ch = ch == '*' ? '0' : '*';
}
cout << endl;
}
return 0;
}
Comments
Leave a comment