Passing matrix to function by reference

Asked

Viewed 697 times

0

Good morning everyone, I wish in my code to leave the Main(); only with calling functions, all work is divided into small functions in the document.

I want to write a matrix[3][3] in a function and organize and print in other distinct functions. My code aims to transpose a matrix, I believe my logic is correct, using Bubble Sort but the parameter passage is not working properly since I am using pass by value and no reference...

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

void receberMatriz(int matriz[3][3])
{
    int i, j;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("Insira o valor de [%i][%i]: ", i, j);
            scanf("%i", &matriz[i][j]);
        }
    }

}



void organizarMatriz(int matriz[3][3])
{
    int i, j, aux;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            aux = matriz[i][j];
            matriz[i][j] = matriz[j][i];
            matriz[j][i] = aux;
        }
    }
}

void imprimirMatriz(int matriz[3][3])
{
    int i, j;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("[%i][%i]", i, j);
        }

        printf("\n");

    }
}



    int main(void)
    {
        int matriz[3][3];

        receberMatriz(matriz);
        imprimirMatriz(matriz);
        //organizarMatriz(matriz); desabilitei chamada para testar valores recebidos e impressos pela matriz. 

        return 0;
    }

How can I work with passing parameters of a matrix by reference? In other words, pointers...?

  • There seems to be only logic error: https://ideone.com/cBUnXo

  • Really, after your comment I looked at the code more carefully, thank you very much master!!!

1 answer

1

If anyone has a similar question I will leave the answer. The logic error that our collaborator Maniero commented is the following:

I switched the balls when writing the matrix printing function.

void imprimirMatriz(int matriz[3][3])
{
    int i, j;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("[%i][%i]", i, j);
        }

        printf("\n");

    }
}

In the printf `("[%i][%i]", i, j); besides putting two whole data output I was printing the index value and not the matrix. The correct is the following code:

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

void receberMatriz(int matriz[3][3])
{
    int i, j;

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("Insira o valor de [%i][%i]: ", i, j);
            scanf("%i", &matriz[i][j]);
        }
    }

}



void imprimirMatriz(int matriz[3][3])
{
    int i, j;

    printf("\n");
    limparTela();

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("[ %i ]", matriz[i][j]);
        }

        printf("\n");

    }
}



void limparTela()
{
    system("cls");
}



    int main(void)
    {
        int matriz[3][3];

        receberMatriz(matriz);
        imprimirMatriz(matriz);

        return 0;
    }

Credits to Maniero

Browser other questions tagged

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