![]() |
| | #1 |
| Registered User Join Date: Apr 2005
Posts: 3
| problems finding the average of an array column 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;
}
Last edited by mrgeoff; 04-18-2005 at 02:25 PM. |
| mrgeoff is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| 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.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #3 |
| Registered User Join Date: Apr 2005
Posts: 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;
}
|
| mrgeoff is offline | |
| | #4 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| 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 */
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #5 |
| Registered User Join Date: Apr 2005
Posts: 3
| 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;
}
Last edited by mrgeoff; 04-18-2005 at 11:54 PM. |
| mrgeoff is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Finding largest number in array | el_chupacabra | C Programming | 2 | 01-22-2009 02:31 PM |
| Dictionary into a 2d Char Array... Problems. | Muzzaro | C Programming | 10 | 12-02-2006 12:34 PM |
| average in an array | s_ny33 | C++ Programming | 2 | 04-22-2005 01:03 PM |
| Class Template Trouble | pliang | C++ Programming | 4 | 04-21-2005 04:15 AM |
| Array sorting problems | cazil | C++ Programming | 2 | 02-04-2002 01:36 PM |