I'm trying to create a 2D array filled with random ints. The number of rows and columns are decided by the user. The issue is that when I run the program, the array is filled with the same number in each spot.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int** createArray(int rows, int cols){
// Create a 2D array of ints that is rows x cols
int** array = new int*[rows];
for(int i = 0;i<rows;++i){
array[i] = new int[cols];
}
// Fill the array
srand(time(NULL));
for(int r = 0; r<rows; ++r){
for(int c = 0;c<cols;++c){
array[r][c] = (rand()%100);
}
}
return array;
}
int main(int argc, char* argv[]){
int** array;
int row = atoi(argv[1]);
int col = atoi(argv[2]);
array = createArray(row,col);
for(int x=0;x<row;x++){
cout<<endl;
for (int y=0;y<col;y++){
cout<<array[row-1][col-1]<<" ";
}
}
cout<<endl;
return 0;
}
The output generally follows something along the lines of:
53 53 53
53 53 53
53 53 53