Test: matrices (two-dimensional arrays)

Test 5 of 8 · Tests

A test on matrices in C++: reading and printing a matrix, sums by rows and columns, diagonals, searching for an element and checking properties of the whole matrix. This page is new – the tasks were made following the model of tests from other areas, in two groups, with solutions.

Before you start: review the lessons Two-dimensional arrays in C++, Arrays in C++, and then do the tasks from the page Matrices – examples.
How to practise: choose one group and solve it in 45 minutes, without looking at the solutions or your notes. Only after that compare your solution with the solution of the practice test.

What you need to know for the test

  • the declaration int a[100][100] and reading with two nested loops
  • what a row (i) and a column (j) are, and how to go through rows and how through columns
  • the main diagonal a[i][i] and the anti-diagonal a[i][n-1-i]
  • the sum, the largest element and its position, counting elements with some property
  • the transposed matrix and checking properties (symmetric, magic square)

What the test looks like

  • Duration: one school lesson (45 minutes).
  • Tasks: 3 (sometimes 4) tasks; every group has tasks of the same difficulty, but with different data.
  • What is graded: whether the program works correctly for the example and for edge cases, whether the input and output are as in the text and whether the code is clear.
Suggested scoring (3 tasks of 10 points each)
Points0–1112–1617–2122–2627–30
Grade12345

The scale is a suggestion for self-assessment; the teacher can adapt it. (In Serbian schools grades go from 1 to 5, where 5 is the best.)

Group 1

TASK 01Group 1

Sum by rows

Read a matrix of integers with n rows and m columns and print the sum of the elements of each row.

Input: the numbers n and m (1 ≤ n, m ≤ 100), then n lines with m numbers each.
Output: n lines – the sum of each row.

Example

Input

2 3
1 2 3
4 5 6

Output

6
15
TASK 02Group 1

Main diagonal and anti-diagonal

Read a square matrix of order n and print the sum of the elements on the main diagonal and on the anti-diagonal.

Example

Input

3
1 2 3
4 5 6
7 8 9

Output

Main: 15
Anti: 15
TASK 03Group 1

Cinema: free seats

The seating in a cinema is given as a matrix: 0 is a free seat, 1 is a taken seat. For every row print the number of free seats, and then the number of the row (from 1) with the most free seats (if there are several, the first one).

Example

Input

3 4
1 0 0 1
0 0 0 0
1 1 1 0

Output

2
4
1
Most free seats: row 2

Group 2

TASK 04Group 2

Transposed matrix

Read a matrix with n rows and m columns and print the transposed matrix (rows become columns).

Example

Input

2 3
1 2 3
4 5 6

Output

1 4
2 5
3 6
TASK 05Group 2

The largest element and its position

Read a matrix with n rows and m columns and print the largest element, as well as the row and column where it is (counting from 1). If the largest element appears several times, print the first occurrence.

Example

Input

3 3
4 9 2
3 5 7
8 1 6

Output

Largest: 9 (row 1, column 2)
TASK 06Group 2

Magic square

A square matrix is a magic square if the sums of all rows, all columns and both diagonals are equal. Read a matrix of order n and print YES or NO.

Example 1

Input

3
4 9 2
3 5 7
8 1 6

Output

YES

Example 2

Input

2
1 2
3 4

Output

NO
Common mistakes and tips
  • The first index is the row, the second the column: a[row][column].
  • Set the row sum to 0 inside the outer loop – otherwise the sums "stick" to each other.
  • When printing a matrix, the new line goes after the inner loop, not after every element.
  • People count from 1, and arrays from 0: when you print the number of a row, print i + 1.
  • Diagonals exist only in a square matrix (n × n).

Check your knowledge

Open the quiz

Mini quiz: matrices

1. Which element is in the second row and the third column of the matrix a?




2. Which elements make up the anti-diagonal of a matrix of order n?




3. How many elements does a matrix with 4 rows and 5 columns have?