IPA FE Subject B first sample Question 4
A question IPA Japan published in 基本情報技術者試験 科目Bのサンプル問題 問4, translated into English. ITPEC is based on the same exams.
From the answer group below, select the correct combination of answers to be inserted into A through C in the description. Here, the array index starts at 1.
A matrix in which most of the elements are 0 is called a sparse matrix. The following program converts a matrix stored in a two-dimensional array into a data structure suitable for storing a sparse matrix, in order to reduce the amount of data of the matrix.
The function transformSparseMatrix converts the matrix given as a two-dimensional array in the argument matrix into an array of integer-type arrays, and returns it. When the function transformSparseMatrix is called as transformSparseMatrix(, the return value is {{A}, {B}, {C}}.
Answer group
Explanation
A: 1, 2, 2, 3, 3, 4, 5, B: 1, 2, 3, 4, 5, 4, 5, C: 3, 2, 2, 1, 3, 2, 1 — the program scans the matrix row by row and, for each nonzero element, appends its row number, column number and value to the three arrays.
The outer loop runs over the rows i and the inner one over the columns j, so row 1 is scanned from left to right, then row 2, and so on. The nonzero elements in that order:
Reading the columns down gives the three arrays: sparseMatrix[1] holds the row numbers (A), sparseMatrix[2] the column numbers (B) and sparseMatrix[3] the values (C). Three arrays of seven replace the 25 elements of the matrix.
Answer (b)
Why not others:
- •(a) — A and B are right, but C lists the values column by column (column 4's 1 and 2 before column 5's 3 and 1), so its fifth value, 2, no longer belongs to element [3, 5], which is 3
- •(c) and (d) — swap A and B, but
sparseMatrix[1]receivesi, the row number; (c) also has the column-by-column values of (a)
Key rule: This sparse-matrix format keeps three parallel arrays (row, column, value) with one entry per nonzero element, so the same position in each array describes the same element.
Math & Data Science · AI-written, may contain errors
This question comes from the past questions IPA Japan publishes, translated into English by ITPEC Practice. ITPEC Practice is an independent study tool and is not affiliated with IPA or ITPEC. See the original paper (PDF) or Report an issue.