Monday, September 3, 2012

Homework 1

We have already seen how to draw a checkerboard, here is a similar problem. The desired output looks like this:
OXOXOXOX
XXXXXXXX
OXOXOXOX
XXXXXXXX
OXOXOXOX
XXXXXXXX
OXOXOXOX
XXXXXXXX
It is an 8 by 8 board. The odd-numbered rows are X's, and the odd-numbered columns are X's. The other characters are O's.

Solution (just for reference, please write your own code):

class Checkerboard
{
  public static void main ( String[] args )
  {
        int i, k;
        
        for (i = 0; i < 8; i++)        
        {
            if ((i % 2) == 1)
                for (k = 0; k < 8; k++)
                    System.out.print("X");
            else
                for (k = 0; k < 8; k++)
                    if ((k % 2) == 1)
                        System.out.print("X");
                    else
                        System.out.print("O");    
            System.out.println();
        }    
  }
}