CISC105 Summer 2007 Lab04
Notes before you begin:
- You will write one program for each of the following problems. You may start each program using a previous program as a base if it seems suitable.
- Be sure to save each program as its own .c file. Name them labo4.n.c, where "n" is the number in the program list below. For example, the name of the file for the first program will be lab04.1.c
- Style counts. This includes header comments, comments on any tricky lines of code, good variable naming, good spacing and indentation. These are worth points to you.
- When scripting your lab, you will compile and run each one. For each .c file, you will first cat the file, then compile it, then run a.out and fully demonstrate the capability of the program. If your program does not compile, do not attempt to run it. Since it doesn't compile, no new a.out file is created--thus if you try to run it, you'll just be running the previous file which did compile.
- Please Remember that the lab is due electronically by Sunday at 11:55p.m. A 10% penalty is assessed for each day that the lab is late, and this penalty depends upon the electronic submission time (from myCourses).
Programs
Recall the programs from lab last week in which you used a loop to print a user-defined number of asterisks on a single line. The first three problems of this lab expand on this idea. You will be printing a square of asterisks, using three slightly different methods.
- In main, ask the user for the size of the square (we'll call it squareSize here). Then in a single loop, print a square of that size. Since you are printing a square, instead of looping to squareSize, you will have to loop to squareSize squared. You will then need a conditional statement inside the loop, which prints a single newline. Think very carefully: at what values of your loop counter should you print the newline? What operator might you use to see whether the counter is currently a multiple of another number? For clarity, a run of the program might look like this:
> ./a.out
How big would you like yoru square? 4
****
****
****
****
>
- Here, you will print the same square of asterisks, but you will do it in a different way. Use two loops (whichever type of loop you prefer), one nested inside of the other. There should be only a sin gle printf in the inner loop, which prints a single asterisk. The outer loop should contain only a single printf statement, which prints the newline. Remember, each loop will need a separate counter.
-
Finally, we're going to move the two parts into separate functions. First, define a function printAsteriskLine, which takes a single integer as a parameter, and prints that many asterisks on a line. Once you have tested printAsteriskLine, define another function, printAsteriskSquare, which uses printAsteriskLine in a loop to print a square of asterisks. Finally, once you have tested printAsteriskSquare, add a sentinel loop in main that continues to ask the user for a square size until the user enters -1.
The next few problems also build upon each other. As always, there should be a separate file for each, but it may be easiest to copy the contents of each problem to use as a base for the following problem. For example, you might make a copy of lab04.4.c to use as a base for lab04.5.c. Also, it is highly recommended that you do these following problems in small chunks. Think of how we do it in class--we add one small feature, and then test to make sure it works. This is imperative here, because the following problems get a little complicated. Write a few lines, test to make sure that they work, then consider the next step carefully, write a few lines, test, and so on.
- Declare an integer array of size 30. You should specify this size in a #define, because you are going to write functions that need to know the maximum size of the array. At each index of the array, assign to the array the square of that index. Do this in a loop. So at index 0 the array value is 0, at 1 it is 1, at 2 it is 4, at 3 it is 9, and so on. NExt, write a function that takes the array and an integer size as a parameter. The function should print the values of the array from 0 to index "size" out to the screen. It might look like this:
> ./a.out
To what index should the program print? 5
0 1 4 9 16 25
>
Make sure to test in the function that the size is valid--greater than zero and less than your defined array size. If the size is not valid, you should print no numbers, but rather an error message. Once you have tested the function, have main() ask the user for the size as input.
- Using lab04.4.c as a base, add another function. This function once again takes the array and an integer size as parameters. In this function, sum the numbers in the array from 0 .. size, and then print the sum. Last, add one final function, which does the same summing as the previous function, but instead prints the average of the numbers. Make sure error checking is in place, just like that of problem 4.
- You should now have 3 separate functions from lab04.5.c. make sure (in all versions, lab04.3.c-lab04.5.c) that the functions all have good names and are well-commented. Here, you are going to add one final element to your main(). Add a menu and a sentinel loop, which continues to ask which functions the user wants to call until the user enters -1. Use a switch statement to actually call the functions. The menu might look like this:
You have cohices. Please choose one of the following, or enter -1 to quite.
1) Print the values of the array
2) Sum the values of the array
3) Average the values of the array
Your choice?
After the user chooses a valid number, you will of course have to ask for the number of elements to use. for example, if the user chooses option (1):
Your choice? 1
To what index should the program print? 5
0 1 4 9 16 25
(then the menu is printed again)
That's it! Thoroughly test th eprogram you've just written, making sure that everything behaves how you expect before you begin scripting. Also, when scripting, make sure you fully demonstrate the functionality of your program, including the error-checking parts.
Submission
You should have a total of 6 programs, named lab04.1.c to lab04.6.c. Make a single script file (named lab04.txt) where you cat, compile, and run each program in sequence. That is, you should cat, compile and run lab04.1.c, then do the same with lab04.2.c, etc.
Submit all program and script files on MyCourses before 11:55 Sunday night. Give the paper version of your script to your TA next Monday at the beginning of lab. Write your name and the lab number on the front of your paper submission. Also, if something doesn't work properly, please circle it in bright ink and make a note for your TA explaining the problem. You will lose less points for a problem you recognize and can explain (at least a little) than for one you don't seem to notice.