In: Computer Science
Task G. Checkerboard (3x3)
Write a program checkerboard3x3.cpp that asks the user to input width and height and prints a checkerboard of 3-by-3 squares. (It should work even if the input dimensions are not a multiple of three.) Don't use function. Please explain in detail and make it simple. And be clear with the {}. Thank you in advance.
Example 1:
Input width: 16 Input height: 11 Shape: *** *** *** *** *** *** *** *** *** *** *** * *** *** * *** *** * *** *** *** *** *** *** *** *** *** *** *** * *** *** *
Example 2:
Input width: 27 Input height: 27 Shape: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Thanks for the question, here is the simple program in C++; that draws the checkers board for any input
==========================================================================
#include<iostream>
using namespace std;
int main(){
int width;
int height;
int alternate_width;
cout<<"Input width: "; cin>>width;
cout<<"Input height: "; cin>>height;
int rowType;
int colType;
for(int row=0; row<height; row++){
rowType = row/3;
if(rowType%2==1){
alternate_width=width-3;
cout<<"
";
}else{
alternate_width=width;
}
for(int col=0;
col<alternate_width; col++){
colType=col/3;
if(colType%2==0)cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}