Answer to Question #250104 in C++ for John

Question #250104

Write a Program: Print a table of stars and zeroes 

print a square table of alternating stars and zeroes. 


Ask the user to enter a size of the table. The 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. 




Each row in said table should have ‘*’ 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.

enter an odd size from 3 to 15: 3


* 0 *

0 * 0

* 0 *





1
Expert's answer
2021-10-12T05:14:17-0400
#include<iostream>
using namespace std;
int validate(int n){
	if((n % 2==0) || (n<3 ) || (n>15)){
		cout<<"Invalid number\n";
		return 0;
	
	}
	else{
		return 1;
	}
}
int main(){
	int x;
	do{
		cout<<"Enter an odd size from 3 to 15: \n";
		cin>>x;
	}while(validate(x) ==0);
for(int i=1; i<=x; i++){
	if(i % 2 != 0 ){
		cout<<" * ";
	}
	else{
		cout<<" 0 ";
	}
	for(int j=2; j<= x; j++){
		if(j%2==0 && i%2==0){
			cout<<" * ";
		}
		else if (j%2 !=0 && i%2 !=0){
			cout<<" * ";
		}
		else{
			cout<<" 0 ";
		}
	}
	cout<<"\n";
		
	}
	


	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS