I'm practicing C++ by writing matrix manipulation programs, each one being more sophisticated than the one before. (I'm also in linear algebra, so matrices are of specific interest to me)
Anyways, say I wanted the user to make as many matrices as they wanted and then bring them up when desired, but I dont' want to waste a huge amount of memory by making 30 objects at the beginning of the program.
For example, a prompt which asks the user if they want to make another matrix. If they say yes, a new object is THEN created with the users parameters.
Is this possible?
Here's my code right now:
Code:
#include <iostream.h>
#include <stdlib.h> // needed for system("PAUSE")
class matrix
{
public:
matrix(int,int); // constructor - takes size, aXb, and fills the array with 0's
~matrix(); // destructor - doesn't do much, but still needed
void ShowMatrix();
void GetValues();
private: // the following variables are private, which means they
// can only be changed by functions/methods within the class itself!
int x;
int y;
int a;
int b;
int matrix_ar[8][8]; // 8x8 array
};
matrix::matrix(int A, int B)
{
a = A; // sets row size
b = B; // set column size
for(x = 0; x<8 ; x++) // fills array with 0's
{
for(y = 0; y<8; y++)
{
matrix_ar[x][y]=0;
}
}
}
matrix::~matrix()
{
}
void matrix::ShowMatrix() // displays the matrix
{
for(x = 0; x<a; x++)
{
cout << "| ";
for(y = 0; y<b; y++)
{
cout << matrix_ar[x][y] << "\t";
}
cout << " |\n";
}
}
void matrix::GetValues()
{
cout << "CON GRAD JOO LAY SHUNS! You've just made a NEW MATRIX!\n";
cout << "The SIZE of your MATRIX is: " << a << "x" << b << endl;
cout << "Now you must enter the matrix values! WHOO!\n";
for(x = 0; x<a; x++)
{
for(y=0; y<b; y++)
{
cout << "(" << (x+1) << ", " << (y+1) << ")\n" << endl;
cin >> matrix_ar[x][y];
}
}
}
int main(int argc, char *argv[])
{
// some variables...
int a;
int b;
// program really starts here!
cout << "Welcome!\n";
cout << "Enter the size of your matrix:\n";
cout << "Rows: ";
cin >> a;
cout << endl << "Columns: ";
cin >> b;
matrix matrix_A(a,b);
matrix_A.GetValues();
matrix_A.ShowMatrix();
system("PAUSE");
return 0;
}
P.S. -> I brought this up in my previous thread on arrays, but the one reply wasn't clear so I thought I'd create a new topic.
Thanks!