Average numbers of a matrix in C

0

I need to create a program that builds an array and returns me the average of its numbers.

I already have the array built but I can not average the values.

The code I already have:

#include<stdio.h>
#include<stdlib.h>

int main(void){
    int matriz [3][3] ={{0}}, i, j;
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            printf("introduza numeros para a matriz nos lugares [%d][%d] \n", i+1, j+1);
            scanf("%d", &matriz[i][j]);
        }
    }
    printf("\n\t");
    printf("estes sao os valores da matriz\n\n");
    printf("\t\t matriz ordenada");
    for(i=0; i<3; i++){
        printf("\n");
        for(j=0;j<3;j++){
            printf("%6d", matriz[i][j]);
        }
    }
printf("\n");

}
    
asked by anonymous 16.07.2017 / 03:11

1 answer

1

You have to go through the whole matrix and add the numbers, then just divide by the number of elements, which is the mean formula.

#include<stdio.h>

int main(void) {
    int matriz [3][3] = {{0}};
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("introduza numeros para a matriz nos lugares [%d][%d] \n", i+1, j+1);
            scanf("%d", &matriz[i][j]);
        }
    }
    printf("\n\testes sao os valores da matriz\n\n");
    printf("\t\t matriz ordenada");
    int soma = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
           soma += matriz[i][j];
        }
    }
    printf("\nMédia: %d", soma / 9);
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

    
16.07.2017 / 03:23