Pass pointer array to function

Asked

Viewed 515 times

0

I need some help with this exercise. I did the whole exercise and runs well only that in the end gives error because of passing the address of the matrix to the function.

What’s the problem here? Supposedly I have to pass the address of the matrix, make all the modifications and at the end return 2 values (average of odd and average of 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;
}

1 answer

1

Has been answered here!

In your code just change the function troca_num for:

int troca_num(int mat[][M], int n_lin, int n_col);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.