People need help in this exercise. I did the whole exercise and it runs fine only in the end it gives error because of passing the array address to the function.
What's the problem here? Suppose I have to pass the address of the array, make all the modifications and in the end return 2 values (mean of odd and even pairs)
#include <stdio.h>
#include <stdlib.h>
#define N 4
#define M 4
float media_final[2];
int troca_num(int *mat, int n_lin, int n_col);
int main()
{
//MATRIZ DE INTEIROS
int mat[N][M]= {{1,5,4,8},{2,3,7,9},
{6,1,7,4},{9,1,5,3}
};
troca_num(&mat,N,M);
printf("\n\nMedia dos valores impares: %f\n", media_final[0]);
printf("Media dos valores pares:% f\n", media_final[1]);
return 0;
}
int troca_num(int *mat, int n_lin, int n_col)
{
//VARIAVEIS
float media_impares, media_pares, media, soma=0, soma_pares, soma_impares;
int i, j, k = 1, impar=0, par=0;
//CICLO PARA ESCREVER A MATRIZ
printf("MATRIZ ORIGINAL\n\n");
for(i = 0; i < N; i++)
{
for(j = 0; j < M; j++)
{
printf("%d ",mat[i][j]);
}
printf("\n");
}
printf("\n-----------------------\n");
//CICLO PARA FAZER AS MEDIAS
for(i = 0; i < N; i++)
{
for(j = 0; j < M; j++)
{
soma += mat[i][j];
media = soma/16;
}
}
//CICLO PARA TROCAR OS NUMEROS MAIORES QUE A MEDIA
printf("\n\nMATRIZ MODIFICADA\n\n");
for(i = 0; i < N; i++)
{
for(j = 0; j < M; j++)
{
if(media < mat[i][j])
{
mat[i][j] = k;
k++;
}
printf("%d ",mat[i][j]);
}
printf("\n");
}
//MEDIA DOS PARES E IMPARES NA MATRIZ MODIFICADA
for(i = 0; i < N; i++)
{
for(j = 0; j < M; j++)
{
if(mat[i][j] % 2 == 0)
{
par++;
soma_pares = soma_pares + mat[i][j];
}
else
{
impar++;
soma_impares = soma_impares + mat[i][j];
}
}
}
media_pares = soma_pares/par;
media_impares = soma_impares/impar;
media_final[0]=media_impares;
media_final[1]=media_pares;
printf("\n\nPARES: %d\n",par);
printf("IMPARES: %d",impar);
return media_final;
}