C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-18-2005, 02:14 PM   #1
Registered User
 
Join Date: Apr 2005
Posts: 3
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.

Last edited by mrgeoff; 04-18-2005 at 02:25 PM.
mrgeoff is offline   Reply With Quote
Old 04-18-2005, 05:49 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 04-18-2005, 07:43 PM   #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;
}
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.
mrgeoff is offline   Reply With Quote
Old 04-18-2005, 11:47 PM   #4
+++ OK NO CARRIER
 
quzah's Avatar
 
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 */
Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 04-18-2005, 11:49 PM   #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;
}
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?

Last edited by mrgeoff; 04-18-2005 at 11:54 PM.
mrgeoff is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:13 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22