Sie sind auf Seite 1von 10

Fall 2011 CISP 300

Assignment 2 Answer Key

Introduction:

Below, youll find the problem statement repeated from the instruction document,
my solution, and any additional notes I may have for you. In my solution, I talk
about how I broke down the problem to read the clues to help me find the solution.
These are the techniques you learned in the problem solving unit. Please note, this
is just one way to solve the problem there may be others.

The Problem:

The Austin Widget Manufacturing Company wants to reward the employees who
have increased their production of widgets this year over last years production.
These employees will receive a certificate of commendation and a bonus.

Write a program design using pseudocode that allows the user to input the
employees first name, employees last name, number of widgets produced this year,
and number of widgets produced last year. Calculate any bonus, if appropriate, and
display the employees name (first and last) and the calculated bonus.

For an employee, if this years production is greater than last years production,
calculate any bonus as follows:

If this years production is less than 1000, the bonus is $750


If this years production is 1001 to 2000, the bonus is $1000
If this years production is 2001 to 4000, the bonus is $1500
If this years production is 4001 to 6000, the bonus is $2000
If this years production is greater than 6000, the bonus is $3000

Write a complete program plan, including Introduction, Variable List/Data


Dictionary, and Main Detailed Plan (algorithm).

A NOTE ABOUT THE INTERFACE: You have the freedom to include what you
wish, but, as a minimum, use appropriate heading(s), input prompts and output
prompts. Assume input from the keyboard and output to the screen.

NOTE: do not ask the user to make any calculations. The program should make all
calculations. Write your design for a single customer. The design must include all
IO prompts and/or headings. You may choose to include an interface storyboard(s)

Assignment 2 Answer Key Page 1


OR include statements in the design defining headings and prompts. Again, assume
input from the keyboard and output to the screen.

It is required that you use the simple sequence and selection (if) structures. DO
NOT use the repetition structure, arrays, or modules.

MY Solution:

NOTE: again, this is just one way the problem could be solved. My solution is based
on the examples Ive given you in the lectures. In my design, any comments to help
you see the logic of my solution are highlighted in yellow.

Before I begin actually writing the design, I make some notes as I read the
problem. I define what needs to be input into the program and what needs to be
output. For example, as I read the problem statement, I determine that the Input
items are:

Employees first name


Employees last name
Number of widgets produced this year
Number of widgets produced last year

Next, I read the problem for what needs to be output. Again, as I read the
problem statement, I determine that the Output items are:

Employees name (first and last)


Bonus

Also, I look for any heading information as I read the problem statement and see
that it would be helpful to have report headings. Ive chosen to use the following.

Report Heading
Heading Line 1 = Austin Widget Manufacturing Company
Heading Line 2 = Employee Bonuses

Last, I focus on any processing requirements I may need to include:

Calculate the bonus

Notice there isnt any real logic and the above notes are NOT clear enough to be
considered the design of the program. Im simply getting my ducks in a row so I

Assignment 2 Answer Key Page 2


can be sure I dont forget anything. The above exercise also helps me be sure I
truly understand the problem.

The Design:

To write the introduction, I need to understand the problem. Simply copying the
problem from the assignment instructions doesnt insure that I understand the
problem. Also, a statement such as Write a design to input stuff, calculate results
and output those results is so ambiguous its of no benefit whatsoever.

INTRODUCTION: Design a program to calculate any employee bonuses based on


widgets produced both this year and last year. Input the employees first name,
employees last name, number of widgets produced this year and the number of
widgets produced last year. If this years production is greater than last years
production, determine the following bonuses. If this years production is:
Less than 1000, the bonus is 750
1001 to 2000, the bonus is 1000
2001 to 4000, the bonus is 1500
4001 to 6000, the bonus is 2000
Greater than 6000, the bonus is 3000

Output the employees name (first and last) and the bonus.

My exercise above has given me enough information to create my variable list. I


need to create variables which are easy to use (I plan to use them in my detail plan),
not too long, and are representative of what the data is that theyll contain. I must
create this list because Ill need the programmer to allocate memory for the
program and they should know EXACTLY what allocations to make. I also need to
include what TYPE of data the variables will contain since the computer handles
different data types differently. Please note that I went one step further than
the book and divided my numeric data into either integer or floating point data.

VARIABLE LIST:

Variable Name Data Type


First string
Last string
This integer
Last integer
Bonus integer
heading1= Austin Widget Manufacturing Company
heading2 = Employee Bonuses

Assignment 2 Answer Key Page 3


NOTE: The heading values can be constants and do not HAVE to have a data type
since Ive initialized their values in the variable list.

As you read through the following detail plan, please notice the indentation and the
EXPLICIT beginning and ending of the structures. This is done to make the plan as
easy to read as possible. Remember, your goal here is to be as clear as you possibly
can. DO NOT leave any room for assumption.

NOTE AGAIN: the DETAIL PLAN is the main start/stop algorithm.

DETAIL PLAN (without storyboards this means Ive included screen prompts in
my detail plan):

Start
Declare variables NOTE: you can put the variable list under this
statement, if you wish. YOU DO NOT HAVE TO
DO BOTH.

Write heading1
Write heading2
Write blank line
Write Enter the Employees First Name:
Read First
Write Enter the Employees Last Name:
Read Last
Write Enter the number of widgets produced this year:
Read This
Write Enter the number of widgets produced last year:
Read Last

NOTE: I put all of the input statements in one place for claritys sake. Also
note that the prompts are listed in a write statement and separate from
the read statements the reads represent statements to the
programmer to accept data from some outside source to be used later in
the program.

Below are my if (selection) statements.


Note the end if thats different than the book, but lets give the
programmer the information that the selection statement is finished.

Bonus = 0 NOTE: set the bonus variable to 0

Assignment 2 Answer Key Page 4


If This > Last then
If This < 1000 then
Bonus = 750
Else
If This < 2001 then
Bonus = 1000
Else
If This < 4001 then
Bonus = 1500
Else
If This < 6001 then
Bonus = 2000
Else
Bonus = 3000
End if
End if
End if
End if
End if

I used a nested if statement because I could eliminate the different levels


of widgets produced. The biggest problem is that 1000 widgets produced
isnt included in the conditional logic. Based on the above decision
statement, anyone producing 1000 widgets this year, will earn a bonus of
3000. If eliminating 1000 widgets was truly the purpose, then, technically,
the last bonus = 3000 should also have a test for This >= 6000. If 1000
widgets produced should be included, then the first test (This < 1000) should
really be (This <= 1000).

Write blank line


Write Employee Name is: , Last, , , First
Write The Bonus is: , Bonus
Stop

Notice the Stop statement. This is so the programmer will know that Ive ended
the program design without any doubt. Next, Ive written the storyboard(s) and
have redone the detail plan to remove the prompts. Either way you choose to do
this, is fine by me.

Assignment 2 Answer Key Page 5


STORYBOARD(S):

Storyboard:

Austin Widget Manufacturing Company


Employee Bonuses

Enter the Employees First Name: XXXXXXXXXXX


Enter the Employees Last Name: XXXXXXXXXXXX
Enter the number of widgets produced this year: 99999999
Enter the number of widgets produced last year: 99999999

Employee Name is: XXXXXXXXXXX, XXXXXXXXXXX


The Bonus is: 99999

DETAIL (with storyboards):

Start
Declare variables NOTE: you can put the variable list under this
statement, if you wish. YOU DO NOT HAVE TO
DO BOTH.

Read First
Read Last
Read This
Read Last

Bonus = 0 NOTE: set the bonus variable to 0


If This > Last then
If This < 1000 then
Bonus = 750
Else
If This < 2001 then
Bonus = 1000

Assignment 2 Answer Key Page 6


Else
If This < 4001 then
Bonus = 1500
Else
If This < 6001 then
Bonus = 2000
Else
Bonus = 3000
End if
End if
End if
End if
End if

Write Last, , , First


Write Bonus
Stop

Notes to students:

The above design makes up the full requirements listed in the instructions and is
presented in the requested order. The requirements include an introduction, a
variable list (also called a data dictionary), and a main start/stop algorithm. The
examples in the book show you that you can include the variable list in the main
algorithm indented under the declare variables statement. I will accept that as
well. You dont need to do both.

The book also DOES NOT include the declare variables statement. I think using
such a statement is clearer and gives the reader an understanding of the variable
declaration statements at the beginning of the designs presented in the book.

This design utilizes both the simple sequence and selection structures and they are
written using structured programming. In fact, the main start/stop algorithm is
also structured (single entry, single exit, using combinations of three structures).

When we talk about program flow or logical flow, we mean the presentation of
commands (or statements) one after the other in a top-down approach. That is the
way the computer will process this program once its written in a programming
language and present to the computer. The computer will read and process the
statements IN THE ORDER GIVEN IN THE DESIGN.

Assignment 2 Answer Key Page 7


As programmers youll want to remember that and think about when you want to list
your statements in your design.

Do you also see that this algorithm is for a SINGLE employee bonus? If I want to
process multiple employee bonuses, then I have to add a loop (THATS RIGHT, the
IF statement isnt a loop). Where to add that loop is really something to think
about.

With all loops, regardless of the type, you have to have to take care of three
situations:

1. you have to have proper control


2. before the loop starts, you have to set an initial control value
3. inside the loop body, you have to modify the control value

To add a loop to this algorithm, you have to think about how youre going to control
it. Since the input is coming from the keyboard, we cant control it with an EOF-
controlled loop, so well need to think of another way. How about asking our user a
question, such as Do you wish to continue? This way, we can use their answer to
control the loop.

First, we need to add a variable to the variable list; a variable to use to control (the
relational expression) the loop:

VARIABLE LIST:

Variable Name Data Type


First string
Last string
This integer
Last integer
Bonus integer
heading1= Austin Widget Manufacturing Company
heading2 = Employee Bonuses
ans character

Before we continue, lets talk about the input. The variables First, Last, This and
Last represent the input for a complete employee. We can call that a data set or
record. When we think about a loop, we want to process a complete employee bonus
before we repeat the next employee bonus. That means, in this case, we want to
input the data set, calculate the bonus and display the output BEFORE we read in
the next data set or record.

Assignment 2 Answer Key Page 8


Next, we need to think about what should be outside the loop and what should be
inside the loop. Well, we have input and output processes and the headings. We
need to test each input record before output, so it looks to me like we need to
include almost everything in the loop except declare variables and start/stop???
Remember, whatever is inside the loop, will be repeated each time the loop repeats.

Heres the algorithm with the loop (I used the storyboard version since its the
simplest to read). I highlighted the required loop components:

Start
Declare variables

Write Do you wish to continue? (Y/N) NOTE: Step 2, set initial control
Read ans value

While ans = Y OR ans = y loop start with proper control,


requires step 2 so theres some-
thing to test.
Read First
Read Last
Read This
Read Last

Bonus = 0 NOTE: set the bonus variable to 0


If This > Last then
If This < 1000 then
Bonus = 750
Else
If This < 2001 then
Bonus = 1000
Else
If This < 4001 then
Bonus = 1500
Else
If This < 6001 then
Bonus = 2000
Else
Bonus = 3000
End if
End if
End if

Assignment 2 Answer Key Page 9


End if
End if

Write Last, , , First


Write Bonus

Write Do you wish to continue? (Y/N) step 3, change


Read ans control value
End loop (end of loop structure all statements between while and end loop
make up the loop body and are repeated each time the loop
repeats)
Stop

REMINDER: you want to include all statements inside the loop that you want
repeated. Do you recognize the order of events in the loop body?

Get the employee information


Determine bonus (if statements)
Display the employee information

Because the loop is properly controlled, these three events will be executed in the
above order EACH TIME THE LOOP REPEATS.

Do you also recognize what keeps the loop repeating? Thats right, the users ans.
If its upper case Y or lower case y, then the loop keeps repeating, allowing another
employees information to be processed. If the users ans is ANY OTHER VALUE
THAN an upper or lower case y, the loop will STOP and program control will
continue to the next statement AFTER the end loop (in this case Stop).

ONE OTHER CONSIDERATION: if youre processing multiple records (employees),


then you might want to reconsider the output. I didnt include the report titles
because theyre shown on the storyboard. What do you think? Where should the
report titles be listed? What will happen if you include them INSIDE the loop?

Assignment 2 Answer Key Page 10

Das könnte Ihnen auch gefallen