-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadingMatrixFromFile.cpp
More file actions
73 lines (66 loc) · 1.34 KB
/
ReadingMatrixFromFile.cpp
File metadata and controls
73 lines (66 loc) · 1.34 KB
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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int main() {
int row, column, matrix1[3][3], matrix2[3][3], matrix3[3][3];
printf("Reading files....\n");
//get matrices from the file
FILE *file1, *file2;
char *line1, *line2, *number;
file1 = fopen("matrix.txt","r");
row=0;
while(!feof(file1)) {
column = 0;
fscanf(file1, "%s", line1);
number = strtok(line1, ",");
while(number != NULL){
matrix1[row][column] = atoi(number);
column++;
number = strtok(NULL, ",");
}
printf("\n");
row++;
}
fclose(file1);
printf("29");
file2 = fopen("matrix2.txt","r");
printf("Reading files....\n");
row=0;
while(!feof(file2)) {
column = 0;
fscanf(file2, "%s", line2);
number = strtok(line2, ",");
while(number != NULL){
matrix2[row][column] = atoi(number);
column++;
number = strtok(NULL, ",");
}
printf("\n");
row++;
}
fclose(file2);
printf("File Reading Completed....\n");
//multiply the matrices
int sum=0;
int i, j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
for(int k=0;k<3;k++){
sum+=(matrix1[i][k]*matrix2[k][j]);
}
matrix3[i][j]=sum;
sum=0;
}
}
printf("Matrix multiplication Completed....\n");
printf("Printing the result...\n");
//printing the result marix
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d ",matrix3[i][j]);
}
printf("\n");
}
return 0;
}