C++ Program to Find if an Array is a Sparse Matrix

This is a C++ Program to Find if an Array is a Sparse Matrix.

Problem Description

The program takes an array and checks if it is a sparse matrix. A sparse matrix is a matrix which has maximum elements equal to 0.

Problem Solution

1. The program takes the number of rows and columns of the matrix.
2. Then the elements are entered.
3. If the matrix contains maximum number of elements as 0, then it is a sparse matrix.
4. Else not.
5. The result is printed.
6. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find if an Array is a Sparse Matrix. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int A[10][10], i, j, m, n, count = 0;
  6.     cout << "Enter number of rows and columns : ";
  7.     cin >> m >> n;
  8.     cout << "Enter array elements : ";
  9.     for (i = 0; i < m; i++)
  10.     {
  11.         for (j = 0; j < n; j++)
  12.         {
  13.             cin >> A[i][j];
  14.             if (A[i][j] == 0)
  15.                 count++;
  16.         }
  17.     }
  18.     if (count > ((m * n) / 2))
  19.         cout << "The matrix is a sparse matrix.\n ";
  20. 	else
  21.         cout << "The given matrix is not a sparse matrix.\n ";
  22.     for (i = 0; i < m; i++)
  23.     {
  24.         for (j = 0; j < n; j++)	
  25.             cout << A[i][j] << " ";
  26.         cout << "\n ";
  27.     }
  28.     return 0;
  29. }
Program Explanation

1. The user is asked to enter the number of rows and columns.
2. The elements are then asked to enter and stored in the matrix ‘A’.
3. If the element entered is 0, a variable ‘count’ is incremented.
4. If count is greater than half the total size of the matrix, then the matrix is a sparse matrix.
5. Else the entered array is not a sparse matrix.
6. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter number of rows and columns : 3 3
Enter array elements : 1 0 2 0 3 0 4 0 5
The given matrix is not a sparse matrix.
 1 0 2
 0 3 0
 4 0 5
 
Case 2 :
Enter number of rows and columns : 1 3
Enter array elements : 0 10 0
The matrix is sparse matrix.
0 10 0
 
Case 3 :
Enter number of rows and columns : 3 3
Enter array elements : 1 0 0 0 2 0 0 0 3
The matrix is a sparse matrix.
 1 0 0
 0 2 0
 0 0 3

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Note: Join free Sanfoundry classes at Telegram or Youtube

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.