ECS 30A

Homework #4

Fall 2008

 

Due: Wednesday, October 29.  Programs: 11:59pm using handin to cs30a p3 directory.

Filenames: display.c, gcd.c, efficiency.c, temperature.c

 

Programming: All programs should be able to compile with no warnings when compiled with the –Wall option.  You should put your name(s) in a comment on the first line of each file.  The prompts, and output format of each program must match the examples exactly.  To use functions from math.h, you must have –lm on your compile line (that is an “l” as in library) to link with the math library, e.g., gcc –Wall –lm gcd.c

 

 #2, page 250. Filename: display.c

      You must use a nested of for loops.

 

[…@pc…]$display.out

0

0 1

0 1 2

0 1 2 3

0 1 2 3 4

0 1 2 3

0 1 2

0 1

0

[…@pc…]$

 

#3, page 273. Filename: gcd.c

          If you “man abs”, you can determine which header file will provide the prototype for abs().

 

[…@pc…]$ gcd.out

Please enter two integers> 735 252

The greatest common denominator is 21.

[…@pc…]$ gcd.out

Please enter two integers> 60 20

The greatest common denominator is 20.

[…@pc…]$

 

#4, page 274. Filename: efficiency.c

 

[…@pc…]$ cat carpool.txt

4 75 11.0

2 50 9.0

5 95 15.5

2 60 4.5

4 35 4.3

6 70 20.7

0 0 0

[…@pc…]$ efficiency.out

Please enter the minimum efficiency> 25

[…@pc…]$ cat effic.txt

         CARPOOLS MEETING MINIMUM PASSENGER EFFICIENCY OF 25 PASSENGER KM / L

Passengers      Weekly Commute       Gasoline            Efficiency         Weekly

                (km)                 Consumption(L)      (pass km / L)      Subsidy($)

4               75                   11.0                27.3               24.00

5               95                   15.5                30.6               38.00

2               60                   4.5                 26.7               9.60

4               35                   4.3                 32.6               11.20

[…@pc…]$

 

 

#6, page 275  Filename: temperature.c

     The user will terminate their input by pressing Ctrl-N

 

[…@pc…]$ temperature.out

Please enter the temperatures (Ctrl-N to end).

55 62 68 74 59 45 41 58 60 67 65 78 82 88 91

92 90 93 87 80 78 79 72 68 61 59

There were 6 cold days, 14 pleasant days, and 6 hot days.

The average temperature was 71.2 degrees.

[…@pc…]$