secret
Created

C++ Matrices

  • Download Gist
gistfile1.txt
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
#include <iostream>
using namespace std;
 
const int n = 3; // Change this as needed for a new test.
 
typedef float SquareMatrixType[n][n];
 
void MatrixSum(int size, SquareMatrixType A, SquareMatrixType B, SquareMatrixType Result);
void Print(int size, SquareMatrixType M);
 
int main(void)
{
SquareMatrixType First = { {2.0, 1.0, 3.0},
{0.0, 4.0, -1.0},
{-2.0, 5.0, 1.0}
};
SquareMatrixType Second = { {1.0, 6.0, 0.0},
{-2.0, 4.0, -1.0},
{0.0, 3.0, 2.0}
};
SquareMatrixType Sum;
MatrixSum(n, First, Second, Sum);
cout << "First matrix:" << endl;
Print(n, First);
cout << endl << "Second matrix:" << endl;
Print(n, Second);
cout << endl << "Sum matrix:" << endl;
Print(n, Sum);
cout << endl;
return 0;
}
void MatrixSum(int size, SquareMatrixType A, SquareMatrixType B, SquareMatrixType Result)
{
int row, col;
for (row = 0; row < size; row++)
{
for (col = 0; col < size; col++)
Result[row][col] = A[row][col] + B[row][col];
}
}
void Print(int size, SquareMatrixType M)
{
int row, col;
for (row = 0; row < size; row++)
{
for (col = 0; col < size; col++)
cout << M[row][col] << " ";
cout << endl;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.