Extra Credit 2: Box in Box
Due: Friday May 11, 2012
Note: This extra credit assignment is worth 2 points (same as homework). Recall that extra credit can only boost your homework and classwork scores and not your quiz and final exam scores. Also, extra credit will only be given for submissions that are mostly correct. For example, a submission that would receive 5/10 on homework would not receive any points for extra credit. Please consult the syllabus: Syllabus: Extra Credit.
Objectives
more practice with nested for loops.
The Assignment
Write a program that asks the user for the length and height of a rectangle. Your program draws a rectangle of that size and a smaller rectangle inside it. The smaller rectangle has half the size of the larger rectangle. Sample runs of your program might look like:
PT[274]% ./a.out Enter # of rows: 12 Enter length of each row: 12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * PT[275]%
PT[275]% ./a.out Enter # of rows: 8 Enter length of each row: 16 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * PT[276]% ./a.out
Notes
- There are spaces between the *'s in the sample runs above. This is just to make the rectangles look nice. You don't have to have the same number of spaces as these examples, just make sure that they are consistent.
- This is a good opportunity to practice incremental
programming. As a first step, write a program that draws the outer
box. Then, print out the row and column indices along with the box,
something like:
PT[115]% ./a.out Enter # of rows: 8 Enter length of each row: 16 1: * * * * * * * * * * * * * * * * 2: * 2 3 4 5 6 7 8 9 10 11 12 13 14 15 * 3: * 2 3 4 5 6 7 8 9 10 11 12 13 14 15 * 4: * 2 3 4 5 6 7 8 9 10 11 12 13 14 15 * 5: * 2 3 4 5 6 7 8 9 10 11 12 13 14 15 * 6: * 2 3 4 5 6 7 8 9 10 11 12 13 14 15 * 7: * 2 3 4 5 6 7 8 9 10 11 12 13 14 15 * 8: * * * * * * * * * * * * * * * * PT[116]%
- If you leave these index numbers inside the rectangle until the very end, they will help you figure out how to draw the inner box.
- You need to figure out the "coordinates" of the inner rectangle. The coordinates of the top-left corner of the inner rectangle is roughly 1/4 of the length and height of the outer rectangle.
- Once you figure out the locations of the 4 corners of the inner rectangle, you are pretty much done.
Submit
Use the script command to record several sample runs of your program. Demonstrate that your program works for different size rectangles as well as non-square rectangles. Don't forget to exit from script. Then, submit your program and sample run as usual:submit cs104_chang ec2 boxes.c typescript