Objectives Get Familiar with C Basics, Including

Objectives Get Familiar with C Basics, Including

Assignment 1

Due Date: 10am, January 26th , Monday, 2004

Objectives Get familiar with C basics, including

control constructs

command line arguments

random generator

basic data types

printf

problem decomposition and user defined functions

Problem Statement For this assignment you are asked to write a C program that draws a staircase on screen, as shown in Figure 1, using the printf statement. Each step of the staircase is W wide, and W/2 high, where integer W is randomly generated between 3 and 5. The number of steps is randomly generated between 2 and 7. The staircase could be upward, downward or both, depending on the command line argument. All staircases start from top-left corner (downward) or top-right corner (upward) of the screen. Depending on the system settings, some terminals could be up to 150 characters wide. To be consistent, assume that the screen is 79 characters wide and it is big enough to display the whole graph. You can download this demo (right click, save link as) to see what is expected. It can only be run on Linux by typing in staircase -1, or staircase -2, or staircase -3. You need to change the file attribute before running it. On a terminal, enter chmod +x staircase.

The total marks for this assignment are 15 plus 3 bonus, which are sketched out below.

Figure 1. Downward Staircase

Question #1 Downward (5 marks) Implement a function which draws a downward staircase like Figure 1. This function will be called and display the proper staircase when a user enters -1 as command line argument, that is, staircase -1 (assume that the executable file is staircase).

Question #2 Upward (5 marks) Implement a function which draws an upward staircase like Figure 2. This function will be called and display the proper staircase when a user enters -2 as command line argument, that is, staircase -2 (assume that the executable file is staircase).

Figure 2. Upward Staircase

Question #3 Downward and upward (5 marks) Implement a function which draws a downward staircase followed by an upward like Figure 3, 4 or 5. This function will be called and display the proper graph when a user enters -3 as command line argument, that is, staircase -3 (assume that the executable file is staircase). You have two options for this question.

Option #1 (Maximal 5 marks): asymmetric or symmetric. You need to randomly generate two numbers between 2 and 7, one representing the number of downward steps, the other for upward. These two number might not be the same, resulting in an asymmetric staircase like Figure 3 (downward is greater than upward) and 4 (downward is greater than upward).

Option #2 (Maximal 3 marks): symmetric only. You need to generate only one number between 2 and 7, which represents the number of downward steps, as well as upward, resulting in a symmetric staircase like Figure 5.

Figure 3. Asymmetric Staircase, starting from the right

Figure 4. Asymmetric Staircase, starting from the left

Figure 5. Symmetric Staircase, always starting from the left

Question #4 Surprise me (3 bonus) You are not condemned to any specifics for this question. Use your own creativity and draw any kind of staircase you can imagine. Following are some ideas that might help broaden your dimension.

101-step spiral staircase, downward. The specifications given for the above questions guarantee that the staircase won't exceed the screen dimension. What happens if we need to draw a 101-step staircase? The screen is surely not big enough to draw the whole thing. Here we want to handle such case properly like Figure 6. As you can see, when a staircase reaches the right-hand side of the screen, it is relayed by a new staircase starting from right-hand side; when it reaches the left-hand side of the screen, it is relayed by another new staircase starting from left-hand side. The real challenge rises from the step crossover the screen boundary. As shown in Figure 6, the last step of the first staircase, 7 characters wide, is only drawn 5, and the rest 2 is drawn as the first step of the second/relayed staircase.

A staircase filled with different patterns.

A staircase always starting at the center of the screen, or one step beyond, a staircase starting at a random location

These are merely suggestions and you don't have to follow them. You are encouraged to create your own.

Figure 6. Spiral Staircase

Hand-in Procedure (Important) You must submit the following files for this assignment: staircase.c and readme.txt (and staircase.h if you have it), where staircase.c stores you C source code and readme.txt contains brief descriptions of your program, including:

Which questions you have done

How to execute your program. If command-line arguments are required, explain what they are, what they represent and how to use them

If you have answered Question #4, describe what you did

Limitation or special features that worth mentioned

How many hours you spent on this assignment

You must hand in your program through WebCT:

To upload completed assignments, click Assignment and lab test submission. The Assignments screen appears.

Under Title, click the assignment that you want to submit. The Assignment screen appears.

Click Student files. The Student Files for Assignment screen appears.

Click Upload. The Upload File for Assignment screen appears.

To locate the file, click Browse. Your computer's file manager appears.

Select the file. The Upload File For Assignment screen reappears, with the name of the file in the Filename text box.

Click Upload. The Student Files for Assignment screen appears.

Click the Return to Assignment link at the top of the screen. The Assignment screen appears.

Click Submit assignment. The Submit Assignment screen appears. You can receive email notification that your assignment was submitted successfully. In the text box, type your email address.

Click Submit assignment. A confirmation message appears.

Click OK. The Assignments screen appears with the message Submitted appearing in the Status column. When the assignment has been graded, this message changes to a Graded link. Click the Graded link to view your grade and any instructor comments.

** There is a very common mistake that students upload their files and forget to hit the submission button. Remember, this is a two-step procedure: upload your file first then submit. If you have more than one files, you need to upload several times, but only need to submit once.

Your program will be marked based on style and correctness. Style includes proper indentation, meaningful identifier names and comments (explanation of the functionality of each function implemented, the meaning of important variables etc). The maximal deduction for lacking or poor style is 20% of the total grade you earn. For example, a student did Q1 and Q2 but with poor indentation and no comment. His/her final grade for this assignment would be 10-20*10%=8. Not submitting the readme.txt file subjects to 10% deduction.

If you develop your program on a different platform (i.e. different system and tool), make sure it is compatible with Redhat Linux 9.0 and gcc 3.2.2, which are used to compile, execute and mark your assignment. A program that cannot be compiled or is not submitted properly receives a grade of ZERO. If you have any questions about the hand-in procedure, please ask the TAs or instructors well before the due date.