Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1,...

12
Homework Assignment #3 J. H. Wang Apr. 19, 2007

Transcript of Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1,...

Page 1: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

Homework Assignment #3

J. H. Wang

Apr. 19, 2007

Page 2: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

Homework Assignment #3

• Chap. 3– Exercise 3.1: 1, 3 (p.104)– Exercise 3.2: 1, Prog. 2 (p.111)– Exercise 3.3: 2 (p.114)– Exercise 3.5: Prog. 2 (p.136)– Programming Projects: Prog. 10 (p.143)

• Chap. 4– Exercise 4.2: 2, 4 (p.157)– Exercise 4.3: 2 (p.162)– Exercise 4.4: Prog. 2 (p.166)– Exercise 4.7: 3 (p.189)– Exercise 4.8: Prog. 1 (p.195)

• Deadline:– Two weeks

Page 3: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

Details

• Exercise 3.1: 1, 3 (p.104)– 1. Describe the problem inputs and outputs and write

the algorithm for a program that computes an employee’s gross salary given the hours worked and the hourly rate.

– 3. In computing gross salary, what changes should you make to extend the payroll algorithm in Self-Check Exercise 1 to include overtime hours to be paid at 1.5 times an employee’s normal hourly rate? Assume that overtime hours are entered separately.

Page 4: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

• Exercise 3.2: 1, Prog. 2 (p.111)– 1. Rewrite the following mathematical expressions using C functi

ons:• a.

• b.

• c.

• d.

– Prog. 2. Write a complete C program that prompts the user for the Cartesian coordinates of two points (x1, y1) and (x2, y2) and displays the distance between them computed using the formula:

Page 5: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

• Exercise 3.3: 2 (p.114)– 2. Draw the structure chart for the problem of

drawing the house shown in Fig.3.9.

Page 6: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

• Exercise 3.5: Prog. 2 (p.136)– Prog. 2. Write a function that computes the sp

eed (km/h) one must average to reach a certain destination by a designated time. You need to deal only with arrivals occurring later on the same day as the departure. Function inputs include departure and arrival times as integers on a 24-hour clock (8:30P.M. = 2030) and the distance to the destination in kilometers. Also write a driver program to test your function.

Page 7: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

• Programming Projects: Prog. 10 (p.143)– 10. Write a program to take a depth (in kilometers) ins

ide the earth as input data; compute and display the temperature at this depth in degrees Celsius and degrees Fahrenheit. The relevant formulas are

• Celsius = 10 (depth) + 20 – (Celsius temperature at depth in km)

• Fahrenheit = 1.8 (Celsius) + 32

Include two functions in your program. Function celsius_at_depth should compute and return the Celsius temperature at a depth measured in kilometers. Function fahrenheit should convert a Celsius temperature to Fahrenheit.

Page 8: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

• Exercise 4.2: 2, 4 (p.157)– 2. Evaluate each of the following expressions if a is 5,

b is 10, c is 15, and flag is 1. Which parts of these expressions are not evaluated due to short-circuit evaluation?a. c == a+b || !flagb. a != 7 && flag || c>=6c. !(b<=12) && a%2 ==0d. !(a>5 || c<a+b)

– 4. Complement each expression in Exercise 2. Use DeMorgan’s theorem if applicable.

Page 9: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

• Exercise 4.3: 2 (p.162)– 2. What value is assigned to x when y is 15.0?

a. x = 25.0; if (y != (x-10.0)) x = x-10.0; else x = x/2.0;

b. if (y<15.0) if (y>=0.0) x = 5*y; else

x = 2*y; else x = 3*y;c. if (y<15.0 && y>=0.0) x = 5*y; else x = 2*y;

Page 10: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

• Exercise 4.4: Prog. 2 (p.166)– 2. Write an interactive program that contains a

n if statement that may be used to compute the area of a square (area=side2) or a triangle (area=1/2*base*height) after prompting the user to type the first character of the figure name (S or T).

Page 11: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

• Exercise 4.7: 3 (p.189)– 3. Write as nested if statement for the decision diagrammed in

the accompanying flowchart. Use a multiple-alternative if for intermediate decisions where possible.

pH>7

pH<12pH is 7

pH>2 “Alkaline"“Very

alkaline"“Veryacidic"

“Acidic"

“Neutral"

true

truetrue

true

false

falsefalse

false

Page 12: Homework Assignment #3 J. H. Wang Apr. 19, 2007. Homework Assignment #3 Chap. 3 –Exercise 3.1: 1, 3 (p.104) –Exercise 3.2: 1, Prog. 2 (p.111) –Exercise.

• Exercise 4.8: Prog. 1 (p.195)– Write a switch statement that assigns to the variable lumens the

expected brightness of a standard light bulb whose wattage has been stored in watts. Use this table: (Assign -1 to lumens if the value of watts is not in the table.)

Watts Brightness (in Lumens)

15 125

25 215

40 500

60 880

75 1000

100 1675