u

Friday, 14 September 2012

C++ Program to print out a checkered board of size n

C++ Program to print out a checkered board of size n

 #include <iostream.h>
#include <conio.h>

const int cellSize = 4;
void printnc(char c, int n){
   for(int k = 0; k<n; ++k)
      cout << c;
}

void printBWcells(void){
   printnc('.', cellSize);
   printnc(' ', cellSize);
}

void printWBcells(void){
   printnc(' ', cellSize);
   printnc('.', cellSize);
}

void printBoard(int n){
   printnc('*', 2+2*n*cellSize);
   cout << endl;
   for(int k = 0; k<n; ++k){
      for(int h = 0; h < cellSize; ++h){
     cout << '*';
     for(int m=0; m<n; ++m){
        if((k%2)==0)
          printBWcells();
        else
          printWBcells();
     }
     cout << '*' << endl;
      }
   }
   printnc('*', 2+2*n*cellSize);
   cout << endl;
}

void main(void){
   int n;
   clrscr();

   cout << "Enter size of board: ";
   cin >> n;
   clrscr();

   printBoard(n);
   cout<<endl;
   cout<<"Press any key to continue";
   getch();
}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More