• Home
  • Textbooks
  • AS and A Level OCR Computer Science
  • Data structures

AS and A Level OCR Computer Science

PM Heathcote, RSU Heathcote, PG Online

Chapter 7

Data structures - all with Video Answers

Educators


Section 33

Arrays, tuples and records

03:44

Problem 1

Referring to the BirdWatch program given earlier in this chapter:
(a) Explain why the for... next loop repeated below is not the most efficient type of loop in this situation.
for count $=0$ to 7
if bird == birdName [count] then
$\quad$ birdFound = True
$\quad$ birdsobserved = input ("Enter number of birds observed: ")
$\quad$ birdCount[count] = birdCount[count] + birdsobserved
endif
next count
for count $=0$ to 7
if bird == birdName[count] then
birdFound $=$ True
birdsobserved = input ("Enter number of birds observed: ")
birdCount [count $]$ = birdCount $[$ count $]+$ birdsobserved
endif
next count
(b) Rewrite the algorithm using a different type of loop.

Mayank Tripathi
Mayank Tripathi
Numerade Educator
05:39

Problem 2

The birth weights in grams of 100 babies, which vary between 1500 to 4000 grams, are held in an array weight.

Write pseudocode for an algorithm which calculates the average birth weight, and then prints out the number of babies who are more than 500 grams below the average weight, together with the average weight of these.

Gretchen Laviolette
Gretchen Laviolette
Numerade Educator
01:54

Problem 3

The marks for 3 assignments, each marked out of 10 , for a class of 5 students are to be input into a two-dimensional array mark so that mark $[3,1]$, for example, holds the second mark achieved by the 4th student. Any missing assignments are given a mark of zero.
Draw a table representing this array, and fill it with test data.
Write a pseudocode algorithm which allows the user to enter the marks for the class. Calculate the average mark for each student, and the class average.

Alexander Cheng
Alexander Cheng
Numerade Educator
07:10

Problem 4

In a certain game, treasure is hidden in a $10 \times 10$ grid. The grid coordinates are given by grid [row, col] where grid [0,0] represents the top left hand corner and grid[ [9, 9] the bottom right corner. The grid coordinates of the treasure are signified by a 1 at $g r i d[r o w, \operatorname{col}]$. All other grid elements are filled with zeros.
What is the purpose of the following pseudocode algorithm?
for row $=0$ to 9
for col $=0$ to 9
$\quad$ if grid[row, col] == 1 then
print("row ", row, "column ", col)
endif
next col
next row
for row $=0$ to 9
for col = 0 to 9
if grid[row, $c o l]==1$ then
print("row ", row," column ", col)
endif
next col
next row
Write pseudocode statements to initialise the grid and "hide the treasure" at a random location inside the grid.

Morgan Cheatham
Morgan Cheatham
Numerade Educator