-
Notifications
You must be signed in to change notification settings - Fork 1
/
onediDiaMat.c
60 lines (55 loc) · 966 Bytes
/
onediDiaMat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Program to implement Diagonal Matrix in one-dimensional array
#include <stdio.h>
int main()
{
int a[10], n, i, j, an[10][10], count = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("The array is: ");
for (i = 0; i < n; i++)
{
printf("%d", a[i]);
printf(" ");
}
// logic for implementing
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
{
an[i][j] = a[count];
count++;
}
else
an[i][j] = 0;
}
}
printf("\nThe array in the diagonal matrix is: \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", an[i][j]);
}
printf("\n");
}
return 0;
}
/*
Output:
Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
The array is: 1 2 3 4 5
The array in the diagonal matrix is:
1 0 0 0 0
0 2 0 0 0
0 0 3 0 0
0 0 0 4 0
0 0 0 0 5
*/