Homework # 02 Answers: Mr. Mahmoud Moussa AS CS 9618

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

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]

 Reference variables for Count of students and Total marks


 Loop through all students (Count)
 Input individual mark (in loop)
 Compare mark with threshold / boundary values to determine grade (in loop)
 Output the grade for a student (in loop)
 Maintain a Total (and Count if required) (in loop)
 Calculate average by dividing Total by Count and Output (after loop)
Question 2

A student is learning about arrays.


She wants to write a program to:

 declare a 1D array RNum of 100 elements of type INTEGER


 assign each element a random value in the range 1 to 200 inclusive
 hint use RANDOM(1,200) To generate a number between 1 to 200 inclusive
 count and output how many numbers generated were between 66 and 173 inclusive.
Write pseudocode to represent the algorithm. [6]
DECLARE RNum : ARRAY[1:100] OF INTEGER
DECLARE Index, Count : INTEGER
Count ← 0
FOR Index ← 1 TO 100
RNum[Index] ← RANDOM(1,200)
IF RNum[Index] >= 66 AND RNum[Index] <= 173 THEN
Count ← Count + 1
ENDIF
NEXT Index
OUTPUT Count

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]

 Initialise the array to a rogue value (to indicate 'unassigned' element)


 Add a conditional loop to:
 Generate and store a random number (in the correct range)
 Check the stored number against values already in the array
 If the stored number is found then generate another random value
 Otherwise add it to the array (and exit loop)
Question 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

The algorithm needs to be amended to include the following changes:


 Input three values, ensuring that each value input is unique.
 Output the average.
 Output the largest value.
Write the pseudocode for the amended algorithm [6]
DECLARE A, B, C : INTEGER
DECLARE Average : REAL
INPUT A
REPEAT
INPUT B
UNTIL B <> A
REPEAT
INPUT C
UNTIL C <> A AND C <> B
Average ← (A + B + C) / 3
OUTPUT Average
IF A > B AND A > C THEN
OUTPUT A
ELSE
IF B > A AND B > C THEN
OUTPUT B
ELSE
OUTPUT C
ENDIF
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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy