Tie does not perform all it should

0

I have a problem with my program, so I need it to randomly show an order of numbers (teams). The problem is that when I put a number greater than 10 it does not show the 10, it shows only 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'));

}
    
asked by anonymous 26.11.2018 / 02:31

1 answer

3

The problem is accessing the array . You are accessing indexes 1 and 2 from array , but it only goes to index 1. arrays start at 0. Note that in the column you are doing right, since the loop starts at 0, but the line is starting at 1, so one of the dimensions is not being initialized, and this is no problem, because it is never accessed even, but when it tries to write at index 2 it does not have memory reserved for it ends up passing over other areas of memory reserved for something else and then the control number in n ends up being changed unintentionally. This is correct and more organized (you can improve it 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');
}

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

    
26.11.2018 / 12:07