w3resource

C Exercises: Determine whether a matrix is a sparse matrix

C Array: Exercise-29 with Solution

Write a program in C to accept a matrix and determine whether it is a sparse matrix.

A sparse martix is matrix which has more zero elements than nonzero elements. The task is to write a C program that accepts a matrix and determines whether it is a sparse matrix. A sparse matrix is one where most of the elements are zero. The program prompts the user for the matrix dimensions and its elements, counts the number of zeros, and then checks if the number of zeros is greater than half the total number of elements to determine and print if the matrix is sparse.

Visual Presentation:

C Exercises: Determine whether a matrix is a sparse matrix

Sample Solution:

C Code:

#include <stdio.h>

/* A sparse matrix is a matrix that has more zero elements than nonzero elements */
int main() {
    static int arr1[10][10];
    int i, j, r, c;
    int ctr = 0;

    // Display the purpose of the program
    printf("\n\nDetermine whether a matrix is a sparse matrix :\n");
    printf("----------------------------------------------------\n");

    // Input the number of rows of the matrix
    printf("Input the number of rows of the matrix : ");
    scanf("%d", &r);

    // Input the number of columns of the matrix
    printf("Input the number of columns of the matrix : ");
    scanf("%d", &c);

    // Input elements into the matrix and count zeros
    printf("Input elements in the matrix :\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            printf("element - [%d],[%d] : ", i, j);
            scanf("%d", &arr1[i][j]);
            if (arr1[i][j] == 0) {
                ++ctr; // Increment the counter for zero elements
            }
        }
    }

    // Check if zeros are more than half of total elements to determine sparsity
    if (ctr > ((r * c) / 2)) {
        printf("The given matrix is a sparse matrix.\n");
    } else {
        printf("The given matrix is not a sparse matrix.\n");
    }

    printf("There are %d number of zeros in the matrix.\n\n", ctr);

    return 0;
}

Sample Output:

Determine whether a matrix is a sparse matrix :                                                               
----------------------------------------------------                                                          
Input the number of rows of the matrix : 2                                                                    
Input the number of columns of the matrix : 2                                                                 
Input elements in the first matrix :                                                                          
element - [0],[0] : 0                                                                                         
element - [0],[1] : 0                                                                                         
element - [1],[0] : 1                                                                                         
element - [1],[1] : 0                                                                                         
The given matrix is sparse matrix.                                                                            
There are 3 number of zeros in the matrix. 

Flowchart:

Flowchart: Determine whether a matrix is a sparse matrix

C Programming Code Editor:

Previous: Write a program in C to calculate determinant of a 3 x 3 matrix.
Next: Write a program in C to accept two matrices and check whether they are equal.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming-exercises/array/c-array-exercise-29.php