-3

(Write a C function that should check if a matrix (4x5) is sparse or not. Knowing that: sparse matrix is a matrix that has zeros more than the half of its size.)

That's a problem for a sheet in out subject here is my code:

#include <stdio.h>
#include <stdlib.h>

int Spare(int [4][5]);

int main()
{
    int arr[4][5];
    int m,n;
    for(m=0;m<4;m++){
        for(n=0;n<5;n++){
            scanf("%d ",&arr[4][5]);
        }
    }
    Spare(arr[4][5]);
    return 0;
}

int Spare(int Arr[4][5]){

    int i,j;
    int zerocount=0;
    for(i=0;i<4;i++){
        for(j=0;j<5;j++){

            if(Arr[i][j]==0){
                zerocount++;
            }
        }
    }
    if(zerocount>=10) return 1;
    else return 0;
}

Its Running but after the user enter inputs of array it stops working! Any Help Guys?

WhozCraig
  • 65,258
  • 11
  • 75
  • 141

1 Answers1

0

Change scanf("%d",&arr[4][5]) into scanf("%d",&arr[m][n]). You are just storing the input in arr[4][5] everytime in the loop.

The Spare(arr[4][5]) pass the element of fifth row and sixth column only which doesn't exist. The Spare function expects an array as parameter and you are passing the single element only. The function call must be done in this way: Spare(arr)

ShivaGaire
  • 2,283
  • 1
  • 20
  • 31