# random number gen & looping probelm :?

• 04-10-2009
FoxeySoulSLayer
random number gen & looping probelm :?
ok in the following code I'm trying to make the program print the array and sum it up for each row and column. In addition to that use a random number for each item in the array....I only get the number 42 and I'm at a loss as to how i create the random number generater + loop to put in that will generate a random number for each item in the array and to top that off put it in a function:
Code:

```// This program demonstrates accepting a 2D array argument. #include <iostream> #include <iomanip> #include<fstream> using namespace std; // Global constants const int COLS = 11;  // Number of columns in each array const int TBL1_ROWS = 3;// Number of rows in table1 void Sum_Rows(int [][COLS], int); void Sum_Columns(int [][COLS],int); int main() {     int total,         rNumb = 0;                 rNumb = 1 + rand()%100;           int table1[TBL1_ROWS][COLS] = {{rNumb, rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb},                                   {rNumb, rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb},                                   {rNumb, rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb,rNumb}};   Sum_Rows(table1,TBL1_ROWS);   Sum_Columns(table1,COLS);                     system("PAUSE");   return 0; } void Sum_Rows(int array[][COLS], int rows) {     int total;     int rNumb;           for(int row = 0; row < TBL1_ROWS; row++){           total = 0;           for (int col = 0; col < COLS; col++){           total += array[row][col];           cout << setw(5) << array[row][col];           }           cout << setw(2) << "=" << setw(3)<< total <<endl;                  }                 }                 void Sum_Columns(int array[][COLS], int rows) {     int total;           for(int col = 0; col < COLS; col++){           total = 0;           for (int row = 0; row < TBL1_ROWS; row++){           total += array[row][col];           //cout << setw(3) << array[row][col];           }                     cout << setw(2) <<" "<< total;                  }                 cout <<endl;                 }```
:?
• 04-10-2009
matsp
You need to call rand() multiple times, so you need a loop similar to the ones that walk across the columns and rows in your functions, that fill in each cell with a random number.

And you may want to look up srand() in your help, so as to avoid the question "every time I run the program I get the same random numbers, why?"

--
Mats