C Tutorial

# C program to validate dense matrix or not

## C program to validate dense matrix or not

In numerical analysis, a sparse dense is a matrix in which most of the elements are nonzero.

Let us see example 4x4 dense matrix,

```1 1 0 0
0 1 1 1
0 0 1 1
1 1 1 1
```

## C program to validate whether matrix is dense matrix or not

This c program is used to check whether the user input matrix is dense matrix or not.

This program is implementd using two dimensional array for matrix data and validating sparse matrix logic is done using nested for loops.

A dense matrix is a matrix in which most of the elements are nonzero.

```#include<stdio.h>

#define COLUMN 10
#define ROW 10

void main()
{
int dense_matrix[ROW][COLUMN];
int i,j, rows, columns, zeroscount=0, nonzeroscount=0;

printf("\nEnter Matrix Rows: ");
scanf("%d", &rows);
printf("\nEnter Matrix Columns: ");
scanf("%d", &columns);
printf("\nEnter Matrix: \n");
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
scanf("%d", &dense_matrix[i][j]);
}
}

for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
if(dense_matrix[i][j]==0)
{
zeroscount++;
}
else
{
nonzeroscount++;
}
}
}
if(nonzeroscount>zeroscount)
{
printf("\nUser input matrix is a dense matrix!\n");
}
else
{
printf("\nUser input matrix is not a dense matrix!\n");
}
}
```

Output:

```\$ cc dense_matrix.c
\$ ./a.out

Enter Matrix Rows: 3

Enter Matrix Columns: 3

Enter Matrix:
1 1 1
1 1 0
0 0 1

User input matrix is a dense matrix!

\$ ./a.out

Enter Matrix Rows: 3

Enter Matrix Columns: 3

Enter Matrix:
0 0 0
0 1 1
1 0 1

User input matrix is not a dense matrix!

```

C Tutorial