Program does not execute when filling vector with non-repeated random numbers in C

Asked

Viewed 146 times

-1

I’m trying to get random numbers to be generated and fill in a vector in a separate function, but the program doesn’t run and crash if I put a high number, and not as high as 50 or 100. Below follows my code, if anyone knows what is wrong help me there, pfv.

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

//variáveis globais
int tamanho = 0;

//links das funções
int menuPrincipal();
int retornaTamanho();
void preencheVetor(int tamanhoVetor, int *vetor);

int main(){
    setlocale(LC_ALL, "Portuguese");

    int opcao = 0, vetor[tamanho];

    do{
        opcao = menuPrincipal();

        switch(opcao){
        case 0:
            printf("\n ----------- APLICAÇÃO ENCERRADA! ----------- \n");
            exit(0);
            break;
        case 1:
            tamanho = retornaTamanho();

            printf("\n Tamanho do Vetor = %d \n", tamanho);

            preencheVetor(tamanho, vetor);

            break;
        default:
            printf("\n ----------- OPÇÃO INVÁLIDA ----------- \n\n");
            system("pause");
            system("cls");
            break;
        }
    }while(true);

    return 0;
}

int menuPrincipal(){
    int opcaoMenu = 0;

    printf("\n ------------ MENU ------------ \n");
    printf("\n ESCOLHA O MÉTODO DE ORDENAÇÃO: \n");
    printf("\n 1 - BUBBLE SORT \n");
    printf("\n 2 - SELECT SORT \n");
    printf("\n 3 - INSERT SORT \n");
    printf("\n 0 - SAIR \n");
    printf("\n DIGITE SUA OPÇÃO = ");
    scanf("%d", &opcaoMenu);

    system("cls");

    return opcaoMenu;
}

int retornaTamanho(){
    int opcaoTamanho = 0, valorTamanho = 0;

    printf("\n ------- TAMANHO DO VETOR -------- \n");
    printf("\n ESCOLHA O TAMANHO DO VETOR: \n");
    printf("\n 1 - 100 \n");
    printf("\n 2 - 10.000 \n");
    printf("\n 3 - 100.000 \n");
    printf("\n 4 - 1.000.000 \n");
    printf("\n 0 - VOLTAR \n");
    printf("\n DIGITE SUA OPÇÃO = ");
    scanf("%d", &opcaoTamanho);

    system("cls");

    switch(opcaoTamanho){
    case 0:
        main();
        break;
    case 1:
        valorTamanho = 100;
        break;
    case 2:
        valorTamanho = 10000;
        break;
    case 3:
        valorTamanho = 100000;
        break;
    case 4:
        valorTamanho = 1000000;
        break;
    default:
        retornaTamanho();
        break;
    }

    return valorTamanho;
}

void preencheVetor(int tamanhoVetor, int *vetor){
    int randomico = 0;

    bool numExistente = false;

    srand(time(NULL));

    for(int i = 0; i < tamanhoVetor; i++){

        randomico = (int) rand() % tamanhoVetor;

        for(int j = 0; j < i; j++){
            if(randomico == vetor[j]){
                numExistente = true;
                break;
            }
        }

        if(!numExistente){
            vetor[i] = randomico;
        }else{
            i--;
        }
    }
}

1 answer

0


When you create a vector in a static way (without using malloc or things like that), its size is set at the time of creation of the variable.

Try changing your code to something like this :

#define MAX 1000000
/* código omitido */

int main() {
    int vetor[MAX];
    /* o resto do código */
    return 0;
}

This way, the vector will be created with the size MAX (one million) and there will be no problems with your code context size

  • Thank you for answering! But @Jeffersonquesado, this size of the vector will vary according to what the user defines in the function returnSize(), because I need to do tests with various sizes of values, for my evaluation. As I would then?

  • The vector is only created once. Therefore, it will take on the value tamanho from the beginning of the code, which is guaranteed to be 0. This is an example of a static vector, not a dynamic vector. For dynamic vectors, you will need to tinker with memory allocation, which is a slightly pposterior topic to use static memory ;-)

  • I think I get it. Then I’ll have to use that malloc function?

  • You’re starting to program now, aren’t you? I suggest you get used to static allocation and variable scope before you take the malloc

  • More or less. I’m messing with C now because of college. But I’ve programmed with C# and Delphi (Object Pascal), but they didn’t have those things there.

  • It may be interesting to deepen : http://wiki.icmc.usp.br/images/5/59/Aula14-AlocacaoDinamica.pdf. https://www.ime.usp.br/~pf/algorithms/lessons/aloca.html

  • 1

    Thanks, buddy! I’ll take a look. Thanks for the help.

  • Any upvote is welcome/

Show 3 more comments

Browser other questions tagged

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