I'm not sure if I should post this on the DevC++ forum on sourceforege but whatever, maybe it'll help someone.
I recently had the following code to compute an the average in an array's rows
PHP Code:
double RowAvg(const double arr[][CLM], int row){
/* computes average of each set of numbers. using 1 demensional array method */
int i;
double avg;
for(i = 0; i<CLM; i++)
avg+= arr[row][i]; /* this gives the given row and adds all elements in that row */
avg = avg / CLM;
return avg;
}
for the life of me I couldn't figure out why the ouput would be ouptut like this
PHP Code:
Enter row 0 element 0 : 122
Enter row 0 element 1 : 233.3
Enter row 0 element 2 : 33.5
Enter row 1 element 0 : 233
Enter row 1 element 1 : 93
Enter row 1 element 2 : 233
This array contains:
Row 0: 122.000 233.300 33.500
Row 1: 233.000 93.000 233.000
The avg of row 1 is 264764614254408340000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000.000
The avg of row 2 is 882548714181361150000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000.000
The avg of row 3 is 294182904727120370000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000.000
The avg of the total array is 315.933
when i finally tracked the problem down, it was so simple I wanted to smack my self. Most of you know but I had for gotten to initialize avg to 0. THAT'S IT.
I Understand C's philosophy is not to get in the way of the programmer, but wow. in another language if i had done lets say[
PHP Code:
#language unknown :D
>>> l = [1,2,3,4,5,6,7]
>>> for num in l:
avg+=num
avg2 = avg / len(l)
I'd get this output
PHP Code:
Traceback (most recent call last):
File "<pyshell#4>", line 2, in -toplevel-
avg+=num
NameError: name 'avg' is not defined
really simple??? I guess my beef is: is it too much to ask for C to help me out once in awhile. i mean I know it has it's idiosyncasies but sheesh!!!..
that's my two cents, my ten cents is free.