Loop does not perform everything it should

Asked

Viewed 97 times

1

I have a problem in my program, I need it to show randomly an order of numbers (teams). The problem is that when I put a number greater than 10 it doesn’t show the 10, it only shows a few:

   int cont1, aux, n, sorteio;
    char op = 's';



setlocale(LC_ALL, "portuguese");

do{
    printf("Digite a quintidade de equipes para serem sorteadas: ");
    scanf("%d", &n);
    printf("\n--------------------------------------------------------------------------------------------\n\n");

    int matriz[2][n];

    for(cont1 = 0; cont1 < n; cont1++){
        matriz[1][cont1] = cont1 + 1;
        matriz[2][cont1] = cont1 + 1;
    }

    srand(time(NULL));

    for(cont1 = 0; cont1 < n; cont1++){
        sorteio = rand() % n;
        aux = matriz[2][cont1];
        matriz[2][cont1] = matriz[2][sorteio];
        matriz[2][sorteio] = aux;

    }

    for(cont1 = 0; cont1 < n; cont1++){
        printf("Linha 1 da MATRIZ - Ordem das Apresentações: %d   |   ", matriz[1][cont1]);
        printf("Linha 2 da MATRIZ - Número da Equipe: %d", matriz[2][cont1]);
        printf("\n");

    }
    printf("\n--------------------------------------------------------------------------------------------\n\n");
    printf("Deseja realizar outro sorteio? (S/N)> ");

    fflush(stdin);

    op = getchar();

    fflush(stdin);

    printf("\n--------------------------------------------------------------------------------------------\n\n");

}while((op == 'S') | (op == 's'));

}
  • You have declared int matrix[2][n]; and therefore the first index must be 0 or 1 and not 1 or 2 as used.

1 answer

6


The problem is access to array. You are accessing index 1 and 2 of array, ma it only goes up to index 1. arrays start at 0. Note that in the column you are doing it right, since the loop starts at 0, but in the line it is starting at 1, so one of the dimensions is not being initialized, and this is no problem, because it is never accessed anyway, but when you try to write in index 2 that has no memory reserved for it ends up passing over other areas of memory reserved for something else and then the control number, in case n ends up being changed unintentionally. So it is correct and more organized (can improve more):

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

int main(void) {
    int n;
    char op = 's';
    do {
        printf("Digite a quantidade de equipes para serem sorteadas: ");
        scanf("%d", &n);
        printf("\n--------------------------------------------------------------------------------------------\n");
        int matriz[2][n];
        for (int cont1 = 0; cont1 < n; cont1++) {
            matriz[0][cont1] = cont1 + 1;
            matriz[1][cont1] = cont1 + 1;
        }
        srand(time(NULL));
        for (int cont1 = 0; cont1 < n; cont1++) {
            int sorteio = rand() % n;
            int aux = matriz[1][cont1];
            matriz[1][cont1] = matriz[1][sorteio];
            matriz[1][sorteio] = aux;
        }
        for (int cont1 = 0; cont1 < n; cont1++) {
            printf("Linha 1 da MATRIZ - Ordem das Apresentações: %d   |   ", matriz[0][cont1]);
            printf("Linha 2 da MATRIZ - Número da Equipe: %d\n", matriz[1][cont1]);
        }
        printf("\n--------------------------------------------------------------------------------------------\n\n");
        printf("Deseja realizar outro sorteio? (S/N)> ");
        op = getchar();
        printf("\n--------------------------------------------------------------------------------------------\n\n");
    } while (op == 'S' || op == 's');
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Your diagnosis would have been right 30 years ago. Most likely, as modern compilers assume that there is no undefined behavior in their code, optimizations have seen that the loops access a non-existent place, causing UB. So the compiler concludes that it is impossible for these links to perform. But even that can change over time. Anyway, the technically correct diagnosis is: "access outside the limits causes undefined behavior and after that your entire program is rendered useless and anything can happen."

  • And you’re wrong now? Show the error. I’m helping a person with a problem, I’m not writing a thesis.

  • Maniero, I just meant that the reasons may be various due to the advancement in compiler optimizations. You cited one of many possible side effects as being the only possible one. For example, showing the output without and with optimizations, we see the effect that UB causes in the program: https://godbolt.org/z/ODncL2 I believe that technically correct answers are more useful, because many people will end up reading this thread.

Browser other questions tagged

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