So ... You Want to be a Programmer by Ron English

So ... You Want to be a Programmer by Ron English

Author:Ron English [English, Ron]
Language: eng
Format: azw3
Publisher: Internethics
Published: 2002-08-24T16:00:00+00:00


After we test our pseudo-code to ensure that it a) matches our flowchart, and b) works, then we write the code. Bank Machine

The bank teller problem is a little more challenging, because the possible inputs and outputs vary. Keeping in mind that the solution has to be expandable, the easy solutions are not necessarily the best.

Input Has part pennies Amount There are three places to the right of the decimal

Not enough money in

account Amount is greater than the balance Not multiple of $20.00 OK The amount is not a

multiple of $20.00

Amount is less than the balance. Subtract the

amount from the balance, and give the customer the amount.

Save the balance

For example, we can agree that partial pennies are not acceptable, but what about multiples of $20.00? Is it reasonable to assume that withdrawals might be for bill payment, or interact payments? If so, then can these withdrawals be odd amounts? We can’t just lump all the invalid amounts together, as this would make the solution hard to maintain. We need to ask each question separately. Even the order in which the questions are posed can be the difference between a good program and a not-so-good one. In this example, if our first question is about multiples of $20, then if we add interact payments our code will have to be rewritten. Ask the questions which are crucial first. In this case, having part pennies means the amount is not a valid dollar amount.

Our flowchart will look something like this: Bank ATM Machine

Again, we need to test our flowchart. Does it work? Our pseudo-code is derived form the flowchart.

start

get AMOUNT

get BALANCE

if AMOUNT three decimal places

Error Message - invalid number else

if AMOUNT > BALANCE

error message - NSF

else

if AMOUNT not multiple of $20 Error message - Not $20 else

BALANCE <= BALANCE-AMOUNT Print BALANCE, AMOUNT end if

end if

end if

end

Again, we test. Then we get to code.

Coding Coding is not a major theme of this book. In the first example here, the conversion program, I'll present code in different languages. This is more for comparison of different languages than to teach you how to code. I will leave the bank teller program for you to code in the language of your choice.

In all cases, I assume the input is an integer in the range of 0 to 18,000.

Pascal

Program Conv(input, output); var miles : integer; answer : integer;

begin readln (miles);

if miles >= 0 then

if miles <= 18000 begin

end

else

answer := 1.8 *invalue; writeln (answer); writeln ("Error, too big"); else

writeln ("Error, negative");

end.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.