1

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
asked Dec 23, 2013 at 3:50
1
  • 1
    [OT]: You have memleaks: you don't release array ('rows' and 'column'). Commented Dec 23, 2013 at 4:04

3 Answers 3

3

The error is in your print loop, not your initialization.

 for(int x=0;x<row;x++){
 cout<<endl;
 for (int y=0;y<col;y++){
 cout<<array[row-1][col-1]<<" ";
 }
 }

That always prints the lower-right corner (row-1, col-1). You probably meant this:

 for(int x=0;x<row;x++){
 cout<<endl;
 for (int y=0;y<col;y++){
 cout<<array[x][y]<<" ";
 }
 }

Also a small usage tip: Don't call srand() in your createArray() function. If you call it anywhere, call it in main(), near the beginning, and make that the only place in your code it gets called.

answered Dec 23, 2013 at 3:51
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Finals week left me a little brain dead X(
1

Your code has several errors:

  1. The range in the print is wrong. It must be array[row][col]
  2. Don't initialize srand() every time you call the function. It's a big mistake.
  3. Don't use the modulo to get the random number. Multiply and divide: 100.0 * rand() / static_cast<double>(RAND_MAX)
  4. Please! Free the memory you allocated. Call delete to free up the arrays you created (first free the internal arrays and finally the main array)
answered Dec 23, 2013 at 4:07

Comments

0

In your main row and cols arn't changing they are just the dimensions of your array. You have to print the values based on the corrdinates:

for(int x=0;x<row;x++){
 cout<<endl;
 for (int y=0;y<col;y++){
 cout<<array[x][y]<<" ";
 }
}
cout<<endl;

If you want to print the arry reversly you have to do this

for(int x=0;x<row;x++){
 cout<<endl;
 for (int y=0;y<col;y++){
 cout<<array[row-1-x][row-1-y]<<" "; // not row-1 and cols-1 as they're not changing
 }
}
cout<<endl;
answered Dec 23, 2013 at 4:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.