zadanie 21/ 2
Ostatnio zmodyfikowano 2016-11-20 21:46
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. #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 ] |
|
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?? #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 ); } }
//wracając do twojego kodu propunuje pętle for patrz jak skraca kod i ile mniej kombinowania w tym przypadku bool czy_weszlo( int liczba, int tab[], int lastElementIndex ) { for( int i = 0; i < lastElementIndex; i++ ) { if( tab[ i ] == liczba ) return false; } return true; } |
|
karambaHZP |
» 2016-11-20 21:46:04 Przykład wylosowania trzech liczb spośród dziesięciu liczb użyszkodnika. #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 ) { 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 }; 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/WXX0zbbCihDykcyUew: można skorzystać z rozwiązań wygodniejszych: http://melpon.org/wandbox/permlink/NLI6RZeC6Bh4GivB |
|
« 1 » |