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 141 142 143 144 145 146 147 148 149 150 151 152 153
|
//****************************************************************************************************
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
void loadData(int matrix1 [4][4], int matrix2[4][4], int rows, int cols);
void addMatrices(int matrix1 [4][4], int matrix2[4][4], int result[4][4], int rows, int cols);
void subtractMatrix(int matrix1 [4][4], int matrix2[4][4], int result[4][4], int rows, int cols);
//void multiplyMatrix(int matrix1 [4][4], int matrix2[4][4], int result[4][4], int rows, int cols);
void outputData(int matrix1 [4][4], int matrix2[4][4], int rows, int cols);
void outputResult(int result[4][4], int resultType, int rows, int cols);
int main ()
{
int rows,
cols,
resultType = 1,
matrix1[4][4],
matrix2[4][4],
result[4][4];
ifstream DataFile;
ifstream Data2File;
fstream OutputFile;
loadData(matrix1, matrix2, rows, cols);
outputData(matrix1, matrix2, rows, cols);
addMatrices(matrix1, matrix2, result, rows, cols);
outputResult(result, resultType, rows, cols);
resultType = 2;
subtractMatrix(matrix1, matrix2, result, rows, cols);
outputResult(result, resultType, rows, cols);
resultType = 3;
//multiplyMatrix(matrix1, matrix2, result, rows, cols);
//outputResult(result, resultType, rows, cols);
return 0;
}
//****************************************************************************************************
void loadData(int matrix1 [4][4], int matrix2[4][4], int rows, int cols)
{
cout << "Enter Data for Matrix 1" << endl;
for (int rows = 0; rows < 4; rows++)
{
for (int col = 0; col < 4; col++)
{
cout << "row - " << rows + 1 << " colummn - " << col + 1 << " = ";
cin >> matrix1[rows][col];
}
}
cout << endl << "Enter Data for Matrix 2" << endl;
for (int rows = 0; rows < 4; rows++)
{
for (int col = 0; col < 4; col++)
{
cout << "row - " << rows + 1 << " colummn - " << col + 1 << " = ";
cin >> matrix2[rows][col];
}
}
cout << endl;
}
//****************************************************************************************************
void addMatrices(int matrix1 [4][4], int matrix2[4][4], int result[4][4], int rows, int cols)
{
for( int rows = 0; rows < 4; rows++)
{
for (int col = 0; col < 4; col++)
{
result[rows][col] = matrix1[rows][col] + matrix2[rows][cols];
}
}
}
//****************************************************************************************************
void subtractMatrix(int matrix1 [4][4], int matrix2[4][4], int result[4][4], int rows, int cols)
{
for( int rows = 0; rows < 4; rows++)
{
for (int col = 0; col < 4; col++)
{
result[rows][col] = matrix1[rows][col] - matrix2[rows][cols];
}
}
}
//****************************************************************************************************
void outputData(int matrix1 [4][4], int matrix2[4][4], int rows, int cols)
{
cout << "Matrix 1:" << endl;
for (int rows = 0; rows < 4; rows++)
{
for (int col = 0; col < 4; col++)
{
cout << setw(6) << matrix1[rows][col];
if (col == 3)
{
cout << endl;
}
}
}
cout << "Matrix 2;" << endl;
for (int rows = 0; rows < 4; rows++)
{
for (int col = 0; col < 4; col++)
{
cout << setw(6) << matrix2[rows][col];
if (col == 3)
{
cout << endl;
}
}
}
}
//****************************************************************************************************
void outputResult(int result[4][4], int resultType, int rows, int cols)
{
if (resultType == 1) {
cout << "Addition Results" << endl;
}
if (resultType == 2) {
cout << "Subtraction Results" << endl;
}
if (resultType == 3) {
cout << "Multiplication Results" << endl;
}
for (int rows = 0; rows < 4; rows++)
{
for (int col = 0; col < 4; col++)
{
cout << setw(6) << result[rows][col];
if (col == 3)
{
cout << endl;
}
}
}
}
//****************************************************************************************************
|