### Forming a matrix multiplication program

Hey guys, I just started Linear Algebra for my programming degree and to full understand everything I want to put it into code and make my own "matrix calculator." I have all the theory but I am having issues keeping the matrix variable and moving it around the class' functions to create it and output it. Once I know I have it saved I think I can get the addition/multiplication to work. Thanks! Here is the code I have so far:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149`` ``````#include #include using namespace std; class matrix{ private: int q = 1, s = 1, a = 1, b = 1; int i=0, j=0; int *r, *c, *w, *x; int rows[2], columns[2]; int matrix[2][2]; public: void getInfo1(int matrix[2][2]); void getInfo2(); void output1(); void output2(); }; int main() { matrix m1, m2; return 0; } void matrix::getInfo1(int matrix[2][2]) { cout<<"How many rows is your first matrix?"<>rows[0]; cout<<"How many columns is your first matrix?"<>columns[0]; //assigns rows and columns to pointers r = &rows[0]; c = &columns[0]; //Creates an array of matrices int matrix1[*r][*c]; //assigns value for the matrix1 definied by rxc for (i=0; i>matrix1[i][j]; s++; }//end column (j) for loop s=1; q++; }//end row (i) for loop } /* void matrix::getInfo2() { cout<<"How many rows is your second matrix?"<>rows[1]; cout<<"How many columns is your second matrix?"<>columns[1]; w = &rows[1]; x = &columns[1]; int matrix2[*w][*x]; //assings values for the matrix2 definied by w x for (i=0; i>matrix2[i][j]; a++; }//end column (j) for loop a=1; b++; }//end row (i) for loop }*/ void matrix::output1() { //outputs the matrix1 of numbers for (i=0; i
You can create static array for matrix like:

 ``1234567`` ``````#define MAX 20 class matrix { private: values[MAX][MAX]; };``````

and when `getInfo1 `is executed you can show boundaries to user and check his input.

More professional would be creating dynamic array, ether of using pointer to pointer array ( `**p`) and `new` or `vector` class from std library (i wrote example in this topic http://www.cplusplus.com/forum/general/107678/ )
SWEET! But what do you mean by check boundaries? I can define MAX 20 and set my array to that and use it in the functions correct? But then how would I nullify all the other parts if its a 4X4 and not a 20X20? Or do I copy what I from that array into an array of a distinct size? I tried using pointers but I do not know how to call it then because I do not know what the actual size would be. Thank you so much for your help!
this is what i meant:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` `````` unsigned row_size; unsigned col_size; unsigned row; unsigned col; int matrix[MAX_SIZE][MAX_SIZE]; do { cout << "enter number of columns: "; cin >> col_size; } while (col_size > MAX_SIZE); do { cout << "enter number of rows: "; cin >> row_size; } while (row_size > MAX_SIZE); //now that you know matrix is of size [col_size][row_size] cout << "enter values:" << endl; for (row=0; row < row_size; row++) for (col=0; col < col_size; col++) { cout << "[" << row << "][" << col << "] = "; cin >> matrix[row][col]; } cout << endl << "values entered:" << endl; for (row=0; row < row_size; row++) { for (col=0; col < col_size; col++) { cout << matrix[row][col] << " "; } cout << endl; } cout << endl;``````

but as i said before, this is static memory solution (C-like) and user can't enter matrix size bigger than defined value. If you would like to do this dynamically (user can enter any matrix size he wants):
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` `````` unsigned row_size; unsigned col_size; unsigned row; unsigned col; int **matrix; do { cout << "enter number of columns: "; cin >> col_size; } while (col_size <= 0); do { cout << "enter number of rows: "; cin >> row_size; } while (row_size <= 0); //now that you know matrix is of size [col_size][row_size] // create it: matrix = new int*[col_size]; // create columns for (col=0; col < col_size; col++) //create rows matrix[col] = new int[row_size]; cout << "enter values:" << endl; for (row=0; row < row_size; row++) for (col=0; col < col_size; col++) { cout << "[" << row << "][" << col << "] = "; cin >> matrix[col][row]; } cout << endl << "values entered:" << endl; for (row=0; row < row_size; row++) { for (col=0; col < col_size; col++) { cout << matrix[col][row] << " "; } cout << endl; } cout << endl; for (col=0; col < col_size; col++) { delete [] matrix[col]; //delete rows } delete [] matrix; //delete cols ``````

or you can use http://www.cplusplus.com/forum/general/107678/ ;)
Last edited on
@tath: Normal `delete` does not match the array-form of `new`; use array delete.

You do use column-major matrix. C/C++ style is to use row-major. That is not an error, but has to be documented.

Furthermore, you could allocate a single block for data rather than col_size separate blocks.

PS. Prefer a const to macro.
Last edited on