Exchanging rows from an array

1

Below is my code. I need to change line 1 to line 3 and line 2 to line 4. The problem is that the third line is not changing and I do not find the problem.

    int matriz[TAM][TAM], n = 0, x = 0, y = 0, aux = 0, aux2 = 0, aux3 = 0;

    printf("Informe um tamanho par para sua matriz:  \n");
    scanf("%d", &n);

    for(x = 0; x < n; x++){
        for(y = 0; y < n; y++){

            printf("Preencha a matriz: \n");
            scanf("%d", &aux2);

            matriz[x][y]=aux2;

       }
    }

    for(x = 0; x < n; x++){
        for(y = 0; y < n; y++){
                if (x<=(n/2)){

                aux = matriz[x][y];
                matriz[x][y] = matriz[x+2][y];
                matriz[x+2][y] = aux;
                printf("%d ", matriz[x][y]);
                }

                if ( x==3 ||(x>(n/2)))
                {
                aux3 = matriz[x][y];
                matriz[x][y] = matriz[x-2][y];
                matriz[x-2][y] = aux3;
                printf("%d ", matriz[x-2][y]);
                }

        }

    printf("\n");
    }
   }
    
asked by anonymous 22.01.2018 / 14:31

1 answer

1

Instead of x + 2 and x - 2 that will only work if 4 is reported for n , use x + n / 2 and x - n / 2 .

What is repeated within the two if s can be moved to before or after them.

Remember that the first index is zero. This means that your if (x<=(n/2)) , in an array of four rows, will go to the third row, not the second row. What you wanted was to use < instead of <= .

Instead of using if ( x==3 ||(x>(n/2))) , just use else . Also, due to the previous item, it should be >= instead of > , which would eliminate the need for this x==3 .

No printf of first if , you use matriz[x][y] , but not matriz[x-2][y] .

You also do not need a lot of variables aux , only one is needed.

Here's how your code gets with these fixes:

int matriz[TAM][TAM], n = 0, x = 0, y = 0;

printf("Informe um tamanho par para sua matriz:  \n");
scanf("%d", &n);

for (x = 0; x < n; x++) {
    for (y = 0; y < n; y++) {
        printf("Preencha a matriz: \n");
        scanf("%d", &matriz[x][y]);
   }
}

for (x = 0; x < n; x++) {
    for (y = 0; y < n; y++) {
        int aux = matriz[x][y];
        if (x < n / 2) {
            matriz[x][y] = matriz[x + n / 2][y];
            matriz[x + n / 2][y] = aux;
        } else {
            matriz[x][y] = matriz[x - n / 2][y];
            matriz[x - n / 2][y] = aux;
        }
        printf("%d ", matriz[x][y]);
    }

    printf("\n");
}

You can simplify your code a bit more and unify if and else by verifying that the line position you want to change (let's call nx ) can be calculated from x like this:

int nx = (x + n / 2) % n;

To see how this works, see that being n equals 4, we will have to:

  • If x is 0, nx will be 2.
  • If x is 1, nx will be 3.
  • If x is 2, nx will be 0.
  • If x is 3, nx will be 1.

This will work for any other peer value of n .

In this way, your code looks like this:

int matriz[TAM][TAM], n = 0, x = 0, y = 0;

printf("Informe um tamanho par para sua matriz:  \n");
scanf("%d", &n);

for (x = 0; x < n; x++) {
    for (y = 0; y < n; y++) {
        printf("Preencha a matriz: \n");
        scanf("%d", &matriz[x][y]);
   }
}

for (x = 0; x < n; x++) {
    for (y = 0; y < n; y++) {
        int nx = (x + n / 2) % n;
        int aux = matriz[x][y];
        matriz[x][y] = matriz[nx][y];
        matriz[nx][y] = aux;
        printf("%d ", matriz[x][y]);
    }

    printf("\n");
}
    
22.01.2018 / 17:02