Lab02, CISC105 Summer 2005
Directions
- Write a program for each of the following problems. If you wish, you
may start each program using a previous program as a base and then
modifying it, BUT you will learn more if you code each one from
scratch. Be sure to save every separate program. All programs must be
properly commented and indented (see
Assignment Standards on the class
website).
- Some programs below are associated with a question. Answer the
question using comments in your code.
- During your scripting of the programs, you will compile and run each
one. If a program does not compile, don't try to run it (because a.out
will not have been created).
- Name each program lab02.n.c, where n is the number in the list
below. For example, the name of the file for the first will be lab02.1.c
Practicing UNIX
Create a directory for this lab and then go into that directory to
create, compile, and execute your programs.
Programs
- (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.
- (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.
- (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 |
- (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)?)
- (15)
Write a program that prints the average
for students' grades entered as user input. (When should you stop
reading user input?)
- (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.)
- (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.
- (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.
- (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.
- (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.
- (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.