Doubts with dynamic lists

Asked

Viewed 50 times

0

In case I need to generate 5 random numbers, and insert them in a dynamic list, I’ve been trying, but the most I can is generate them and display them

follows code:

#include <iostream>
#include <cstdlib>//Biblioteca responsavel por numeros aleatorios
#include <ctime>//Biblioteca responsavel por numeros aleatorios
#include <list>//Biblioteca responsavel por criar listas

#define PCT 5
using namespace std;

typedef struct Album{
    int figura;
    Album *prox;
/*  list<int> album;
    int tam;

    tam=681;

    for(int i=tam; i>0; i--){
    album.push_front(i);
    }
    cout<<"Album da copa 2018 - total "<<album.size()<<" figurinhas"<<endl<<endl;

    tam=album.size();
    for(int i=0; i < tam; i++){
        cout<<album.front()<<endl;
        album.pop_front();
    }*/
};
    Album *inicio = NULL;
    Album *fim = NULL;
    Album *aux;
    Album *anterior;

void Pacote(){
    system("cls");

    srand((unsigned)time(0)); //para gerar numeros aleatorios reais.
    for (int i=0; i<PCT; i++){
        int maior = 681;
        int menor = 1;
        int Figurinha = rand()%(maior-menor+1) + menor;
        cout << Figurinha << endl;  }
        //Valida();

    Album *novo = new Album();
    cin>>novo->figura;
        if (inicio==NULL){
            inicio = novo;
            novo->prox=NULL;
        }
        else{
            anterior=NULL;
            aux=inicio;
            while (aux != NULL && novo -> figura > aux->figura){
                anterior = aux;
                aux = aux->prox;
            }
            if (anterior==NULL){
                novo->prox=inicio;
                inicio=novo;
            }
            else if (aux==NULL){
                fim->prox=novo;
                fim = novo;
                fim->prox=NULL;
            }
            else{
                anterior->prox=novo;
                novo->prox=aux;
            }


        }
}


/*void Valida(int Figurinha, list<int> album){
    if (Figurinha ){

    }*/

void Menu(){
    int x=0;
    system("color F0");
    do{
        cout<<"==========ALBUM DA COPA========="<<endl;
        cout<<"1 - Inserir novas figurinhas"<<endl;
        cout<<"2 - Exibir album"<<endl;

        cout<<endl<<endl<<"Selecione uma das opcoes acima :"<<endl;
        cin>>x;

        switch (x){
            case 1:
                Pacote();
                break;
            case 2:
                Album();
                break;
        }
    }while (x!=5);
}

int main(int argc, char** argv) {
    Menu();
    return 0;
}

1 answer

0

You can do as below:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <utility>
#include <list>

int main() {
    std::srand(std::time(nullptr));
    int quantidade = 5;
    int cap = 682;
    std::list<int> lista;
    float rand_number;
    for(int i=0; i<quantidade; ++i) {
        rand_number = rand()%cap;
        lista.push_front(std::move(rand_number));
    }
    for (const int& l : lista)
        std::cout << l << ' ';
    std::cout << '\n';
    return 0;
}

Browser other questions tagged

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