ADVANCED DATA STRUCTURES PROGRAM • LEVEL 10 — SPARSE MATRICES
Transpose a Sparse Matrix by Column Scan
Learn how to transpose a sparse matrix by column scan using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
0 1 5 1 2 6 2 0 4
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int triplets[][3] = {{0, 2, 4}, {1, 0, 5}, {2, 1, 6}};
for (int column = 0; column < 3; column++)
for (int index = 0; index < 3; index++)
if (triplets[index][1] == column) printf("%d %d %d\n", column, triplets[index][0], triplets[index][2]);
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
0 1 5 1 2 6 2 0 4
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Visit source columns in order and exchange each matching row-column pair.
- Display the computed result.
Visit source columns in order and exchange each matching row-column pair.
EFFICIENCY
Time and space complexity
Time complexity
O(columns * non-zero)
Auxiliary space
O(non-zero elements)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
