c code for sparse matrix .

  • csparse matrix
  • write a c code for sparse matrix .

    Write a program to determine whether the given matrix is a sparse matrix or not.

    Code

    #include <stdio.h> #include <stdlib.h> void setvalue(int **arr, int r, int c) { int n; for (int i = 0; i < r; i++) { printf("enter the values for row %d\n", i + 1); for (int j = 0; j < c; j++) { printf("enter the element\n"); scanf("%d", (*(arr + i) + j)); } } } void display(int **arr, int r, int c) { printf("\n"); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { printf("%d ", *(*(arr + i) + j)); } printf("\n"); } } int zero(int **arr, int r, int c) { int k = 0, count = 0; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { if (*(*(arr + i) + j) == 0) { count++; } } } return count; } int main() { int r, c; printf("enter the number of rows followed by coulum\n"); scanf("%d%d", &r, &c); int **arr = (int **)malloc(r * sizeof(int *)); for (int i = 0; i < r; i++) { arr[i] = (int *)malloc(c * sizeof(int)); } setvalue(arr, r, c); printf("\n"); display(arr, r, c); printf("\n"); if(zero(arr, r, c)==0){ printf("the given matrix is not sparse matrix\n"); } else printf("the given matrix is sparse matrix\n"); return 0; }