📖 Scenario: You are working on a simple program to represent a small network of cities connected by roads. Each city is a node, and roads are connections between them. You will use an adjacency matrix to show which cities are directly connected.
🎯 Goal: Build a program that creates an adjacency matrix for 4 cities, sets up connections between them, and then prints the matrix to show the connections.
📋 What You'll Learn
Create a 4x4 integer matrix called adjMatrix initialized with zeros
Create an integer variable numCities and set it to 4
Set specific connections between cities by updating adjMatrix
Print the adjacency matrix in a readable format
💡 Why This Matters
🌍 Real World
Adjacency matrices are used in computer networks, social networks, and maps to represent direct connections between points.
💼 Career
Understanding adjacency matrices helps in roles involving graph algorithms, network analysis, and software development for mapping and routing.
Progress0 / 4 steps
1
Create the adjacency matrix
Create a 4x4 integer matrix called adjMatrix initialized with zeros.
DSA C
Hint
Use nested braces to initialize all elements to zero.
2
Add the number of cities
Create an integer variable called numCities and set it to 4.
DSA C
Hint
Just create a simple integer variable with the value 4.
3
Set connections between cities
Set the following connections in adjMatrix: city 0 connected to city 1 and city 2, city 1 connected to city 2, and city 2 connected to city 3. Use adjMatrix[row][column] = 1; to mark connections.
DSA C
Hint
Assign 1 to the correct positions in the matrix to show connections.
4
Print the adjacency matrix
Use nested for loops with variables i and j to print the adjMatrix in matrix form. Print each element followed by a space, and print a new line after each row.
DSA C
Hint
Use two loops to go through rows and columns, printing each value with a space, then a new line after each row.
Practice
(1/5)
1. What does an adjacency matrix represent in graph theory?
easy
A. A 2D array showing connections between nodes
B. A list of nodes in the graph
C. A sequence of edges in order
D. A tree structure of nodes
Solution
Step 1: Understand adjacency matrix concept
An adjacency matrix is a 2D array where rows and columns represent nodes.
Step 2: Identify what the matrix stores
Each cell shows if an edge exists between two nodes (1 for connected, 0 for not).
Final Answer:
A 2D array showing connections between nodes -> Option A
Quick Check:
Adjacency matrix = 2D array [OK]
Hint: Adjacency matrix is always a 2D grid of node connections [OK]
Common Mistakes:
Confusing adjacency matrix with adjacency list
Thinking it stores node values instead of edges
Assuming it is a sequence or tree
2. Which of the following is the correct way to declare a 4x4 adjacency matrix in C?
easy
A. int matrix[4];
B. int matrix[4][4];
C. int matrix[];
D. int matrix[4,4];
Solution
Step 1: Recall C array declaration syntax
In C, a 2D array is declared as type name[rows][columns].
Step 2: Match syntax for 4x4 matrix
For 4 rows and 4 columns, it is int matrix[4][4];
Final Answer:
int matrix[4][4]; -> Option B
Quick Check:
2D array syntax = int matrix[4][4]; [OK]
Hint: Use int matrix[rows][columns] for 2D arrays in C [OK]
Common Mistakes:
Using single dimension array for matrix
Using comma inside brackets (invalid syntax)
Not specifying both dimensions
3. Given the following adjacency matrix for a graph with 3 nodes:
Step 1: Understand incoming edges in adjacency matrix
Incoming edges to a node are shown by 1s in that node's column.
Step 2: Check each column for 1s
Column 0 has 1 at row 2 (node 2 -> node 0). Column 1 has 1 at row 0 (node 0 -> node 1). Column 2 has 1 at row 1 (node 1 -> node 2). Column 3 has no 1s (all 0s).
Step 3: Identify nodes with no incoming edges
Nodes 1 and 3 have no incoming edges because column 1 has 1 at row 0 (so node 1 has incoming edge), so this is incorrect. Actually, column 1 has 1 at row 0, so node 1 has incoming edge. Column 3 has no 1s, so node 3 has no incoming edges. Node 0 has incoming edge from node 2. Node 2 has incoming edge from node 1. So only node 3 has no incoming edges.
Final Answer:
[3] -> Option D
Quick Check:
Column 3 all 0s [OK]
Hint: Look down columns to find incoming edges for each node [OK]
Common Mistakes:
Checking rows instead of columns for incoming edges
Confusing directed and undirected edges
Missing edges from nodes to themselves
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.