Arranged Two-dimensional Array

0

I have a code made in C in which you enter data in a two-dimensional array and sort the rows, I also want you to order the columns

for example you enter: 9 8 7 6 5 4 3 2 1 and it looks like this: 7 8 9 4 5 6 1 2 3 that really is fine because I ordered each row, but I can not do it for the columns, make you visualize 1 2 3 4 5 6 7 8 9

I leave the code I have:

void introducirMatriz();
void visualizarMatriz();
void ordenarFilas();
void ordenarColumnas();

//Variable
int matriz[3][3] , aux;

int main()
{
    printf("INTRODUCE DATOS EN LA MATRIZ BIDIMENSIONAL\n\n");

    introducirMatriz();
    printf("\n  Matriz introducida\n\n");
    visualizarMatriz();
    ordenarFilas();
    printf("\n  Matriz ordenada\n\n");
    visualizarMatriz();

    return 0;
}

void introducirMatriz()
{   
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            printf("[%d][%d]: ",i,j);
            scanf("%d",&matriz[i][j]);
        }
    }
}

void visualizarMatriz()
{
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            printf("    %d",matriz[i][j]);          
        }
        printf("\n");
    }
}

void ordenarFilas()
{
    for(int i=0;i<3;i++) //Metodo para ordenar la primera fila
    {
        for(int j=0;j<3-1-i;j++)
        {
            if(matriz[0][j]>matriz[0][j+1])
            {
                aux=matriz[0][j];
                matriz[0][j]=matriz[0][j+1];
                matriz[0][j+1]=aux;
            }
        }   
    }

    for(int i=0;i<3;i++) //Metodo para ordenar la segunda fila
    {
        for(int j=0;j<3-1-i;j++)
        {
            if(matriz[1][j]>matriz[1][j+1])
            {
                aux=matriz[1][j];
                matriz[1][j]=matriz[1][j+1];
                matriz[1][j+1]=aux;
            }
        }   
    }

    for(int i=0;i<3;i++) //Metodo para ordenar la tercera fila
    {
        for(int j=0;j<3-1-i;j++)
        {
            if(matriz[2][j]>matriz[2][j+1])
            {
                aux=matriz[2][j];
                matriz[2][j]=matriz[2][j+1];
                matriz[2][j+1]=aux;
            }
        }   
    }
}
    
asked by Mario Guiber 17.08.2017 в 00:15
source

1 answer

1

Well, here is the solution: here they are ordered from highest to lowest if you want them to be ordered from lowest to highest just change the > in this instruction

matrix [i] [j] > matrix [x] [y]

try it and I hope it serves you;

#include <stdio.h>

void introducirMatriz();
void visualizarMatriz();
void ordenarFilas();
void ordenarColumnas();

//Variable
int matriz[3][3] , aux;

int main()
{
printf("INTRODUCE DATOS EN LA MATRIZ BIDIMENSIONAL\n\n");

introducirMatriz();
printf("\n  Matriz introducida\n\n");
visualizarMatriz();
ordenarFilas();
printf("\n  Matriz ordenada\n\n");
visualizarMatriz();

return 0;
}

void introducirMatriz()
{
int i,j;
for(i=0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
        printf("[%d][%d]: ",i,j);
        scanf("%d",&matriz[i][j]);
    }
 }
}

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

 void ordenarFilas()
{
 int i, j , x ,y;
 int mayor =0;
 for(i=0; i<3; i++)
 {
    for(j=0; j<3; j++)
    {
        for(x=0; x<3;x++)
        {
            for(y=0; y<3; y++)
            {
                if(matriz[i][j]<matriz[x][y])
                {
                    mayor=matriz[i][j];
                    matriz[i][j]=matriz[x][y];
                    matriz[x][y]=mayor;
                }

            }
        }
     }
  }
 }
    
answered by 17.08.2017 / 04:15
source