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
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.

📌 Limited Seats! Register Now - Free AI-ML Certification (December 2025)

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.