ECS 30 B

Homework #4

Winter 2009

 

Due:  Feb 3, midnight.  Programs: 11:59pm using handin to cs30b p4 directory.

Filenames: stats.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 273. Filename: stats.c. First read in  a number, and then readin  that many numbers. Then calculate max, min,  standard deviation and average of this list. The book explains all the formulae. Do it all  with just one single honkin loop. This stats stuff is incredibly fun and useful. I use a program called "R" almost every day to analyze massive amounts of software engineering data to compute just these kinds of summaries of large data sets (I even get paid for this).You may assume all numbers are  < 9999 and  > -9999



[…@pc…]$./stats.out

How many numbers  >  6

Enter number 1 > 10
Enter number 2 > 11
Enter number 3 > 12
Enter number 4 > 13
Enter number 5 > 14
Enter number 6 > 15

**OK, Got it.
Mean = 12.50; Max = 15.0; Min= 10.0; Std Dev=1.71

[…@pc…]$

 

#3, page 273. Filename: gcd.c. Details explained in the book. This is also called Euclid's algorithm.  If you were to ask for something in computer science that's as old and beautiful as  the  Pyramids, or  the Taj Mahal,   or  these 14 lines  Euclid's algorithm would have to be it. It's used to this day in such areas as cryptography to keep your credit card number, etc., from prying eyes.    


 
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. See book for details.

 

[…@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 See book for details.

     The user will terminate their input by pressing Ctrl-D

 

[…@pc…]$ temperature.out

Please enter the temperatures (Ctrl-D 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…]$