Copy and sort matrix in C

1

I am learning C but I am still very new. I face the following problem: given a square matrix I must copy it in another matrix and order it as follows: For example, if I work with this input matrix:

{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,1,2,1,1,0,0,0,0}
{1,1,2,0,1,0,0,0,0}
{2,2,1,0,2,1,0,0,0}
{1,1,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}

I should get the following output:

{1,2,1,1,1,0,0,0,0}
{1,1,2,1,0,0,0,0,0}
{2,2,1,2,1,0,0,0,0}
{1,1,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}

I have tried changing the indexes of the matrix, "rising" the values 4 positions upwards but it does not work. The solution must serve for any square matrix. I leave the code that I have worked that works for a 3x3 matrix but with this 9x9 it does not work. I hope you can help me to see what I'm wrong about.

#include <stdio.h>
int main(){
int matriz[9][9]={{0,0,0,0,0,0,0,0,0},
                  {0,0,0,0,0,0,0,0,0},
                  {0,0,0,0,0,0,0,0,0},
                  {0,0,0,0,0,0,0,0,0},
                  {0,1,2,1,1,0,0,0,0},
                  {1,1,2,0,1,0,0,0,0},
                  {2,2,1,0,2,1,0,0,0},
                  {1,1,0,0,0,0,0,0,0},
                  {0,0,0,0,0,0,0,0,0},
                };
int matriz2[9][9]={{0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0},
                   {0,0,0,0,0,0,0,0,0},
                };
int m=0;
int n=0;
int i=0;
int j=0;
while(i<9){
    while(j<9){
        if (matriz[i][j]!=0)
        {
            while(matriz[i][j]!=0 && j < 9)
            {
                matriz2[m][n]=matriz[i][j];
                n++;
                j++;
            }
            if(j == 9)
                m++;
                n=0;
        }
        else
            j++;
    }
    i++;
    j=0;
}
for (int i = 0; i < 9; ++i)
{
    for (int j = 0; j < 9; ++j)
    {
        printf("[%d]",matriz2[i][j] );
    }
    printf("\n");
}
}

this code returns me:

{1,1,1,1,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
{0,0,0,0,0,0,0,0,0}
    
asked by Tamos 05.01.2019 в 05:44
source

1 answer

1

A possible solution to your problem is:

#include <stdio.h>

const int entrada[][9] = {
  {0,0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0,0},
  {0,1,2,1,1,0,0,0,0},
  {1,1,2,0,1,0,0,0,0},
  {2,2,1,0,2,1,0,0,0},
  {1,1,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0,0}
};

int salida[9][9] = { 0 };

int main( ) {
  int fila_salida = 0;

  for( int fila_entrada = 0; fila_entrada < 9; ++ fila_entrada ) {
    int col_salida = 0;
    int hay_datos = 0;
    for( int col_entrada = 0; col_entrada < 9; ++ col_entrada ) {
      if( entrada[fila_entrada][col_entrada] ) {
        hay_datos = 1;
        salida[fila_salida][col_salida] = entrada[fila_entrada][col_entrada];
        ++col_salida;
      } else if( hay_datos ) {
        ++col_salida;
      }
    }
    if( hay_datos ) ++ fila_salida;
  }

  for( fila_salida = 0; fila_salida < 9; ++fila_salida ) {
    for( int col_salida = 0; col_salida < 9; ++col_salida ) {
      printf( "%d ", salida[fila_salida][col_salida] );
    }
    printf( "\n" );
  }

  return 0;
}

You will see that there are many changes regarding your code:

  • Variables with understandable names.
  • We limit the scope of the variables to the minimum.
  • Initialize the formation salida more simply, filling it with 0 .
  • We use for( ) , which now compares us later and leaves the code clearer and cleaner.
answered by 05.01.2019 / 06:03
source