Homework # 02 Answers: Mr. Mahmoud Moussa AS CS 9618
Homework # 02 Answers: Mr. Mahmoud Moussa AS CS 9618
Homework # 02 Answers: Mr. Mahmoud Moussa AS CS 9618
Homework # 02 Answers
Question 1
A teacher uses a paper-based system to store marks for a class test. The teacher requires a program
to assign grades based on these results. The program will output the grades together with the
average mark. Write a detailed description of the algorithm that will be needed. [6]
www.mahmoudmoussa.com 1
Mr. Mahmoud Moussa AS CS 9618
The student decides to modify the algorithm so that each element of the array will contain a unique
value. Describe the changes that the student needs to make to the algorithm. [3]
The following pseudocode is an attempt to define an algorithm that takes two numbers as input and
outputs the larger of the two numbers.
DECLARE A, B : INTEGER
INPUT A
INPUT B
IF A > B THEN
OUTPUT A
ELSE
OUTPUT B
ENDIF
www.mahmoudmoussa.com 2
Mr. Mahmoud Moussa AS CS 9618
Question 4
The following structured English describes an algorithm used to count the number of odd and even
digits in an input sequence.
1. Initialise variables OddCount and EvenCount to zero.
2. Prompt and input an integer.
3. If the integer is not in the range 0 to 9 then go to step 7.
4. If the integer is an even number then add 1 to EvenCount.
5. Otherwise add 1 to OddCount.
6. Repeat from step 2.
7. Output "Same" if there are the same number of odd and even integers.
8. Output "Odd" if there are more odd than even integers.
9. Output "Even" if there are more even than odd integers.
Draw a flowchart on the following page to represent the algorithm. [7]
www.mahmoudmoussa.com 3
Mr. Mahmoud Moussa AS CS 9618
www.mahmoudmoussa.com 4
Mr. Mahmoud Moussa AS CS 9618
Question 5
A global 1D array, ProdNum, of type INTEGER contains 5000 elements and is used to store
product numbers. An algorithm is needed to sort ProdNum into ascending order using a bubble sort
algorithm. Write the algorithm using pseudocode. [7]
DECLARE Temp : INTEGER
DECLARE NoSwaps : BOOLEAN
DECLARE Boundary, J : INTEGER
Boundary ← 4999
REPEAT
NoSwaps ← TRUE
FOR J ← 1 TO Boundary
IF ProdNum[J]> ProdNum[J+1] THEN
Temp ← ProdNum[J]
ProdNum[J] ← ProdNum[J+1]
ProdNum[J+1] ← Temp
NoSwaps ← FALSE
ENDIF
ENDFOR
Boundary ← Boundary - 1
UNTIL NoSwaps = TRUE
Question 6
An algorithm is needed to input a list of numbers representing test marks for a class of 30 students.
The algorithm will output the number of students who have a mark greater than 75. It will also
output the average mark for the class.
Document the algorithm using structured English. [8]
1. Set Total to 0
2. Set AGradeCount to 0
3. Input Mark
4. Add Mark to Total
5. If Mark > 75 then increment AGradeCount
6. Repeat from Step for 30 times
7. Output AGradeCount
8. Output Total / 30
www.mahmoudmoussa.com 5
Mr. Mahmoud Moussa AS CS 9618
Question 7
Mustafa has developed the following pseudocode to generate ten random integers in the range 1 to
100.
DECLARE Random : ARRAY [1:10] OF INTEGER
DECLARE Count, RNum : INTEGER
FOR Count ← 1 TO 10
Rnum INT(RAND(100)) + 1
Random[Count] ← RNum
ENDFOR
Rewrite the pseudocode so that there are no duplicated numbers in the list of random numbers.
DECLARE Random : ARRAY [1:10] OF INTEGER
DECLARE NextNum, Index, Rnum : INTEGER
DECLARE Exists : BOOLEAN
NextNum ← 1 // index position for the next random number
REPEAT
Rnum ← INT(RAND(100)) + 1 // from original question
Exists ← FALSE
FOR Index ← 1 to NextNum - 1 // search for Rnum
IF Random[Index] = Rnum THEN
Exists ← TRUE
ENDIF
ENDFOR
IF Exists = FALSE THEN
Random[NextNum] ← Rnum // store Rnum
NextNum ← NextNum + 1 // increment index
ENDIF
UNTIL NextNum > 10
www.mahmoudmoussa.com 6