CS140 -- Lab 1

In this lab, you will be required to write three separate programs. Your TA's will tell you in lab how to submit your programs for grading.


Examples

In the directory /home/plank/cs140/Examples/Lab1, you will find working executables for each of these programs. Test them out to see how they work. If you have any questions about how your programs should work, first look at these, and do what they do.

Testing for the end of a file

Remember, scanf() returns 1 when it makes a match. If it doesn't return 1, then it didn't make a match. When you read words using scanf("%s", ...), then the only time it does not return 1 is when it has reached the end of standard input.

Program 1: Averaging a bunch of numbers

Write a program called avg.c which prints out the average of all numbers on standard input. Note that a line of input can contain any number of numbers -- zero, one, fifty, no matter. Have your program print out 0.000000 if no numbers are entered. Your program should quit reading when it sees no more numbers on standard input (i.e. when scanf() does not return 1).

You should use double as your data type, and read in numbers using scanf("%lf", ...).

Print the average using printf("%lf", ...). As always, when in doubt, check your program against the one in /home/plank/cs140/Examples/Lab1.


Program 2: Capitalization

Write a program called cap.c which prints out each word of standard input on its own line. If the word begins with a lower case letter, then capitalize that letter. Otherwise, just print out the word as it is on standard input. You may assume that words are less than 200 characters.

Program 3: ASCII Art - Printing a simple upper triangular matrix

Write a program called ut.c which reads an integer from standard input (with no prompt). Let that integer be w. Your program is going to print out w lines of output. Each line will have w characters, which are either '0' or '1'. The first line will have all 1's. The second line will have one '0' followed by (w-1) ones. The third will have two zeroes followed by (w-2) ones. And so on. The last line will have (w-1) zeroes followed by one one.

If the number entered is not an integer, or is a non-positive number, simply have the program exit.

Here are some examples:

UNIX> /home/plank/cs140/Examples/Lab1/ut
6
111111
011111
001111
000111
000011
000001
UNIX> /home/plank/cs140/Examples/Lab1/ut
3
111
011
001
UNIX> /home/plank/cs140/Examples/Lab1/ut
1
1
UNIX> /home/plank/cs140/Examples/Lab1/ut
Fred
UNIX>