Homework 7 & 8: X Box

Part 1 (Stages 1 & 2) Due: Tuesday 10/25 by 1pm

Part 2 (Stages 3, 4 & 5) Due: Tuesday 11/1 by 1pm

Objectives

For this homework assignment, you will practice programming with nested for loops. You will also practice incremental programming --- writing large programs a bit at a time. You will also likely encounter several off-by-one errors in this project, so you'll have practice fixing those bugs.

The Assignment

Note, you must properly indent your programs. This is very important, because poorly indented programs are hard to read and hard to debug. Follow one of the styles listed in CMSC104 Indentation Styles.

Also, our projects are getting more complicated. Starting from this assignment forward, we will start deducting points from programs that do not have enough comments.

Sorry for you gamers out there, but we are not actually going to implement Microsoft's Xbox system. We'll just write a program that draws an "X" inside a box, like so:


*  *  *  *  *  *  *  *  *  *  *  *  *  
*  *                             *  *
*     *                       *     *
*        *                 *        *
*           *           *           *
*              *     *              *
*                 *                 *
*              *     *              *
*           *           *           *
*        *                 *        *
*     *                       *     *
*  *                             *  *
*  *  *  *  *  *  *  *  *  *  *  *  *  

The size of the box is determined by the user. The box above is size 13. Here's a size 12 box:

PT[300]% ./a.out
Enter size of box: 12
*  *  *  *  *  *  *  *  *  *  *  *  
*  *                          *  *
*     *                    *     *
*        *              *        *
*           *        *           *
*              *  *              *
*              *  *              *
*           *        *           *
*        *              *        *
*     *                    *     *
*  *                          *  *
*  *  *  *  *  *  *  *  *  *  *  *  
PT[301]%

Notice that the even size boxes look different from odd size boxes in the middle. This is a required feature.

You are required to implement this program in stages and submit a separate program for each stage. Stage 1 and Stage 2 are due the first week. Stages 3, 4 and 5 are due the second week.



Stage 1

Stage 1 is simple. Just draw the top line, the left edge and the bottom line. Make sure that you have the right number of asterisks. For horizontal spacing, add two spaces between the asterisks.

A sample run of your program might look like:

PT[308]% ./a.out
Enter size of box: 9
*  *  *  *  *  *  *  *  *  
*
*
*
*
*
*
*
*  *  *  *  *  *  *  *  *  
PT[309]% 

Record several runs of your program using the script command and submit your program as xbox1.c to hw07.

submit cs104_chang hw07 xbox1.c
submit cs104_chang hw07 typescript1



Stage 2

Make a copy of your program from Stage 1 and call it xbox2.c.

For Stage 2, add the first diagonal line to your box. You will need to use nested for loops to do this. For each row of the box (not counting the top and the bottom), you have to print a * for the left edge, some number of spaces, and then the * for the diagonal.

A sample run of your program might look like:

PT[311]% ./a.out
Enter size of box: 11
*  *  *  *  *  *  *  *  *  *  *  
*  *  
*     *  
*        *  
*           *  
*              *  
*                 *  
*                    *  
*                       *  
*                          *  
*  *  *  *  *  *  *  *  *  *  *  
PT[312]% 

Record several runs of your program using the script command and submit your program as xbox2.c to hw07.

submit cs104_chang hw07 xbox2.c
submit cs104_chang hw07 typescript2

You are done for the Homework 7 portion of this project. You can of course continue to Stage 3 if you are on a roll.



Stage 3

Make a copy of your program from Stage 2 and call it xbox3.c.

For Stage 3 you realize that you don't want to draw a diagonal line all the way to the bottom right of the box. This is because you can't draw the second diagonal on top of the first one. (We're not using pen and paper, after all.)

Instead, your first diagonal should cut back to the bottom left corner, like so:

PT[315]% ./a.out
Enter size of box: 13
*  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  
*     *  
*        *  
*           *  
*              *  
*                 *  
*              *  
*           *  
*        *  
*     *  
*  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  
PT[316]% 

Here you have to do something different for the first half of the rows and have a separate inner for loop for the bottom half. It is helpful to print out some row numbers, just for debugging purposes:

PT[328]% ./a.out
Enter size of box: 13
 1 *  *  *  *  *  *  *  *  *  *  *  *  *  
 2 *  *  
 3 *     *  
 4 *        *  
 5 *           *  
 6 *              *  
 7 *                 *  
 8 *              *  
 9 *           *  
10 *        *  
11 *     *  
12 *  *  
13 *  *  *  *  *  *  *  *  *  *  *  *  *  

You can use %2d in your printf statements to get the row numbers to take up 2 spaces.

Make sure that your program also works for even sized boxes:

PT[330]% ./a.out
Enter size of box: 12
 1 *  *  *  *  *  *  *  *  *  *  *  *  
 2 *  *  
 3 *     *  
 4 *        *  
 5 *           *  
 6 *              *  
 7 *              *  
 8 *           *  
 9 *        *  
10 *     *  
11 *  *  
12 *  *  *  *  *  *  *  *  *  *  *  *  
PT[331]% 

Record several runs of your program (with odd and even sizes) using the script command and submit your program as xbox3.c to hw08.

submit cs104_chang hw08 xbox3.c
submit cs104_chang hw08 typescript3



Stage 4

Make a copy of your program from Stage 3 and call it xbox4.c.

For this stage, you will complete the "X". For each row, you need to print the right number of spaces and another *.

Note that even and odd size boxes are drawn differently. The middle row of odd size boxes has only one *. You will need to special case this feature using an if statement.

Make sure that your program works for even and odd size boxes:

PT[338]% ./a.out
Enter size of box: 13
 1 *  *  *  *  *  *  *  *  *  *  *  *  *  
 2 *  *                             *  
 3 *     *                       *  
 4 *        *                 *  
 5 *           *           *  
 6 *              *     *  
 7 *                 *  
 8 *              *     *  
 9 *           *           *  
10 *        *                 *  
11 *     *                       *  
12 *  *                             *  
13 *  *  *  *  *  *  *  *  *  *  *  *  *  
PT[339]% 

PT[339]% ./a.out
Enter size of box: 12
 1 *  *  *  *  *  *  *  *  *  *  *  *  
 2 *  *                          *  
 3 *     *                    *  
 4 *        *              *  
 5 *           *        *  
 6 *              *  *  
 7 *              *  *  
 8 *           *        *  
 9 *        *              *  
10 *     *                    *  
11 *  *                          *  
12 *  *  *  *  *  *  *  *  *  *  *  *  
PT[340]% 

Record several runs of your program (with even and odd size boxes) using the script command and submit your program as xbox4.c to hw08.

submit cs104_chang hw08 xbox4.c
submit cs104_chang hw08 typescript4



Stage 5

Make a copy of your program from Stage 4 and call it xbox5.c.

Now you are ready to finish the program. You just have to print the right number of spaces and then print a * for the left edge.

Sample runs of your program might look like:

PT[345]% ./a.out
Enter size of box: 13
 1 *  *  *  *  *  *  *  *  *  *  *  *  *  
 2 *  *                             *  *
 3 *     *                       *     *
 4 *        *                 *        *
 5 *           *           *           *
 6 *              *     *              *
 7 *                 *                 *
 8 *              *     *              *
 9 *           *           *           *
10 *        *                 *        *
11 *     *                       *     *
12 *  *                             *  *
13 *  *  *  *  *  *  *  *  *  *  *  *  *  
PT[346]%

PT[346]% ./a.out
Enter size of box: 16
 1 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
 2 *  *                                      *  *
 3 *     *                                *     *
 4 *        *                          *        *
 5 *           *                    *           *
 6 *              *              *              *
 7 *                 *        *                 *
 8 *                    *  *                    *
 9 *                    *  *                    *
10 *                 *        *                 *
11 *              *              *              *
12 *           *                    *           *
13 *        *                          *        *
14 *     *                                *     *
15 *  *                                      *  *
16 *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
PT[347]% 

Record several runs of your program using the script command and submit your program as xbox5.c to hw08.

submit cs104_chang hw08 xbox5.c
submit cs104_chang hw08 typescript5

Ta Da! You're done!