Sunteți pe pagina 1din 4

2.

CASE STUDY

Problem
Many programs need input from users. Based on this input, the
program will display an appropriate message. This message will give
the users an indication about the type or choice of input needed by the
program.
One technique of interaction between a program and its user is using
the menu. The menu is a list of items that can be selected by the user.
Once the menu is displayed, the program will wait for the user to input
his or her choice. After the user has chosen the item, the program will
continue processing based on that item.
In this section, we will develop a program that will do these
conversions:

Convert time from minutes to hours;

Convert distance from feet to metres; and

Convert temperature from Fahrenheit to Celsius.

The program will display a menu to get the type of conversion required.
Once that is done, it will ask the user for the input to be converted, do
the calculation and then display the result. After that, the program will
display the menu again and the process will continue until the user
decides to stop the program.

Analysis
The following formula is used to do the conversions:
1 hour
=
1 feet
=
1 Celsius =

60 minutes
0.3048 metres
(5/9) * (Fahrenheit 32)

Design
The menu to be displayed by the program is based on the selection
listed in Table 2.1:
Table 2.1: Menu
Choice Code

Conversion Type

Minutes to hours

Feet to metres

Fahrenheit to Celsius

Program ends

Users choose the type of conversion by entering the corresponding


code. If the user enters a 0, the program ends. If the user enters any
value other than the above, it will be ignored. It means, the program will
re-display the menu and the user has to choose again.
Table 2.2. shows all the variables to be used in the pseudo-code.
Table 2.2: Variables
Variable

Explanation

choice

Users choice from the menu

minutes

Value of minutes input by the user

hours

Keeps the converted value from minutes to hours

feet

Value of feet input by the user

metres

Keeps the converted value from feet to metres

fahrenheit

Value of Fahrenheit input by the user

celsius

Keeps converted value from Fahrenheit to Celsius

The following is the initial pseudo-code for our problem:


1.0 Start
2.0 Do
2.1 Display the menu
2.2
Input the choice from the user
2.2.1
IF (1 <= choice and choice <= 3)
Start_IF
Do the conversion and display the result
End_IF
3.0 Until (choice NOT 0)
4.0 End
Step 2.2.1 involves calculation that will do the conversion and then
display the results. The calculation that is carried out depends on the
type of choice specified by the user in Step 2.2. Thus, Step 2.2.1 need
to be refined further to reflect the users choice. The following is the
refined and complete pseudo-code after refining Step 2.2.1 above:
1.0 Start
2.0 Do
2.1 Display the menu
2.2
Input the choice from the user
2.2.1a IF (choice==1)
Start_IF
Input minutes from the user
Calculate hours
Display hours and minutes
End_IF
2.2.1b IF (choice==2)
Start_IF
Input feet from the user
Refined steps
Calculate metres
Display feet and metres
End_IF
2.2.1c IF (choice==3)

Start_IF
Input fahrenheit from the user
Calculate celsius
Display fahrenheit and celsius
End_IF
3.0
Until (choice NOT 0)
4.0 End

The flow chart for the above pseudo-code is given below in Figure
2.6:
Start

Display menu

Input choice

choice
==1

Yes

Input
minutes

Calculate hours

Display minutes
& hours

No

choice
==2

Yes
Input feet

Calculate metres

Display feet &


metres

Calculate celsius

Display fheit
& celsius

No

choice
==3

Yes

Input
fheit

No

choice
==0

No

Yes

End

Figure 2.6: Flow chart for the pseudo-code

S-ar putea să vă placă și