Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

zadanie 21/ 2

Ostatnio zmodyfikowano 2016-11-20 21:46
Autor Wiadomość
cpunejroo
Temat założony przez niniejszego użytkownika
zadanie 21/ 2
» 2016-11-20 18:57:13
mam pewien problem a mianowicie
nie wiem jak zrobić aby liczby tablicy które są losowane się nie powtarzały.


C/C++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
bool czy_weszlo( int a, int tab[], int b )
{
    if( b <= 0 )
         return true;
   
    int i = 0;
    do
    {
       
        if( tab[ i ] == a )
             return false;
       
        i++;
       
    } while( i < b );
   
    return true;
}
int losujenumertab( int max, int min )
{
   
    return( rand() % max ) + min;
}

int liczby_user( int c )
{
    cin >> c;
    return c;
   
}
int main()
{
    srand( time( 0 ) );
    int losowana = 0;
    int tablica[ 10 ];
    int c;
   
   
   
    do {
       
        int liczba = liczby_user( c );
       
        if( czy_weszlo( liczba, tablica, losowana ) == true )
        {
            tablica[ losujenumertab( 9, 0 ) ] = liczba;
           
            losowana++;
           
           
        }
       
    } while( losowana < 10 );
   
   
   
    cout << "| ";
    int as = 0;
    do
    {
       
        cout << setw( 2 ) << tablica[ as ] << " | ";
        as++;
    } while( as < 8 );
   
    cout << endl; cout << endl;
   
    cout << endl;
    return( 0 );
}[ \c pp ]
za wszystkie odpowiedzi z g ó ry dzi ę kuje: )
[ / i ]
P-153979
mateczek
» 2016-11-20 20:35:06
A co ty w tym kodzie chcesz zrobić??
Chcesz wypełnić tablicę liczbami od użytkownika następnie z tej tablicy losować ??
czy liczby od użytkownika mogą się powtarzać czy są unikatowe??
C/C++
//tak czy siak najprościej użyć wektor i usuwać wylosowany index. powtarzające się liczby od użytkownika nie stanowią problemów
#include <iostream>
#include<vector>
#include<ctime>
using namespace std;

int main()
{
    srand( time( 0 ) );
    vector < int > pula( 10 );
    for( int & element: pula ) {
        cout << "podaj liczbe: ";
        cin >> element;
    }
    for( int i = 0; i < 6; i++ ) {
        int index = rand() % pula.size();
        cout << pula[ index ] << " ";
        pula.erase( pula.begin() + index ); //skasuj wylosowaną z puli
    }
}

//wracając do twojego kodu propunuje pętle for patrz jak skraca kod i ile mniej kombinowania w tym przypadku

C/C++
bool czy_weszlo( int liczba, int tab[], int lastElementIndex ) {
    for( int i = 0; i < lastElementIndex; i++ ) {
        if( tab[ i ] == liczba ) return false;
       
    }
    return true;
}
P-153982
karambaHZP
» 2016-11-20 21:46:04
Przykład wylosowania trzech liczb spośród dziesięciu liczb użyszkodnika.
C/C++
#include <iostream>
#include <numeric>
#include <cstdlib>
#include <ctime>

int getNum()
{
    int num { };
    while( !( std::cin >> num ) ) {
        std::cin.clear();
        std::cin.ignore( std::numeric_limits < std::streamsize >::max(), '\n' );
    }
   
    return num;
}

bool isFindIndex( std::size_t index, std::size_t arr[], std::size_t currentSize )
{
    if( currentSize <= 0 )
         return false;
   
    std::size_t i { };
    do {
        if( index == arr[ i++ ] )
             return true;
       
    } while( i < currentSize );
   
    return false;
}

int randomNum( std::size_t start, std::size_t stop )
{
    return std::rand() %( stop - start + 1 ) + start;
}

void fillCollection( int arr[], std::size_t size )
{
    // równoważne std::size_t i = 0;
    for( std::size_t i { }; i < size; ++i ) {
        arr[ i ] = getNum();
    }
}

void fillIndexes( std::size_t indexes[], std::size_t size, std::size_t start, std::size_t stop )
{
    std::size_t i { };
    do {
        std::size_t randIndex = randomNum( start, stop );
        if( !isFindIndex( randIndex, indexes, i ) ) {
            indexes[ i++ ] = randIndex;
        }
    } while( i < size );
   
}

void printCollection( int arr[], std::size_t size )
{
    for( std::size_t i { }; i < size; ++i ) {
        std::cout << arr[ i ] << ' ';
    }
    std::cout << '\n';
}

void printRandNums( int randNums[], std::size_t randIndex[], std::size_t randIndexSize )
{
    for( std::size_t i { }; i < randIndexSize; ++i ) {
        std::cout << randNums[ randIndex[ i ] ] << ' ';
    }
    std::cout << '\n';
}

int main()
{
    std::srand( std::time( nullptr ) );
    constexpr std::size_t size { 10 }; // równoważne const std::size_t size = 10;
    constexpr std::size_t randIndexesSize { 3 };
    int userNums[ size ];
    std::size_t randIndexes[ randIndexesSize ];
    fillCollection( userNums, size );
    printCollection( userNums, size );
    fillIndexes( randIndexes, randIndexesSize, 0, size - 1 );
    printRandNums( userNums, randIndexes, randIndexesSize );
}
Kod jest podzielony na małe funkcje, zajmujące się jedną określoną czynnością.
http://melpon.org/wandbox​/permlink/WXX0zbbCihDykcyU

ew: można skorzystać z rozwiązań wygodniejszych:
http://melpon.org/wandbox​/permlink/NLI6RZeC6Bh4GivB
P-153983
« 1 »
  Strona 1 z 1