Vous êtes sur la page 1sur 4

Sample Problems and their Solutions

PROBLEM 1 (Related to Sequencing)


Write down an algorithm that accepts temperature in Farhenheit and displays its equivalent temperature in
Centigrade.

SOLUTION
Following steps are required:

Step 1: Identify and name inputs and outputs

Input: Temperature in Farhenheit


Output: Temperature in Centigrade

Step 2: Problem Analysis and Design

We will ask the user to enter temperature in Farhenheit. User will provide one value in Farhenheit (which
may be a floating point value). To convert this temperature into Centigrade, first subtract 32 from it and
then divide it by 32. Once the conversion is done, display the temperature on the output screen
mentioning that this is the equivalent temperature in Centigrade.

Step 3: Variables Required

No variables other than the input and output are required. Here are the variables:

Variable Name Data Type Purpose


TempF floating point Input variable to store entered temperature (in ˚F)
TempC floating point Output variable to store output temperature (in ˚C)

Step 4: Formulas required (if any)

TempC = (TempF – 32) / 1.8

Step 5: MAIN ALGORITHM

Prompt the user for input


Read Input
Generate the output (use the formula of step 4)
Write the Output

Step 6: Refine the algorithm

Write “Enter the temperature in Farhenheit”


Read TempF
TempC = (TempF – 32) / 1.8
Write “Equivalent temperature in Centigrade is”
Write TempC

Flowchart: I assume that drawing flowchart of the above solution is not a difficult task now.
PROBLEM 2 (Related to Selection)
Write down an algorithm that reads two integers and prints either “MULTIPLE” or “NOT MULTIPLE”
depending upon whether one of the integers is multiple of other or not.

SOLUTION
Following steps are required:

Step 1: Identify and name inputs and outputs


Input: two integer numbers
Output: a message

Step 2: Problem Analysis and Design


We will ask the user to enter two integers one by one. Then first one of the numbers will be divided by the
other and remainder will be checked. If the remainder is zero, it means that first number is multiple of the
second. If the remainder is not zero then the other number will be divided by first number. If the
remainder is zero, it means that second number is integer multiple of second. If the remainder is still
nonzero, it means that none of the two integers is integer multiple of the other. Output message will be
generated accordingly.

Step 3: Variables Required


One variable R is required other than the input variables. Here are the variables:
Variable Name Data Type Purpose
N1 integer First Input variable
N2 integer Second Input variable
R integer Variable to store the remainder after division operation

Step 4: Formulas required (if any)


No formula is required. We must only know about modulus operator which is the remainder when one
integer number is divided by other integer number. e.g., 18 mod 7 is 4 (as the remainder of 18 ÷ 7 is 4).

Step 5: MAIN ALGORITHM


Prompt and read the values of the two integers
Divide 1st number by 2nd and check the remainder
If remainder is not zero, divide 2nd number by 1st and check the remainder
If remainder is not zero in both cases, display “NOT MULTIPLE”
Otherwise display “MULTIPLE”

Step 6: Refine the algorithm


Write “Enter the 1st number”
Read N1
Write “Enter the 2nd number”
Read N2
R = N2/N1
If (R ≠ 0)
R = N1/N2
If (R ≠ 0)
Write “NOT MULTIPLE”
Endif
Else
Write “MULTIPLE”
Endif
Flowchart:
PROBLEM: Write pseudo code and draw flow chart of an algorithm that prompts the user to enter the value of a number n
and then asks the user to enter the values of these n integers one by one and finally outputs them in sorted (descending)
order. (use of subscript will be useful here).

Vous aimerez peut-être aussi