Thread: problems finding the average of an array column

  1. #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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #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.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding largest number in array
    By el_chupacabra in forum C Programming
    Replies: 2
    Last Post: 01-22-2009, 02:31 PM
  2. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 12:34 PM
  3. average in an array
    By s_ny33 in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2005, 01:03 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Array sorting problems
    By cazil in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2002, 01:36 PM