two-dimensional array

Apr 14, 2011 at 11:03pm
I'm trying to create a two-dimensional array whose size is based on user input, then the array is filled with random numbers depending on how big the user wants it. I've got most of it stapled out now I just can't get the darn thing to print in a 2D array, it all comes out as one column.

here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
include<iostream>
#include<iomanip>
#include<time.h>
using namespace std;

void fillArray (int array[], int number);
void printArray (int array[], int number);

int main()

{

   int number, array[100] = {};
                                  
   cout << "Enter how many integers you'd like generated" << endl;    cout << "\tbetween 3 and 100: ";          
   cin >> number;
   if (number < 3 || number > 100)
   {
      cout << "ERROR: THAT IS NOT A VALID NUMBER" << endl;
      return 0;
   }
   else
   {
   fillArray(array, number);                    }
}
  
void fillArray(int array[100], int number)

{  
                                                 
   srand(time(NULL));
   
   for (int i = 0; i < number; i++) {
      for (int j = 0; j < number; j++) 
         array [i][j] = rand() % 100 + 1;
   
      cout << array[i][j]  << endl;     
   }


} 


the problem is in the nested for loop.
This program won't run and i'm getting these errors:

error c2109: subscript requires array or pointer type
error c2065: 'j' : undeclared identifier
Last edited on Apr 15, 2011 at 5:17am
Apr 14, 2011 at 11:44pm
Because of this little number: cout << array[i][j] << endl; you are telling it to put a newline after every element it outputs. You should move that to outside of the embeded for loop but inside of the first for loop.

EDIT: You should move THE "endl" to the outer for loop. Sorry about that.
Last edited on Apr 14, 2011 at 11:52pm
Topic archived. No new replies allowed.