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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstdlib>
using namespace std;
// The Matrix class definition goes here
class Matrix
{
public:
//Constructors
Matrix();
Matrix(int, int);
Matrix(int[][5], int, int);
//Methods
void print();
Matrix add( Matrix );
Matrix subtract( Matrix );
//counter variables
int i;
int j;
private:
//Constants for maximum array size of 5
#define maxRows 5
#define maxCols 5
//The matrix for the class
int matrixArray[maxRows][maxCols];
//Variables to represent the actual array size (most arrays will not be maximum size)
int actualRows, actualCols;
};
//Constructor 1 sets the matrix size to the maximum and initializes it to 0's
Matrix::Matrix()
{
actualRows=maxRows;
actualCols=maxCols;
// matrixArray[actualRows][actualCols]={0};
for(i=0;i<actualRows;i++)
{
for(j=0;j<actualCols;j++)
{
matrixArray[i][j]=0;
}
}
}
//Constructor 2 sets the matrix size to the passed in values and initializes it to 0's
Matrix::Matrix( int newNumRows, int newNumColumns )
{
if( newNumRows <=maxRows)
{
actualRows=newNumRows;
}
else
{
actualRows=maxRows;
}
if( newNumColumns <=maxCols)
{
actualCols=newNumColumns;
}
else
{
actualCols=maxCols;
}
}
//Constructor 3 sets the matrix size to the passed in values and initializes it with the passed in 2D array
Matrix::Matrix( int ar[][5], int arRows, int arColumns )
{
if( arRows <=maxRows)
{
actualRows=arRows;
}
else
{
actualRows<maxRows;
}
if( arColumns <=maxCols)
{
actualCols=arColumns;
}
else
{
actualCols=maxCols;
}
ar[actualRows][actualCols]=matrixArray[actualRows][actualCols];
for(i=0;i<actualRows;i++)
{
for(j=0;j<actualCols;j++)
{
matrixArray[i][j]=ar[i][j];
}
}
}
int main()
{
//declaring various 2d arrays for use in the class
int ar1[5][5] = {{0,1,2},
{9,8,7}};
int ar2[5][5] = {{6,5,4},
{3,4,5}};
int ar3[5][5] = {{-1,2,0,15,9},
{0,3,6,7,-11},
{-4,50,12,0,6},
{1,1,1,1,1},
{-8,16,4,6,22}};
int ar4[5][5] = {{0,-4,3},
{9,-4,-3}};
int ar5[5][5] = {{1,2,3,4,5},
{2,3,4,5,6},
{3,4,5,6,7},
{4,5,6,7,8},
{5,6,7,8,9}};
int ar6[5][5] = {{7,8},
{9,10},
{11,12}};
//the following line copies array 3 to the array declared in the class
Matrix m3( ar1, 2, 3);
<< "Test 2: add method" << endl << endl;
Matrix m4( ar4, 2, 3);
cout << "Matrix m3 contains" << endl;
m3.print();
cout << endl << endl << "Matrix m4 contains" << endl;
m4. print();
cout << endl << endl << "The result of adding matrices m3 and m4" << endl;
m5 = m3.add( m4 );
return 0;
}
// Code the Matrix class methods below this line
void Matrix::print()
{
cout<<"\n\n";
for(i=0;i<actualRows;i++)
{
for(j=0;j<actualCols;j++)
{
cout<<matrixArray[i][j]<<" ";
}
cout<<"\n";
}
}
Matrix Matrix::add( Matrix anotherMatrix )
{
int aMatrix[3][3];
for(i=0;i<actualRows;i++)
{
for(j=0;j<actualCols;j++)
{
//error is here: No match for operator[] in anotherMatrix[((Matrix*)this)->Matrix::i]
aMatrix[i][j]=anotherMatrix[i][j]+matrixArray[i][j];
}
}
}
|