ProgrammingModerate
A rectangular table with R rows and C columns is traversed in a loop that touches each cell once in row-major order. Which total cost is correct?
- A. O(R + C), since only the borders are visited
- B. O(R x C), linear in the number of cells
- C. O(R^2 x C^2), since each cell is compared with all others
- D. O(log(R x C)), since the table is indexed
Correct answer: B. O(R x C), linear in the number of cells
A full traversal touches every cell once, so the cost is proportional to the number of cells.
More C Programming questions
- Which declaration makes a pointer that walks row by row through an int matrix with 6 columns?
- For an int matrix a with R rows and C columns stored in row-major order, what is the flat index of the cell at (r, c) in terms of R?
- A matrix of 10 x 10 is searched linearly for a value. About how many cell reads happen in the worst case?
- A 2D array is passed to a function that needs the number of rows. Which argument can it NOT rely on?
- int a[3][3] = {0}; int p = a[1]; p[2] = 7; printf("%d", a[1][2]); What is printed?
- The number of ways to choose a 2 x 2 submatrix in a 4 x 4 matrix (choosing consecutive rows and columns) is what?