In one of my classes at school, part of my assignment includes creating a constructor that assigns random values into a 2D array. While I have been able to create the array, have the constructor assign random values into said array, I cannot seem to create multiple objects with different 2D arrays. Any object I make seems to hold the same values previously assigned to the first one. I am fairly new at c++ so I assume the answer is barely flying over my head. TIA
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;
const int sizeOfArray = 3;
int myArray;//a
class Matrix
{
public:
int myArray[sizeOfArray][sizeOfArray];//a
Matrix ()//b
{
srand(time(NULL));
for(int i = 0; i < sizeOfArray; i++){
for(int j = 0; j < sizeOfArray; j++){
myArray[i][j] = rand() % 10;
}
}
}
void printMyArray();
};
void Matrix::printMyArray()//c
{
cout<<"The Matrix is as follows: \n"<<endl;
cout<<myArray[0][0]<<"\t"<<myArray[0][1]<<"\t"<<myArray[0][2]<<"\n"<<endl;
cout<<myArray[1][0]<<"\t"<<myArray[1][1]<<"\t"<<myArray[1][2]<<"\n"<<endl;
cout<<myArray[2][0]<<"\t"<<myArray[2][1]<<"\t"<<myArray[2][2]<<"\n"<<endl;
}
int main()
{
Matrix matrix;
matrix.printMyArray();
Matrix natrix;
natrix.printMyArray();
return 0;
}
I want the output to spit out two different arrays, however, the same array is repeated twice with the output as:
The Matrix is as follows:
4 0 9
6 0 4
6 3 0
The Matrix is as follows:
4 0 9
6 0 4
6 3 0