Functions – Null Matrix

Functions – Null Matrix
Write a program to find whether the given matrix is null or not using functions.
A null matrix is a matrix in which all its elements are zero.

Function specification:

int checkNull(int **a, int m, int n)
The first argument corresponds to the pointer to an array.
The second argument corresponds to the number of rows.
The third argument corresponds to the number of columns.
The function returns a value of 1 if it is a null matrix and 0 otherwise.

Input and Output Format:
Assume that the maximum number of rows and columns in the matrix is 10.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1:
Enter the number of rows in the matrix
3
Enter the number of columns in the matrix
2
Enter the elements of the matrix
2
4
1
3
0
9
The matrix is
2 4
1 3
0 9
The matrix is not null

Sample Input and Output 2:
Enter the number of rows in the matrix
2
Enter the number of columns in the matrix
2
Enter the elements of the matrix
0
0
0
0
The matrix is
0 0
0 0
The matrix is null

Function Definitions:  int checkNull (int **a, int m, int n) 

Image result for c coding

Solution:
#include  <  stdio. h  >
#include  <  stdlib. h  >
int checkNull(int **a,int m,int n)
{
    int i,j,flag=1;
    for(i=0;i  <  m;i++)
    for(j=0;j  <  n;j++)
    if(a[i][j])
    flag=0;
    return flag;
}
int main()
{
    int m,n,**a,j,i;
    printf("Enter the number of rows in the matrix\n");
    scanf("%d",&m);
    printf("Enter the number of columns in the matrix\n");
    scanf("%d",&n);
    a=(int **)malloc(m*sizeof(int *));
    for(i=0;i  <  m;i++)
    a[i]=(int *)malloc(n*sizeof(int *));
    printf("Enter the elements in the matrix\n");
    for(i=0;i  <  m;i++)
    for(j=0;j  <  n;j++)
    scanf("%d",&a[i][j]);
    printf("The matrix is\n");
     for(i=0;i  <  m;i++)
     {
        for(j=0;j  <  n;j++)  
        printf("%d ",a[i][j]);
        printf("\n");
     }
     if(checkNull(a,m,n))
      printf("The matrix is null\n");
      else
      printf("The matrix is not null\n");
      return 0;

}

No comments