Thread: random number gen & looping probelm :?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    Question 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;
                    }
    :?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  4. random number
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 08:04 PM
  5. random number between 0 and variable value
    By HybridM in forum C++ Programming
    Replies: 23
    Last Post: 02-06-2003, 10:14 AM