Thread: Array Question

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Array Question

    Hey guys I trying to compute averages by row and store them in an array so that I can later print the array holding the averages. However for some reason the program crashes after creating the initial 2 dimensional array. The compiler isn't showing any errors anyone see what's wrong? Any help would be great!



    Code:
    #include <iostream>
    #include <iomanip> 
    #include <ctime> 
    
    using namespace std;
    
    const int ROW = 3;
    const int COL = 4; 
    const int LOW = 1; 
    const int UP = 50; 
    const int SIZE = 4;
    
    void GenerateArray(int[][COL], int, int, int, int);
    void GenerateAverages(int[][COL], float[], int, int, int);
    void PrintArray(int[][COL], int, int);
    void PrintAveArray(float[], int);
    
    int main()
    {
        
    	srand(time(NULL));
        int table[ROW][COL] = {0};
    	float a_table[SIZE] = {0};
    
        GenerateArray(table, ROW, COL, LOW, UP);
    	PrintArray(table, ROW, COL);
    	cout << "\n\n";
    	GenerateAverages(table, a_table, ROW, COL, SIZE);
    	PrintAveArray(a_table, SIZE);
    
    	cout << "\n\n";
    
    
    }//END OF MAIN
    
    
    void GenerateArray(int t[ ][COL], int row, int col, int lower_limit, int upper_limit){
        for (int r = 0; r < row; r++)
            for (int c = 0; c < col; c++)
                t[r][c] = rand()&#37;(upper_limit - lower_limit + 1) + lower_limit;
    }
    
    void PrintArray(int t[][COL], int row, int col){
            for (int r = 0; r < row; r++){
                for (int c = 0; c < col; c++)
                   cout << setw(5) << t[r][c];
                cout << endl;
    		}
    }
    
    void PrintAveArray(float t[], int size){
    	for (int i = 0; i < size; i++)
               cout << setw(5) << t[i];
                cout << endl;
    }
    
    void GenerateAverages(int table[][COL], float a[], int r, int c, int size){
    
    int total = 0;
    int i = 0;
    float average = 0;
    
    for (c = 0; c < COL; c++){
        total = 0;
    	average = 0;
        for (r = 0; r < ROW; r++)
    	{
            total = total + table[r][c];
    		average = (float)(total/ROW);
    	}
    	a[c] = average;
    	
    }
    }
    Last edited by GCNDoug; 11-28-2007 at 02:17 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
        int table[ROW][COL] = {0};
    Your compiler will like you better if you use
    Code:
        int table[ROW][COL] = {{0}};
    Code:
    void GenerateAverages(int table[][COL], float a[], int row, int col, int size){
    // ...
    for (col = 0; col < COL; col++){
        // ...
        for (row = 0; row < ROW; row++)
    Reusing parameters, bad idea. Consider something more like
    Code:
    for (int c = 0; c < col; c++){
        // ...
        for (int r = 0; row < row; r++)
    A quick GDB session shows the line you fault on.
    Code:
    GNU gdb 6.6.90.20070912-debian
    Copyright (C) 2007 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu"...
    Using host libthread_db library "/lib/libthread_db.so.1".
    (gdb) r
    Starting program: ./GCNDoug-1 
        9   29   28
       37   29   44
       36   47   47
       49   48   41
    
    
    
    Program received signal SIGFPE, Arithmetic exception.
    0x0000000000400b62 in GenerateAverages (table=0x7fff2b994010, 
        a=0x7fff2b994040, row=0, col=0, size=4) at GCNDoug-1.cpp:69
    69                      average = float(total / col);
    (gdb) p total
    $1 = 9
    (gdb) p col
    $2 = 0
    (gdb) q
    The program is running.  Exit anyway? (y or n) y
    Right, you have a divide-by-zero error. Why? Because you're reusing the variable col! I told you it was a bad idea.

    See, the first time the loop comes through, the counting variable col is zero, so you get a div-by-zero error. If you use a different variable for the counting variable, col will always be equal to COL -- and away you go.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    ok I got it fixed now, I have one more problem though. The new array is kept as a float throughout the entire program. Why aren't my numbers printing in the single dimension array as floats?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Because in this line
    Code:
    average = (float)(total/ROW);
    you're casting to float too late. The integer division has already taken place. Try using
    Code:
    average = ((float)total) / ROW;
    or
    Code:
    average = (float)total / ROW;
    See, you have to cast one of the operands to a float before the division takes place, or, as I already mentioned, you get an int which you then cast to a float, which is rather redundant.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM