-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path50_matrix_multiplication.c
141 lines (125 loc) · 2.74 KB
/
50_matrix_multiplication.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/********************************************************************************
DOCUMENTATION:
NAME :V.KARTHIEKEYAN
DATE :20.07.2021
DESCRIPTION :WAP to find the product of given matrix.
OUTPUT : ./a.out
ENTER THE INPUT ARRAY DETAILS:
ENTER THE NO. OF ROWS: 1
ENTER THE NO. OF COLUMNS: 3
ENTER THE INPUTS TO ARRAY:
3 2 5
INPUT ARRAY:
3 2 5
TRANSPOSE ARRAY
3
2
5
OUTPUT ARRAY
9
DO YOU WANT TO CONTINUE(Y/y): y
ENTER THE INPUT ARRAY DETAILS:
ENTER THE NO. OF ROWS: 3
ENTER THE NO. OF COLUMNS: 3
ENTER THE INPUTS TO ARRAY:
1 2 3
5 6 8
2 4 7
INPUT ARRAY:
1 2 3
5 6 8
2 4 7
TRANSPOSE ARRAY
1 5 2
2 6 4
3 8 7
OUTPUT ARRAY
34 97 75
34 97 75
34 97 75
DO YOU WANT TO CONTINUE(Y/y): n
******************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<stdio_ext.h>
void print_array(int row, int col, int **arr);
int main()
{
char choice;
int **A, **AT, **OUTPUT_ARRAY, A_row, A_col, AT_row, AT_col;
do
{
A = NULL;
AT = NULL;
OUTPUT_ARRAY = NULL;
printf("ENTER THE INPUT ARRAY DETAILS:\nENTER THE NO. OF ROWS: ");
scanf("%d", &A_row);
printf("ENTER THE NO. OF COLUMNS: ");
scanf("%d", &A_col);
if(A_row < 0 || A_col < 0)
{
printf("ENTER ONLY POSITIVE VALUES");
}
else
{
AT_row = A_col;
AT_col = A_row;
A = (int **)malloc( sizeof(int *) * A_row );
AT = (int **)malloc( sizeof(int *) * AT_row );
OUTPUT_ARRAY = malloc(sizeof(int *) * A_row);
for(int i=0; i<A_col; i++)
{
A[i] =(int *) malloc( sizeof(int) * A_col );
}
for(int i=0; i<AT_row;i++)
{
AT[i] =(int *) malloc( sizeof(int) * AT_col );
OUTPUT_ARRAY[i] = (int *) malloc(sizeof(int) * AT_col);
}
printf("ENTER THE INPUTS TO ARRAY:\n");
for(int i=0; i < A_row; i++)
{
for(int j=0; j < A_col; j++)
{
scanf("%d", &A[i][j]);
AT[j][i]=A[i][j]; //assigning the AT array while inputing the A array
}
}
int temp, i, j;
for(i=0; i<A_row; i++)
{
for( j=0; j<A_row; j++)
{
//product of the row of A to col of AT and store in the output array
temp=0;
for(int k=0; k < A_col ; k++)
temp= temp + ( A[j][k] * AT[k][k] );
OUTPUT_ARRAY[i][j] = temp;
}
}
printf("INPUT ARRAY:\n");
print_array(A_row, A_col, A);
printf("TRANSPOSE ARRAY\n");
print_array(AT_row, AT_col, AT);
printf("OUTPUT ARRAY\n");
print_array(A_row, AT_col, OUTPUT_ARRAY);
free(A);
free(AT);
free(OUTPUT_ARRAY);
A=NULL;
AT=NULL;
OUTPUT_ARRAY=NULL;
}
printf("DO YOU WANT TO CONTINUE(Y/y): ");
scanf(" %c",&choice);
} while ( choice == 'y' || choice == 'Y' );
return 0;
}
//to print the 2D array
void print_array(int row, int col, int **arr )
{
for(int i=0; i<row; i++, printf("\n"))
for(int j=0; j<col; j++)
printf("%d ",arr[i][j]);
printf("\n");
}