Reading values into a 2D array with INPUT in nested loops

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Reading values into a 2D array with INPUT in nested loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement reads a typed value into one cell of a 2D array called Marks?
A INPUT Marks[Student, Test]
B INPUT Marks
2. Marks[Student, Test] is read inside nested loops with Student as the OUTER loop and Test as the INNER loop. Which order are the cells filled in?
A One whole column at a time: Test 1 for every student, then Test 2 for every student
B One whole row at a time: Student 1 tests 1 to 3, then Student 2 tests 1 to 3
3. Which loop structure correctly reads every cell of Marks[1:4, 1:3] (4 students, 3 tests)?
A FOR Student ← 1 TO 4 FOR Test ← 1 TO 3 INPUT Marks[Student, Test] NEXT Test NEXT Student
B FOR Student ← 1 TO 4 INPUT Marks[Student, Test] NEXT Student
4. Which OUTPUT prompt, placed before INPUT in the inner loop, tells the user which cell is being entered?
A OUTPUT "Enter a mark"
B OUTPUT "Enter mark for student ", Student, " test ", Test
5. In which order should these lines be placed inside the inner loop?
A OUTPUT the prompt first, then INPUT Marks[Student, Test]
B INPUT Marks[Student, Test] first, then OUTPUT the prompt

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1INPUT Marks[Student, Test] AOUTPUT "Enter mark for student ", Student, " test ", Test
2FOR Test ← 1 TO 3\n INPUT Marks[Student, Test]\nNEXT Test BFOR Test ← 1 TO 3\n FOR Student ← 1 TO 30\n INPUT Marks[Student, Test]
3A prompt can show which row and column is being entered, so the user knows which mark to type. CReads a typed value into one cell of the 2D array, chosen by the current row (Student) and column (Test).
4To enter values column by column instead of row by row, swap the loops so the outer loop controls the test (column). DThere is no inner loop for Test, so Test never changes. Only one column is filled for each student, and the other columns stay empty.
5FOR Student ← 1 TO 30\n INPUT Marks[Student, Test]\nNEXT Student EThe indexes are in the wrong order. Marks[Test, Student] treats Test as the row and Student as the column, which does not match the Marks[Student, Test] layout and can go out of range.
6FOR Student ← 1 TO 30\n FOR Test ← 1 TO 3\n INPUT Marks[Test, Student] FThe inner loop reads three values into one row, one for each test, before the outer loop moves to the next student.

Definitions

INPUT Marks[Student, Test]

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
FOR Student ← 1 TO 3 FOR Test ← 1 TO 4 _____ Marks[Student, Test]
2.
FOR Student ← 1 TO 3 FOR Test ← 1 TO 4 INPUT Marks[Student, ____]
3.
FOR Test ← 1 TO 4 OUTPUT "Row ", Student, " column ", ____ INPUT Marks[Student, Test]
4.
FOR Test ← 1 TO 4 FOR _______ ← 1 TO 3 INPUT Marks[Student, Test]
5.
FOR Student ← 1 TO 3 FOR Test ← 1 __ 4 INPUT Marks[Student, Test]

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A FOR Student = 1 TO 3
B FOR Test ← 1 TO 2
C INPUT Marks[Student, Test]
D NEXT Test
E NEXT Student
Question 2
A FOR Student ← 1 TO 3
B FOR Test ← 1 TO 2
C INPUT Marks[Student]
D NEXT Test
E NEXT Student
Question 3
A FOR Student ← 3 TO 1
B FOR Test ← 1 TO 2
C INPUT Marks[Student, Test]
D NEXT Test
E NEXT Student
Question 4
A FOR Row ← 1 TO 3
B FOR Row ← 1 TO 4
C INPUT Grid[Row, Col]
D NEXT Col
E NEXT Row
Question 5
A FOR Student ← 1 TO 3
B FOR Test ← 1 TO 2
C INPUT Marks[Student, Test]
D NEXT Student
E NEXT Student

Answer Key

Reading values into a 2D array with INPUT in nested loops
Section A: Quick Questions
Q1
A — INPUT Marks[Student, Test]
Q2
B — One whole row at a time: Student 1 tests 1 to 3, then Student 2 tests 1 to 3
Q3
A — FOR Student ← 1 TO 4 FOR Test ← 1 TO 3 INPUT Marks[Student, Test] NEXT Test NEXT Student
Q4
B — OUTPUT "Enter mark for student ", Student, " test ", Test
Q5
A — OUTPUT the prompt first, then INPUT Marks[Student, Test]
Section B: Match the Pairs
Q1
INPUT Marks[Student, Test] → Reads a typed value into one cell of the 2D array, chosen by the current row (Student) and column (Test).
Q2
FOR Test ← 1 TO 3\n INPUT Marks[Student, Test]\nNEXT Test → The inner loop reads three values into one row, one for each test, before the outer loop moves to the next student.
Q3
A prompt can show which row and column is being entered, so the user knows which mark to type. → OUTPUT "Enter mark for student ", Student, " test ", Test
Q4
To enter values column by column instead of row by row, swap the loops so the outer loop controls the test (column). → FOR Test ← 1 TO 3\n FOR Student ← 1 TO 30\n INPUT Marks[Student, Test]
Q5
FOR Student ← 1 TO 30\n INPUT Marks[Student, Test]\nNEXT Student → There is no inner loop for Test, so Test never changes. Only one column is filled for each student, and the other columns stay empty.
Q6
FOR Student ← 1 TO 30\n FOR Test ← 1 TO 3\n INPUT Marks[Test, Student] → The indexes are in the wrong order. Marks[Test, Student] treats Test as the row and Student as the column, which does not match the Marks[Student, Test] layout and can go out of range.
Section C: Definitions
Q1
INPUT Marks[Student, Test] — Inside nested FOR loops, this reads a typed value into one cell of a 2D array. The inner loop fills a whole row before the outer loop moves to the next row.
Section D: Fill in the Blanks
Q1
FOR Student ← 1 TO 3 FOR Test ← 1 TO 4 INPUT Marks[Student, Test]
Q2
FOR Student ← 1 TO 3 FOR Test ← 1 TO 4 INPUT Marks[Student, Test]
Q3
FOR Test ← 1 TO 4 OUTPUT "Row ", Student, " column ", Test INPUT Marks[Student, Test]
Q4
FOR Test ← 1 TO 4 FOR Student ← 1 TO 3 INPUT Marks[Student, Test]
Q5
FOR Student ← 1 TO 3 FOR Test ← 1 TO 4 INPUT Marks[Student, Test]
Section E: Spot the Error
Q1
A — A FOR loop sets its counter with the assignment arrow, not =. The line should be FOR Student ← 1 TO 3.
Q2
C — Marks is a 2D array, so each cell needs a row index and a column index. It should be INPUT Marks[Student, Test].
Q3
A — The start value is bigger than the end value, so the loop never runs and no marks are read. It should be FOR Student ← 1 TO 3.
Q4
B — The inner loop reuses the outer loop's counter Row, which breaks the nesting. The inner loop must use its own counter: FOR Col ← 1 TO 4.
Q5
D — Nested loops must be closed in reverse order, so the inner loop is closed first. Line 4 should be NEXT Test.