Challenge - 5 Problems
2D Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested loops filling 2D array
What is the output of this C program that fills a 2D array and prints it?
C
#include <stdio.h> int main() { int arr[2][3]; int val = 1; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { arr[i][j] = val++; } } for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0; }
Attempts:
2 left
💡 Hint
Look at how the nested loops assign values row by row.
✗ Incorrect
The outer loop goes over rows, inner loop over columns. Values increment from 1 to 6 filling row-wise.
🧠 Conceptual
intermediate1:30remaining
Memory layout of 2D arrays in C
In C, how is a two-dimensional array like int arr[2][3] stored in memory?
Attempts:
2 left
💡 Hint
Think about how C stores arrays by default.
✗ Incorrect
C stores 2D arrays in row-major order, meaning all elements of the first row are stored first, then the second row, and so on.
🔧 Debug
advanced1:30remaining
Identify the error in 2D array initialization
What error will this code produce when compiled?
int arr[2][3] = { {1, 2}, {3, 4, 5, 6} };
Attempts:
2 left
💡 Hint
Check the number of elements in each inner initializer list.
✗ Incorrect
The second inner initializer has 4 elements but the array row size is 3, causing a compilation error.
📝 Syntax
advanced1:30remaining
Correct syntax to declare a pointer to 2D array
Which option correctly declares a pointer to a 2D array of integers with 3 columns?
Attempts:
2 left
💡 Hint
Remember the parentheses placement for pointer to array.
✗ Incorrect
int (*ptr)[3]; declares ptr as a pointer to an array of 3 integers, which matches a 2D array row.
🚀 Application
expert2:30remaining
Sum of diagonal elements in a square 2D array
Given a 3x3 integer array, which code snippet correctly calculates the sum of the main diagonal elements?
Attempts:
2 left
💡 Hint
Main diagonal elements have equal row and column indices.
✗ Incorrect
The main diagonal elements are arr[0][0], arr[1][1], arr[2][2]. Option B sums these correctly.