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:
#include <string>
#include <iostream>
usingnamespace 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?"<<endl;
cin>>rows[0];
cout<<"How many columns is your first matrix?"<<endl;
cin>>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<rows[0]; i++) {
for (j=0; j<columns[0]; j++) {
cout<<"What is your "<<q<<" "<<s<<" term?"<<endl;
cin>>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?"<<endl;
cin>>rows[1];
cout<<"How many columns is your second matrix?"<<endl;
cin>>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<rows[1]; i++) {
for (j=0; j<columns[1]; j++) {
cout<<"What is your "<<a<<" "<<b<<" term?"<<endl;
cin>>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<rows[0]; i++) {
for (j=0; j!=columns[0]; j++) {
cout<<matrix1[i][j];
cout<<" ";
}//end column (j) output for loop
cout<<endl;
}//end row (i) output for loop
}
/*
void matrix::output2()
{
//outputs the matrix2 of numbers
for (i=0; i<rows[1]; i++) {
for (j=0; j!=columns[1]; j++) {
cout<<matrix2[i][j];
cout<<" ";
}//end column (j) output for loop
cout<<endl;
}//end row (i) output for loop
}
*/
#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!
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):