forked from wafarifki/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnorm_matrix.C
81 lines (57 loc) · 1.84 KB
/
norm_matrix.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* find the norm of a matrix using dynamic memory allocation*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/*The driver code*/
int main(void)
{
int **array, *norm;
int i, j, m, n, max;
/*Taking the number of rows and colums from the user*/
printf("Enter the number of rows and cols of matrix:");
scanf("%d%d", &m,&n);
/*dynamic memory allocation*/
array=(int **)malloc(m*sizeof(int *));
for(i=0;i<m;i++)
array[i]=(int *)malloc(n*sizeof(int));
/*dynamic memory allocation of the normal*/
norm=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
norm[i]=0;
/*Taking the elements of the 2D matrix*/
printf("Enter the elements of the matrix:");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &array[i][j]);
}
}
/*Algorithm for normal separation*/
for(i = 0; i <m; i++)
{
for(j = 0; j <n; j++)
{
norm[i] = norm[i] + array[j][i];
}
}
max = norm[0];
/*Base case of max*/
for(i = 0; i <m; i++)
{
if(max < norm[i])
{
max = norm[i];
}
}
/*Max of each colums */
printf("Max(");
for(i = 0; i < n; i++)
{
printf("%d ", norm[i]);
}
printf(")\n");
/*Final output*/
printf("Norm of the given matrix is %d\n", max);
return 0;
}