Accessing matrix row and swap row by column

Asked

Viewed 1,351 times

1

Good afternoon friends, I am trying to make an exchange in an array, exchange the values of the first row with the values of the last column. I know that a row of a matrix in C is a vector. I thought about doing so but as always my logic is wrong.

#define TAMANHO 4

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

void receberMatriz(int matrizPRI[TAMANHO][TAMANHO]);
void ordenarMatriz(int matrizPRI[TAMANHO][TAMANHO]);


    int main(void)
    {
        setlocale(LC_ALL, "");
        int matrizPRI[TAMANHO][TAMANHO];



        receberMatriz(matrizPRI);
        ordenarMatriz(matrizPRI);

        return 0;

    }






void receberMatriz(int matrizPRI[TAMANHO][TAMANHO])
{
    int lin, col;

    for(lin = 0; lin < TAMANHO; lin++)
    {
        for(col = 0; col < TAMANHO; col++)
        {
            printf("Matriz Principal [%i][%i]: ", lin, col);
            scanf("%i", &matrizPRI[lin][col]);
        }
    }
}

void ordenarMatriz(int matrizPRI[TAMANHO][TAMANHO])
{
    int lin, col;
    int AUX1[TAMANHO], AUX2[TAMANHO];

    for(lin = 0; lin < TAMANHO; lin++)
    {
        for(col = 0; col == TAMANHO; col++)
        {
            if(matrizPRI[lin] == 1)
            {
                AUX1[col] = matrizPRI[col];
                AUX1[col]+1;
            }
            else if(matrizPRI[col] == 4)
            {
                AUX2[col] = matrizPRI[col];
                AUX2[col]+1;
            }
        }
    }

    for(lin = 0; lin < TAMANHO; lin++)            
    {
        for(col = 0; col < TAMANHO; col++)
        {
            if(matrizPRI[lin] == 1)
            {
                matrizPRI[lin] = AUX2[col];
            }
            else if(matrizPRI[col == 4])
            {
                matrizPRI[col] == AUX1[lin];
            }
        }
    }

    for(lin = 0; lin < TAMANHO; lin++)
    {
        for(col = 0; col < TAMANHO; col++)
        {
            printf("[%i] "), matrizPRI[lin][col];
        }

        printf("\n");

    }
}

How can I exchange a row of an array with a column? The switch would be made from a single row and a single column, so I do not believe to use Bubble Sort. Someone gives a light to clear this mind...

  • what exchanging the values of the first row with the values of the last column has to do with Bubble Sort? Your question is a bit confusing. Can provide an example of the input matrix and the expected result for that matrix?

  • I’m really going to try to rephrase the question, I was kind of hot-headed for not finding the logic. I will put the statement of the question: Construct an algorithm to read a 4 x 4 matrix and exchange the values of the 1st row for those of the 4th column, vice versa. Write the obtained matrix at the end.

  • You can do something recursive, thus taking control of each element of the row and column to be able to exchange them safely. Recursion for when you cannot find the next row/column element.

1 answer

0


From what I understand of the problem the answer is quite simple and does not need the algorithm of Buble Sort.

#define TAMANHO 4
#include <stdio.h>

int main(){

int matriz[TAMANHO][TAMANHO];

int i, j, aux;

//recebe os valores dos elementos da matriz
for(i = 0; i < TAMANHO; i++){
    for(j = 0; j < TAMANHO; j++){
        scanf("%d", &matriz[i][j]);
    }
}

//faz a troca
for(i = 0; i < TAMANHO; i++){
    aux = matriz[0][i];
    matriz[0][i] = matriz[TAMANHO - i -1][TAMANHO - 1];
    matriz[TAMANHO - i -1][TAMANHO - 1] = aux;

}

//exibe a matriz após a troca
for(i = 0; i < TAMANHO; i++){
    for(j = 0; j < TAMANHO; j++){

        printf("%d", matriz[i][j]);
        printf(" ");
    }
    printf("\n");
}
}

Browser other questions tagged

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