Sie sind auf Seite 1von 3

Problem Solving with Computers Group Exercise Part A: Algorithm Development

1. Sequence Sally wants to buy a new car. She has saved up a certain amount of money for this purpose but needs to borrow the remaining money from a bank (the principal, p) for a certain number of years (the time, t) at a certain interest rate (the rate, r). The bank will charge Sally interest on the loan using the formula for simple interest: Interest = (p * r * t) / 100 The interest calculated is then added to the principal and Sally will have to repay the total amount of money in equal monthly installments over the period of the loan. You are required to write a program that calculates the total interest that will be charged on the loan and the monthly installment that Sally will have to pay. (a) What are the input/s and output/s of the program? (b) Work out a hand example with appropriate data. (c) Give a structured English algorithm to solve the problem. (d) Write a C program to solve the problem. (e) Test your program to ensure that it is working correctly. 2. Selection A program is required to print an appropriate message if three angles a, b, and c form either: - an equilateral triangle OR - an isosceles triangle OR - a scalene triangle (a) What are the inputs and outputs of the program? (b) Work out by hand the results that the program should generate from the following data: (i) a is 50, b is 80, c is 50 (ii) a is 56, b is 64, c is 60 (c) Give a structured English algorithm to solve the problem. (Assume that the values a, b and c do indeed represent one of the 3 triangles indicated.) (d) State ONE test that can be used to determine if the final program is working correctly.

(e)(i) (ii)

Determine how the algorithm in (c) above will behave with the following data: a is 50, b is 80, c is 100. How can the algorithm be modified to print an appropriate message to handle the case in (i) above?

(f) Write a C program to implement the algorithm in (c) above, incorporating the modifications stated in (ii) above. 3. Repetition A certain product is to sell for UnitPrice dollars. Write a program that reads values for UnitPrice and TotalNumber from the keyboard and then produces a table showing the total price of the product from 1 through TotalNumber units. (a) What are the input/s and output/s of the program? (b) Work out a hand example with appropriate data. (c) Give a structured English algorithm to solve the problem. (d) Write a C program to solve the problem. (e) Test your program to ensure that it is working correctly.

Part B: Choosing Good Problems for Algorithms


Find and discuss among your group members two suitable algorithms in each of the following categories: 1. Sequence (should contain only instructions to be followed in sequence) 2. Selection (should contain instructions involving selection as well as instructions to be followed in sequence) 3. Repetition (should contain instructions to be repeated as well as instructions to be followed in sequence; do not use selection statements in these problems). Specify your algorithms using the same approach given in Part (a). Your algorithms should be at a similar level of difficulty as the algorithms given in Part (a).

2. A program is required to print a message if the equation ax2 + bx + c has imaginary roots, equal roots, or distinct roots. If the roots are not imaginary, they are calculated and displayed after the message. (a) (b) What are the inputs and outputs of the program? Work out by hand the results that the program should generate from the following equations: (i) x2-2x-3 (ii) 3x2 + 4x + 2 Give a structured English algorithm to solve the problem. State ONE test that can be used to determine if the final program is working correctly. (i) Determine how the algorithm in 2(c) above will behave with the following data: - a and b are zero, c is non-zero. - a is zero, b and c are non-zero. (ii) How can the algorithm be modified to correctly handle EACH of the cases in 2(e)(i) above? (f) Write a C program to implement the algorithm in 2(c) above, incorporating the modifications stated in 2(e)(ii) above.

(c) (d) (e)

Das könnte Ihnen auch gefallen