Repository navigation
Expand file tree
/
Copy pathMatrixMultiply.cpp
More file actions
86 lines (69 loc) · 2.53 KB
/
Copy pathMatrixMultiply.cpp
File metadata and controls
86 lines (69 loc) · 2.53 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
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <fstream>
#include <time.h>
#include <vector>
using namespace std;
int GetNextInt(ifstream &fin, char input[]);
void GetMatrixInts(ifstream &fin, vector<vector<int>>, char file[], int dim);
int main()
{
char firstInput[] = "Matrix1.txt"; // source file containing 1st matrix to multiply
char secondInput[] = "Matrix2.txt"; // source file containing 2nd matrix to multiply
vector<vector<int>> firstMatrix; // first matrix to multiply
int firstDim; // number of rows/cols in first matrix
vector<vector<int>> secondMatrix; // second matrix to multiply
int secondDim; // number of rows/cols in second matrix
vector<vector<int>> resultMatrix; // matrix holding result of multiplication
time_t start; // initial time when program starts
time_t end; // time after output image is created
time_t elapsedTime; // time for image processing
// Open input files
ifstream fin1(firstInput);
ifstream fin2(secondInput);
// Verify files opened correctly
if (fin1.fail())
{
cerr << "Couldn't open " << firstInput << endl;
return 1;
}
if (fin2.fail())
{
cerr << "Couldn't open " << secondInput << endl;
return 1;
}
// Fill first input matrix from file
GetMatrixInts(fin1, firstMatrix, firstInput, firstDim);
// Fill second input matrix from file
GetMatrixInts(fin2, secondMatrix, secondInput, secondDim);
// Get dimensions of first and second matrix
firstDim = GetNextInt(fin1, firstInput);
secondDim = GetNextInt(fin2, firstInput);
// Verify that the two matrices have the same dimensions
if (firstDim != secondDim)
{
cerr << "Input matrices are not the same size!";
return 1;
}
fin1.close();
fin2.close();
return 0;
}
// Read the next integer from file input
int GetNextInt(ifstream &fin, char input[])
{
int value;
fin >> value;
if (fin.eof())
{
cerr << "Error in length of " << input << endl;
exit(EXIT_FAILURE);
}
return value;
}
// Fill a matrix with integers from file input
void GetMatrixInts(ifstream &fin, int **matrix, char file[], int dim)
{
for (int i=0; i<dim; i++)
for (int j=0; j<dim; j++)
matrix[i][j] = GetNextInt(fin, file);
}