Computing Excercise 2
Transcript of Computing Excercise 2
-
8/10/2019 Computing Excercise 2
1/8
Exercise 2.4
1. Define what is algorithm?Algorithm is a procedure for solving a problem in terms of the actions to be executed and the
order in which these actions are to be executed.
2. In what kind of form the algorithm can be written?Algorithm can be written in pseudo codes or flowchart.
3. List out the basic control structures and briefly explain each of it.i. Sequence: is a series of statements that execute one after another,ii. Selection (branch): is used to execute different statements depending on certain
conditions. Eg: IF condition THEN statement1 ELSE statement2,
iii. Repetition (loop): is used to repeat statements while certain conditions are met.
4. Briefly explain three type of error.
i. Syntax error: Errors in spelling and grammar (syntax).Caught by compiler.ii. Run-time error: Code does something illegal when it is run (hence runtime). Example,divide by zero.
iii. Logic error: Do not get error messages with logic errors. The only clue to the existence oflogic errors is the production of wrong solutions.
5. List out and briefly explain the common flowcharts symbol?
Symbol Name Discripsion
Terminal/Terminator The beginning or end of aprogram
Process Computation or datamanipulation. Indicates anytype of internal operation
Data Used for any Input / Output(I/O) operation.Indicates that the computer isto obtain dataor display output or results
Decision A program branch point
On-page reference/Connector
An entry to, or an exit from,another part of the flow chart
Flow lines Connecting the flowchartssymbols. Shows direction offlow.
-
8/10/2019 Computing Excercise 2
2/8
7. Write the analysis ( input, output and process/formula ) and design (pseudo code andflowchart) for problems below:
7.1. A program will read four integers from the keyboard, calculates their sum, and then printsthe numbers with the sum.
Input= Four integers from the keyboardOutput= The numbers with the sum.Process= n1+n2+n3+n4
Pseudo code
1. Start
2. Set sum = 0
3. Read four integers: num1, num2, num3 and num4
4. Calculate sum = num1 + num2 + num3 + num4
5. Print sum
6. End
Flowchart
Start
Sum = 0
Read four
integers:
num1, num2,
num3 and
Sum = num1 + num2 +num3 + num4
Print Sum
End
-
8/10/2019 Computing Excercise 2
3/8
7.2. Represent the processes/activities/sequences of sending a message using mobilephone.
Input= Message that we want to sentOutput= Message delivered or message sending failed.Process= Sending the message
Pseudo code
1. Start
2. Input text message, phone number of recipient
3. Send message
4. If sending successful
Print Message sent
5. Else
If sending failed
Print Message sending failed
6. End
Flowchart
Start
Input text
message,
phone numberof recipient
Send message
Doessending
successful?
Print
Message
sent
PrintMessage
sending failed
End
Yes
No
-
8/10/2019 Computing Excercise 2
4/8
7.3. Warong Abu provides a menu of nasi lemak for breakfast. The price of the items are:
Nasi Lemak + sambal + ikan bilik + timun RM1.50Option:Sambal sotong RM1.50Sambal udang RM1.00
Fried/ Boiled Egg RM0.60Each customer may order nasi lemak with any option. Calculate the price of each order.
Input= Option that was selectedOutput= Total priceProcess= Calculate the total price
Pseudo code
1. Start
2. Set Price=1.50
3. Read Options
4. If no option selected,
Print Price
5. Else, If Option is sambal sotong,
Calculate Price=Price+1.50
Print Price
6. Else, If Option is sambal udang,
Calculate Price=Price+1.00
Print Price
7. Else, If Option is fried/boiled egg,Calculate Price=Price+0.60
Print Price
8. End
-
8/10/2019 Computing Excercise 2
5/8
Flowchart
Start
Set Price=1.50
Read
Option
Is there
any option
selected?
PrintPrice
If sambal
sotong
selected
End
If sambal
udang
selected
If
fried/boiled
egg selected
Price=Price+1.50
Price=Price+1.00
Price=Price+0.60
Yes
Yes
Yes
Yes
No
No
No
-
8/10/2019 Computing Excercise 2
6/8
7.4.The program will print A for exam marks greater than or equal to 80, Print B for marks
in the range 65 to 79, Print C for marks in the range 50 - 64, Print D for marks in therange 40 to 49 and E for all other marks.
Input= Read markOutput= Print A or B or C or D or E
Process= If 100 MARKS 80, Print A
If 79 MARKS 65, Print B
If 64 MARKS 50, Print C
If 49 MARKS 40, Print D
If 39 MARKS 0, Print E
Pseudo code
1. Start
2. Read MARKS
4. If 100 MARKS 80
Print A
5. Else
If 79 MARKS 65
Print B
6. Else
If 64 MARKS 50
Print C
7. Else
If 49 MARKS 40
Print D
8. Else
If 39 MARKS 0
Print E
9. End
-
8/10/2019 Computing Excercise 2
7/8
7.5. The program will calculate the perimeter of a circle. The radius is 7.With the perimeter
formula (2*radius*PI), print the output of the perimeter for the circle. Value of PI is 3.142.
Input= set the radius of circle
Output= The perimeter for the circleProcess= 2*radius*PI
Pseudo code
1. Start
2. Set Perimeter=0, PI=3.142
3. Input radius=7
4. Calculate Perimeter=2*radius*PI
5. Print Perimeter
6. End
Flowchart
Start
Set Perimeter=0,
PI=3.142
Input
radius=7
Calculate
Perimeter=2*radius*PI
Print
Perimeter
End
-
8/10/2019 Computing Excercise 2
8/8
Flowchart
Start
Read
MARKS
If
100MARKS
80
If
79MARKS
65
If
64MARKS
50
If
49MARKS40
If
39MARKS0
Print
A
Print
D
Print E
Print C
Print B
End