ECS 30A

Homework #3

Fall 2008

 

Due:          Monday, October 20. 

Written:     4:00pm in 2131 Kemper. 

Programs:  11:59pm using handin to cs30a, p2 directory.

Filenames: car.c, projectile.c, round.c earth.c, music.c, compass.c, gas.c, boiling.c

 

Written:   p. 140: 1, 2, 3, 4, 5.

                  p.202: 5, 6

 

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 following examples exactly.  The actual values calculated by the programs may vary slightly from the examples.  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 car.c.

 

#1. (p. 140)  Filename: car.c

 

[…@pc…]$ car.out

Please enter the principal, and interest> 10000 18

Please enter the number of payments> 36

Your loan of $10000.00 will have monthly payments of $361.52.

[…@pc…]$

 

#4 (p. 141)  Filename: projectile.c

 

[…@pc…]$ projectile.out

This program computes the duration of a projectile's flight,

and its height above the ground when it reaches the target.

The program will ask you for the angle of elevation (in radians),

the distance to the target (in feet), and the project velocity

(in feet per second).

 

Please enter the angle, distance, velocity> 0.3 11000 800

The projectile will take 14.393 seconds to reach the target.

The projectile will hit the target at a height of 70.6 feet.

[…@pc…]$

 

#5 ( p. 142)  Filename: round.c

     This can be done without calling any functions.

 

[…@pc…]$ round.out

Please enter a positive number> 32.4851

That rounded is 32.4900

[…@pc…]$

 

#10 (p. 143)   Filename earth.c

     celsius should be an int, and Fahrenheit should be a double.

 

[…@pc…]$ earth.out

Please enter a depth (in kilometers)> 25.9

The temperature at that depth is 278 Celsius, or 532.4 Fahrenheit.

[…@pc…]$

 

 

 

 

#1. p 203   Filename: music.c

     You should write this program without using an “else”.

     The tricky part of this program is the need to transform the money into ints to guarantee correct arithmetic of the displayed tax and discount with the total purchases.  You should convert the double purchases to an int containing the corresponding value in terms of cents.  Note that rounding errors cause 100 * 24.90 to become 2489 when storing in an int!  You will need to add 0.1 to overcome the rounding problem, e.g. 100 * 24.90 + 0.1 becomes 2490.

 

[…@pc…]$ music.out

Please enter your total purchases> 122.00

Are you a teacher (0 = No, 1 = Yes)> 0

 

Total purchases                   $122.00

Sales tax (5%)                     6.10

Total                           $128.10

[…@pc…]$ music.out

Please enter your total purchases> 122.00

Are you a teacher (0 = No, 1 = Yes)> 1

 

Total purchases                 $122.00

Teacher's discount (12%)          14.64

Discounted total                 107.36

Sales tax (5%)                     5.37

Total                           $112.73

[…@pc…]$

 

#3, p. 204  Filename: compass.c

     North is 0°, East is 90°, South is 180°, and West is 270°.

 

[…@pc…]$ compass.out

Please enter a heading in degrees (0-360.0)> 23.3

That is North 23.3 East.

[…@pc…]$ compass.out

Please enter a heading in degrees (0-360.0)> 110.0

That is South 70.0 East.

[…@pc…]$ compass.out

Please enter a heading in degrees (0-360.0)> 370

Your heading is outside the allowed values.

Please try again.

[…@pc…]$

 

#4. p. 204 Filename: gas.c

     You must use a switch statement.

 

[…@pc…]$ gas.out

Please enter the first letter of the cylinder's color> O

Contents is ammonia.

[…@pc…]$ gas.out

Please enter the first letter of the cylinder's color> y

Contents is hydrogen.

[…@pc…]$ gas.out

Please enter the first letter of the cylinder's color> B

Contents is carbon monoxide.

[…@pc…]$

 

 

 

 

#11. p.207 Filename: boiling.c

[…@pc…]$ boiling.out

Please enter the boiling point of the substance> 103

Substance is water.

[…@pc…]$ boiling.out

Please enter the boiling point of the substance> 1200 

Substance is copper.

[…@pc…]$ boiling.out

Please enter the boiling point of the substance> 2500    

Substance unknown.

[…@pc…]$