/* ************************************************************************** * Program name : 017_Checkerboard_pattern (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/10 - first version * ************************************************************************** * Description : (a) Input the size of the checkerboard pattern * * (b) Output the checkerboard pattern * * (c) Try again ? * ************************************************************************** */ #include #include int main() { // part 1 : declaration int Size, Index_1, Index_2; char again; do { // part 2 : Repeat asking the size of the checkerboard pattern do { cout << "Please input the size of the checkerboard pattern (2-15): "; cin >> Size; if (Size<2 || Size >15) cout << "The number is too big or too small!\n"; cout << "\n"; } while (Size<2 || Size >15); // part 3 : Repeat asking the value of exponent for (Index_1=1; Index_1<=Size; Index_1++) // Note (1) { for (Index_2=1; Index_2<=Size*2; Index_2++) // Note (2) if ((Index_1+Index_2) % 2 ? cout << " " : cout << "*"); cout << "\n"; }; cout << "\n\n"; // part 4 : try another number ? cout << "\n\t\aTry again (Y/N) : "; cin >> again; cout << "\n"; } while (again=='Y' || again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } /* Notes (1) The outer loop counts the number of row printed. If the size of the checkerboard pattern is 5, then the value of the variable Index_1 will be changed from 1 to 2, then from 2 to 3 and so on until it becomes 5. (2) The inner loop counts the number of single symbol or space printed. The numbers of symbol or space printed on each line is controled by the value of variable Size. When Size is equal to 5 , then there should be 5 pairs of "* " or " *" printed. Thus there are 5 symbol and 5 spaces printed. Example : Index_1 Index_2 Index_1+Index_2 (Index_1+Index_2)%2 Output 1 1 2 0 "*" 1 2 3 1 " " 1 3 4 0 "*" 1 4 5 1 " " 1 5 6 0 "*" 1 6 7 1 " " 1 7 8 0 "*" 1 8 9 1 " " 1 9 10 0 "*" 1 10 11 1 " " The whole line becomes "* * * * * " for the odd number row. The whole line becomes " * * * * *" for the even number row. */