Lab02, CISC105 Summer 2005

Directions

Practicing UNIX

Create a directory for this lab and then go into that directory to create, compile, and execute your programs.

Programs

  1. (15) Write a program that sums all the odd integers between 0 and 100 and prints the total. Use a defined constant to stop your loop.
  2. (15) The Fibonacci sequence is 1, 1, 2, 3, 5, 8, 13, 21, ... The pattern is that the nth number is Fn=Fn-1 + Fn-2. The sequence is defined that F0=F1=1. Write a program that computes the first 15 numbers in the Fibonacci sequence.
  3. (20) Write a program to write the multiplication tables from 0 to 12.

    Example output:

    x | 0 | 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 | 12 |
    ---------------------------------------------------------------
    0 | 0 | 0 | 0 | 0 | 0 |  0 |  0 |  0 |  0 |  0 |  0 |  0 |  0 |
    1 | 0 | 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 | 12 |
    2 | 0 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 |
    
  4. (15) Write a program that prints the minimum of 10 numbers. (Make no assumptions about the inputted numbers. How can you handle not initializing the minimum (because any number you pick as the minimum could be greater than an inputted number)?)
  5. (15) Write a program that prints the average for students' grades entered as user input. (When should you stop reading user input?)
  6. (20) Write a program that prints the minimium, maximum, and average grades for students' grades entered as user input. (Note that for the best performance and full credit, you will calculate the minimum, maximum, and average in one loop.)
  7. (10) In class, we wrote a program to draw a square of asterisks using a for loop. Modify the program so that it includes a void function that prints a single asterisk (only). Call the void function-- instead of printf--inside your loop.
  8. (10) Write a void function ``square'' that prints the square of asterisks. (Basically, put the previous program in a function.) In main, put the call to square inside a loop so that it prints three squares.
  9. (10) Copy the previous program into a new program. Change the main() function so that it is a void function ``threeSquares''. Now write a new main() that calls only threeSquares.
    This is an example of an excellent programming technique. Solve a problem in your main() function, then make the solution into a separate function. By breaking a big program down into steps, you can solve each step in the main, move it, and work on the next.
  10. (20) Write a program with a void function that takes two integer parameters, width and height, and draws a rectangle of those dimesions. To draw the rectangle, use a loop, the mod operator (%), and a single printf('*'). Put the function call inside a loop so you can try new dimensions (from user input) without exiting the program.
  11. (10 points) Grab the code from lab02.scope.c. Compile and execute the code. Explain the output with respect to scoping rules in comments.

You should have a total of 11 programs named lab02.1.c to lab02.11.c. Make a single script file (see lab00 for the instructions) where you cat, compile, and run each one in its final form (if it didn't compile, don't run it in the script - mark the place in the printed script file with a marker so it stands out).

Note: Cat, compile, and run each program in order! Do not cat all programs, then compile, etc.

Execute your program multiple times to show that you tested the program well.

More Practicing UNIX

As discussed in class, run ls -la in your lab02 directory. On your printed script file, label the columns for the files' permissions, sizes, dates, and names. Then, label the largest and smallest files and the oldest and newest files. (10 points)

Submission

Email the tar file to Gang by midnight on Wednesday (see lab00 for directions). Give the paper version to Gang at the beginning of your next lab. (15 points for each submission.)

Grading

Grading is as noted above for each program, 10 points for demonstrating your use of UNIX plus 15 points each for the electronic and paper submissions, for a total of 200 points.


Adapted from Terry Harvey and Phill Conrad.