Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a 2D adjacency matrix for 4 vertices.
DSA C
int adjacencyMatrix[4][4] = [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single zero instead of a 2D array initializer.
Using NULL which is invalid for integer arrays.
✗ Incorrect
The adjacency matrix is a 2D array initialized with zeros for no edges.
2fill in blank
mediumComplete the code to add an edge from vertex 1 to vertex 3 in the adjacency matrix.
DSA C
adjacencyMatrix[1][[1]] = 1;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 as index which is out of bounds for vertex 3.
Confusing vertex numbers with array indices.
✗ Incorrect
Index 2 represents vertex 3 because indexing starts at 0.
3fill in blank
hardFix the error in the code to correctly check if there is an edge from vertex 2 to vertex 0.
DSA C
if (adjacencyMatrix[2][[1]] == 1) { printf("Edge exists\n"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using vertex number directly as index without subtracting 1.
Using an index out of bounds like 3.
✗ Incorrect
Vertex 0 corresponds to index 0 in the matrix.
4fill in blank
hardFill both blanks to complete the nested loops that print the adjacency matrix.
DSA C
for (int i = 0; i [1] 4; i++) { for (int j = 0; j [2] 4; j++) { printf("%d ", adjacencyMatrix[i][j]); } printf("\n"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes out of bounds access.
Using '>' or '>=' causes loops not to run.
✗ Incorrect
Loop indices run from 0 to less than 4 to cover all vertices.
5fill in blank
hardFill all three blanks to create a function that initializes the adjacency matrix to zero.
DSA C
void initializeMatrix(int matrix[4][4]) { for (int [1] = 0; [2] < 4; [3]++) { for (int j = 0; j < 4; j++) { matrix[i][j] = 0; } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'j' in outer loop which conflicts with inner loop.
Missing increment expression causes infinite loop.
✗ Incorrect
The outer loop uses variable i starting at 0 and increments by 1 until less than 4.