-
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()%(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;
}
}
-
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.
-
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?
-
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.