Question is:
Write pseudocode for a program that calculates the service charge of a customer owes for writing a bad check.
The program accepts a cust开发者_Go百科omer's name, the date the check was written (year, month and day), the current date (year, month and day), and the amount of the check in dollars and cents. The program continues until an eof value is encountered.
The service charge is $20 plus 2 percent of the amount of the check, plus $5 for every month that has passed since the check was written. A check is one month late as soon as a new month starts-so a bad check written on September 30 is one month overdue on October 1.
A program is generally a series of steps. Can you break down the problem into a series of steps necessary to calculate your answer?
Hints:
- Every time the month changes, you owe another $5. Thus, "day" is irrelevant.
- Next year at the same month, 12 months are passed. The previous month, the number of elapsed months is
12 - 1
. - "2% more than" is equivalent to
* 1.02
- "Continues until EOF is reached" sounds like a loop.
Try to edit your question and make an honest attempt - no-one will solve your homework for you, but we will help you solve it.
In my humble experience, this kind of confusion is caused by trying to solve the problem and write the code at the same time.
Try solving the problem first.
Get a sheet of paper and draw a flowchart which shows the steps and decisions.
e.g. the last box might be:
EOF: Y = Stop, N = go back to "Read next line"
Pick 3 test examples e.g.
- In the current month
- In the last year
- Greater than a year
Work these examples through your flowchart and check that the result is correct. If not, amend the flowchart and rework the test examples.
When you are happy, "translate" the flowchart into English and you will have working pseudo code.
- Load the file
- Read and store check_date_month in a variable
- Read and store current_date_month in a variable
- Read and store check_amount in a variable
- Service_charge = 20 + 0.02*(check_amount) + [(current_month - check_date_month) + current_date_year - check_date_year]*5
- Read customer's name and show to the user something like:
"Customer's Name" Service charge: "$"Service_charge
The days in this case are not relevant because the charge increases every time the month changes, so, in the case we are in October and the check was done in September (10-9 = 1) we have to pay $5 more, but maybe we could be in different years, for example 2010 and 2009, that means that between October and September there are now (1 + 12 = 13) months, so now you have to pay $65. I expect this will help you to understand step 5.
精彩评论