trying to calculate average value of 2d array...getting negative numbers

i am trying to calculate the average value of a 2d array that is 400x400 elements big. for some reason i am getting a different negative value every time. here is my code i have for the program so far. the data file is too big to attach. hopefully its not necessary. the max value in the data file is 99 and no numbers are negative. eventually i will be calculating variance of a subarray.

#include <cmath>
#include <iostream>
#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
#include <string>
#include <stdlib>
using namespace std;
int const N = 400;

double calcMean (const int plot[][N]);

int main()
{
int elevation[N][N];
ifstream file1;
string filename;
int nrows, ncols;

cout << "Enter the name of the input file.\n";
cin >> filename;
file1.open(filename.c_str());
if (file1.fail())
{
cerr << "Error opening input file.\n";
return 0;
}
file1 >> nrows >> ncols;
for (int i=0; i<=nrows-1; i++)
{
for (int j=0; j<=ncols-1; j++)
{
file1 >> elevation[i][j];
}
}

cout << calcMean(elevation);

return 0;
}

double calcMean (const int plot[][N])
{
int sum = 0;
int mean = 0;
for (int r=0; r<=N-1; r++)
{
for (int c=0; c<=N-1; c++)
{
sum += plot[r][c];
mean = (sum / 160000);
}
}
return mean;
}
[1598 byte] By [devlinobrian] at [2007-11-20 11:47:47]
# 1 Re: trying to calculate average value of 2d array...getting negative numbers
With your current algorithm, you should only calculate sum/(N*N) after the outer loop, not at each iteration of the inner loop.

Also, instead of writing c<=N-1, write the simpler c < N.
laserlight at 2007-11-9 1:26:12 >
# 2 Re: trying to calculate average value of 2d array...getting negative numbers
i am trying to calculate the average value of a 2d array that is 400x400 elements big. for some reason i am getting a different negative value every time.Your array is 400x400, but you read number of rows and columns from the file. Are you sure they are both equal to 400?
If they are greater than 400 - your app could crash.
If they are less than 400 - you would leave part of the array uninitialized (containing garbage).
You should probably pass the dimention of the initialized part of that array into your calcMean() function.
VladimirF at 2007-11-9 1:27:15 >
# 3 Re: trying to calculate average value of 2d array...getting negative numbers
thanks laserlight it works now...edit* well no i think i just got lucky with good number. the value changes every time and i still get negatives.

VladimirF the data file is exactly 400x400 so it won't be an issue. however i am curious, how would i pass the initialized dimension?
devlinobrian at 2007-11-9 1:28:16 >
# 4 Re: trying to calculate average value of 2d array...getting negative numbers
In the case of dimension < 400, just pass it as a parameter to calcMean and use that rather than N-1 in your for loops.

In the case of dimension > 400, you'd have to use malloc or new to create the array dynamically. That's a more advanced concept, and would be handled at read-time. You'd still need to pass the array size to calcMean, though.
Lindley at 2007-11-9 1:29:10 >
# 5 Re: trying to calculate average value of 2d array...getting negative numbers
VladimirF the data file is exactly 400x400 so it won't be an issue. however i am curious, how would i pass the initialized dimension?
Just like you pass any parameters: change the declaration to
double calcMean (const int plot[][N], int rows, int cols);
and call it like that:
calcMean(elevation, nrows, ncols);and use those values inside the function instead of N.
thanks laserlight it works now.Pardon me, but are you sure? The suggestion is correct, but all it affects is the efficiency of your code (you shouldn't really recalculate mean on each iteration). The final result will be exactly the same, because you always write over previously calculated value.
VladimirF at 2007-11-9 1:30:17 >
# 6 Re: trying to calculate average value of 2d array...getting negative numbers
Pardon me, but are you sure? The suggestion is correct, but all it affects is the efficiency of your code (you shouldn't really recalculate mean on each iteration). The final result will be exactly the same, because you always write over previously calculated value.

i realized that after i posted so i edited it. i have implemented what you have suggested and i still get random numbers negative or positive for the mean. is there something wrong with my calculations?

here is my edited method

double calcMean (const int plot[][N], int r, int c)
{
int sum = 0;
int mean = 0;
for (r=0; r<N; r++)
{
for (c=0; c<N; c++)
{
sum += plot[r][c];
}
mean = (sum / (N*N));
}
return mean;
}
devlinobrian at 2007-11-9 1:31:22 >
# 7 Re: trying to calculate average value of 2d array...getting negative numbers
Passing r and c as parameters that way is pointless. What you meant was r=0; r<r_max; r++, with r_max as the param.

Also, your mean= statement is still inside one of the for loops. It definitely won't work in there.
Lindley at 2007-11-9 1:32:15 >
# 8 Re: trying to calculate average value of 2d array...getting negative numbers
i have moved mean statement to the correct place. something wrong with the addition part. i tried just returning the sum of all the values and got a very large different negative number every time.
devlinobrian at 2007-11-9 1:33:15 >
# 9 Re: trying to calculate average value of 2d array...getting negative numbers
mean = sum / (r * c);

You're passing in r and c but still using N. Use r and c instead.
GCDEF at 2007-11-9 1:34:24 >
# 10 Re: trying to calculate average value of 2d array...getting negative numbers
i realized that after i posted so i edited it. i have implemented what you have suggested and i still get random numbers negative or positive for the mean. is there something wrong with my calculations?

here is my edited method

double calcMean (const int plot[][N], int r, int c)
{
int sum = 0;
int mean = 0;
for (r=0; r<N; r++)
{
for (c=0; c<N; c++)
{
sum += plot[r][c];
}
mean = (sum / (N*N));
}
return mean;
}


Nope try,

double calcMean (const int plot[][N], int r, int c)
{
int sum = 0;
for (int row = 0; row < r; row++)
{
for (int col = 0; col < c; col++)
{
sum += plot[row][col];
}

}
return sum / (r * c);
}
GCDEF at 2007-11-9 1:35:25 >
# 11 Re: trying to calculate average value of 2d array...getting negative numbers
Also, your mean= statement is still inside one of the for loops. It definitely won't work in there.Once again, it will work just fine in a loop. The only issue is that it will calculate the mean N times (first N-1 times - incorrectly), but the last one will do it right.
VladimirF at 2007-11-9 1:36:26 >
# 12 Re: trying to calculate average value of 2d array...getting negative numbers
thanks GCDEF. every thing you guys were talking about finally makes sense.
devlinobrian at 2007-11-9 1:37:18 >
# 13 Re: trying to calculate average value of 2d array...getting negative numbers
one more question, if i was to calculate the variance of the 2d array, i would have to somehow pass in the value of mean i got from the function...how would i do that?

double calcVariance (const int plot[][N], int r, int c)
{
double sum = 0;
for (int row = 0; row < r; row++)
{
for (int col = 0; col < c; col++)
{
int tempVal = (plot[row][col] - **mean would go here**)^2;
// i know i can't square like that but u get the idea
sum += tempVal;
}
}
double variance = (sum / ((r * c) - 1));
return variance;
}
devlinobrian at 2007-11-9 1:38:25 >
# 14 Re: trying to calculate average value of 2d array...getting negative numbers
Just add another argument to your function.
GCDEF at 2007-11-9 1:39:22 >