Write a program that constructs and displays a unitary square matrix M of dimension N. A unitary matrix is a matrix, such that:
\(u_{ij}=\Big\{\begin{array}{c c c} 1 & if & i=j \\ 0 & if & i\neq j \end{array}\)
Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#define SIZE 50
void main()
{
int M[SIZE][SIZE]; /* Square matrix */
int N; /* dimension of the matrix */
int i, j;
do {
printf("Enter the dimension: ");
scanf("%d", &N);
} while (N <= 0 || N > SIZE);
// constructing the matrix
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
M[i][j]=(i==j);
/*
if (i==j)
M[i][j]=1;
else
M[i][j]=0;
*/
}
printf("\n\n*** Matrix ***\n");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
printf("%4d", M[i][j]);
printf("\n");
}
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Hashing using linear probing