0
0
C++programming~10 mins

Multi-dimensional arrays in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a 2D array of integers with 3 rows and 4 columns.

C++
int arr[3][[1]];
Drag options to blanks, or click blank then click option'
A5
B3
C4
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing rows and columns sizes.
Using the wrong number for the second dimension.
2fill in blank
medium

Complete the code to access the element in the second row and third column of a 2D array named 'matrix'.

C++
int value = matrix[[1]][2];
Drag options to blanks, or click blank then click option'
A1
B2
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 instead of 1 for the second row index.
Confusing row and column indices.
3fill in blank
hard

Fix the error in the code to correctly initialize a 2D array with 2 rows and 3 columns.

C++
int grid[2][3] = {{ {1, 2, 3}, [1] }};
Drag options to blanks, or click blank then click option'
A{4, 5, 6}
B(4, 5, 6)
C[4, 5, 6]
D4, 5, 6
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or parentheses instead of curly braces.
Not enclosing the second row in braces.
4fill in blank
hard

Fill both blanks to declare and initialize a 2D array named 'table' with 2 rows and 2 columns, containing values 1, 2, 3, 4.

C++
int table[[1]][[2]] = {{ {1, 2}, {3, 4} }};
Drag options to blanks, or click blank then click option'
A2
B3
C4
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 or 4 instead of 2 for dimensions.
Mixing up rows and columns.
5fill in blank
hard

Fill all three blanks to create a nested loop that prints all elements of a 2D array 'data' with 3 rows and 2 columns.

C++
for (int i = 0; i < [1]; i++) {
    for (int j = 0; j < [2]; j++) {
        std::cout << data[i][j] << [3];
    }
    std::cout << std::endl;
}
Drag options to blanks, or click blank then click option'
A3
B2
C" "
D"\n"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop limits.
Printing newline inside inner loop instead of space.