ECS 30B

Homework #5

Winter 2009

 

Due: Monday, Feb 9th.
 Programs: 11:59pm using handin to cs30a p5 directory.

Filenames: square_root.c, smud.c, calculator.c, exponents.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

 

 #7. page 326. Filename: square_root.c

 

[…@pc…]$square_root.out

The square root of 4     is approximately 2.0000

The square root of 120.5 is approximately 10.9772

The square root of 88    is approximately 9.3808

The square root of 36.01 is approximately 6.0008

The square root of 10000 is approximately 100.0000

The square root of 0.25  is approximately 0.5000

[…@pc…]$


#8, page 326. Filename smud.c. Read the inputs (Costumer Number, KWHr used) in pairs, one pair per line, from
standard input, and then terminate when EOF is reached

[…@pc…]$smud.out
Enter customer number, usage pair, one pair per line (EOF to exit)>
100 300
101 600
102 310
***THANK YOU, GOT IT.

Customer Number                    KWHr Used              Charge
    100                               300                  27.00
    101                               600                  51.00
    102                               310                  27.80

****End of Run

 

#10, page 327. Filename: calculator.c

 

[…@pc…]$ calculator.out

Repeatedly enter operator and operand (q 0 to end)

+ 5.0

result so far is 5.00

^ 2

result so far is 25.00

/ 2.0

result so far is 12.50

q 0

final result is 12.50

 

[…@pc…]$ calculator.out

Repeatedly enter operator and operand (q 0 to end)

+ 2.0

result so far is 2.00

* 3.0

result so far is 6.00

# 2

Invalid operation: #

Final result is 6.00

[…@pc…]$


#11, page 328 Filename: exponents. c  (with a slight difference: do this for any given base (not just 2). Please tabbing for the header and

for each line, exactly as below:  (we'll shortly place a exponents.out executable for your output comparison)



[…@pc…]$ ./a.out
How many do you want > 6
What base would you like > 5
Power of 2         Fraction         Decimal Value
-1              1/5              0.200000
-2              1/25              0.040000
-3              1/125              0.008000
-4              1/625              0.001600
-5              1/3125              0.000320
-6              1/15625              0.000064