# Thread: problems finding the average of an array column

1. ## problems finding the average of an array column

I am having a problem finding the average of each column in my array. I think I have the layout right, but it gives me a seg. fault when I get to the "Analyze array" section.

Code:
```#include "my.h"

int main(){
int rows, columns, i, j, newrows, *temp, min, max, minrow, maxrow, avgrow;
float **array, avg, avgtemp;

scanf("%d", &rows);
scanf("%d", &columns);

newrows = rows + 3;
minrow = rows;
maxrow = rows + 1;
avgrow = rows + 2;

// Create array
array = malloc(newrows * sizeof(float));
for(i=0; i < rows; i++)
array[i] = malloc(columns * sizeof(float));

// store array
for(i=0; i <rows; i++){
for(j=0; j<columns; j++){
scanf("%d", temp);
*(*(array + i) + j) = *temp;
}
}
// Analyze array
for(i=0; i < rows; i++){
for(j=0; j < columns; j++){
avgtemp += *(*(array + i) + j);
array[avgrow][j] = avg / j;
}
}

// print array
PrintArray(array, rows, columns);

return 0;
}```
I am trying to determine the average and put the answer in a row underneath. I still have two extra rows for the min and max, but if I can find out the average, the rest should fall into place. Thanx in advance.

2. Average = sum / elements.

So sum up the total elements in a row or column (depending on what you're averaging), then simply divide that by how ever many elements there are.

Also, you never free anything you allocate. Fix that.

Furthermore, why are you going through all the convoluted pointer notation crap when you can simply do: array[ i ][ j ] = temp;

Oh, and why is temp a pointer? And why don't you have it point any place?

Quzah.

3. Code:
```#include "my.h"

int main(){
int rows, columns, i, j, newrows, temp, min, max, minrow, maxrow, avgrow;
float **array, avg;

scanf("%d", &rows);
scanf("%d", &columns);

newrows = rows + 3;
minrow = rows;
maxrow = rows + 1;
avgrow = rows + 2;

// Create array
array = malloc(newrows * sizeof(float));
for(i=0; i < rows; i++)
array[i] = malloc(columns * sizeof(float));

// store array
for(i=0; i <rows; i++){
for(j=0; j<columns; j++){
scanf("%d", &temp);
array[i][j] = temp;
}
}
// Analyze array
for(i=0; i < columns; i++){
for(j=0; j < rows; j++){
avg += array[j][i];
array[avgrow][i] = avg / rows;
}
}

// print array
PrintArray(array, rows, columns);

free(array);
return 0;
}```
I made some changes to the code... per your requests, and I am still having the same problems. I don't think that I am getting something.

4. You don't calculate the average every iteration through the column or row. You do it once at the end.
Code:
```int array[] = { 5, 9, 7, 11, 33, 22, 10 }
int x, sum, average;

for( x = sum = 0; x < 7; x++ )
{
sum += array[ x ];
}

average = sum / 7; /* since there are 7 elements in this array */```
Quzah.

5. but then if i wait till the end... how would i determine the average of each column?

Code:
```#include "my.h"

int main(){
int rows, columns, i, j, newrow, temp, min, max, minrow, maxrow, avgrow, sum=0;
float **array;

scanf("%d", &rows);
scanf("%d", &columns);

newrow = rows + 3;
avgrow = rows + 2;
maxrow = rows + 1;
minrow = rows + 0;

printf("%d", newrow);
// Create array
array = malloc(newrow * sizeof(float));
for(i=0; i < rows; i++)
array[i] = malloc(columns * sizeof(float));

// store array
for(i=0; i <rows; i++){
for(j=0; j<columns; j++){
scanf("%d", &temp);
array[i][j] = temp;
}
}
// Analyze array
for(i=0; i < columns; i++){
sum = 0;
for(j=0; j < rows; j++){
sum += array[j][i];
}
array[avgrow][i] = sum / rows;
}

// print array
for(i=0; i < rows; i++){
for(j=0; j <= columns; j++){
printf("%d\t", array[i][j]);
}
}
printf("\n");

free(array);
return 0;
}```
i put the program through visual C++ and it seems that the problem is starting to look like the avgrow in the array[avgrow][i] part.... am i doing the addition right for moving to the right row of the array?