#include <iostream>
int main()
{ int matrix[3][3]; int sparse[10][3]; int k = 0;
cout << "Enter 3 x 3 matrix:" << endl;
# 3 rows ,3 columns , Total = 9 elements
// Input matrix
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> matrix[i][j];
}
}
// Find non-zero elements
for (int i = 0; i < 3; i++)
{
#i represents the row number.
for (int j = 0; j < 3; j++)
# j represents the column number.
{
if (matrix[i][j] != 0) # If the matrix element is not zero, store it.
{
#k keeps track of the next position to store a non-zero element.
sparse[k][0] = i; #sparse[k][0] → Row
sparse[k][1] = j; #sparse[k][1] → Column
sparse[k][2] = matrix[i][j]; #sparse[k][2] → Value
k++;
}
}
}
// Display sparse matrix
cout << "\nSparse Matrix:" << endl;
cout << "Row\t Column\t Value" << endl;
for (int i = 0; i < k; i++)
{ cout << sparse[i][0] << "\t"
<< sparse[i][1] << "\t"
<< sparse[i][2] << endl;
}
return 0;
}




