Subject: Computer Science
Teacher: Maruf Ahmed
Numeric functions and operators
Numeric data types in Pseudocode: INTEGER and REAL
DIV and MOD can only be used as operator such as:
DIV: finds the quotient when one number is divided by another.
Example: 10 DIV 3 evaluates to 3
MOD: finds the remainder when one number is divided by another.
Example: 10 MOD 3 evaluates to 1
Numeric functions in Pseudocode
INT(x : REAL) RETURNS INTEGER
returns the integer part of x
Example: INT(27.8415) returns 27
RAND(x : INTEGER) RETURNS REAL
returns a real number in the range 0 to x (not inclusive of x).
Example: RAND(87) could return 35.430729
Use of INT() and RAND() function together:
//Generate and display 10 random integers in the range 0 to 50 inclusive
FOR Count ← 1 TO 10
NewRandom ← INT(RAND(51)) //INT function needs to be used to make it a whole number
OUTPUT NewRandom
NEXT Count
//Generate and display 10 random integers in the range from 10 to 30 inclusive
FOR Count ← 1 TO 10
NewRandom ← INT(RAND(21)) + 10 // or NewRandom ← INT(RAND(21) + 10)
OUTPUT NewRandom
NEXT Count
N.B. Add the start value to the right of RAND() function. Suppose the random value needs to be generated
from 10 to 30. In that case add the start value 10 to the right of the RAND() function with a parameter value
21 for the function. The RAND() function will always generate a real number in the range 0 to x (not
inclusive of x) which in this case less than 21. The generated value needs to be converted into integer so
INT() function needs to be used.
##Python code
//Generate and display 10 random integers in the range from 10 to 20 inclusive
import random ##This library is required to be imported in Python to work with random number
for i in range(10):
NewRandom = int([Link]()*11)+10 ##[Link]() returns a float in the range from 0 to 1
##(not inclusive of 1)
Page 1 of 2
print(NewRandom)
Question 1: A program is required to simulate the generation of 100 random integers between 1 and 50
inclusive and store them in a one-dimensional array called NumberList.
After generating the numbers, the program should:
• Count and display how many times the number 25 appears
• Calculate and output the average of all numbers
• Output the smallest and largest numbers generated with the index number. The first generated
number should be stored as smallest and largest value. The other values should be checked with this
value to get the final smallest or highest value
• Write the pseudocode to perform this task. Declare all the identifiers before use.
Question 2: A program is required to simulate the results of a class of 30 students. Each student receives a
random grade from the array, GradeList: ["A", "B", "C", "D", "E", "F"].
Task:
• Store the grades in a one-dimensional array called Grades[1:30]
• Count how many students received each grade in CountGrade array
• Output the count for each grade
Page 2 of 2