[Lekcja 17] Zad. 4
Ostatnio zmodyfikowano 2012-12-12 19:47
Precel Temat założony przez niniejszego użytkownika |
[Lekcja 17] Zad. 4 » 2012-12-11 21:32:09 Po przeszukaniu googli i forum wciąż nie wiem jak rozwiązać zadanie domowe, otóż mam za pomocą funkcji mam wylosować liczby od a do b i nijak mi to wychodzi, proszę o pomoc. #include <iostream> #include <cstdlib> #include <ctime> int wylosuj( int start, int stop ) { return( rand() %( start - stop + 1 ) + stop ); } int main() { srand( time( NULL ) ); int start; int stop; std::cin >> start; std::cin >> stop; int ile = 5; do { std::cout << wylosuj( start, stop ) << std::endl; ile--; } while( ile > 0 ); return 0; }
|
|
Mrovqa |
» 2012-12-11 21:36:58 int wylosuj( int start, int stop ) { return( rand() %( start - stop + 1 ) + stop ); } |
|
jsc |
» 2012-12-11 21:38:39 start - stop + 1 to przesada sam start powinien starczyć. |
|
Mrovqa |
» 2012-12-11 21:45:09 @jsc nie mieszaj, jeśli nie wiesz co piszesz. start to dolna granica przedziału, a stop to górna granica. Autor spokojnie znajdzie błąd :) |
|
Precel Temat założony przez niniejszego użytkownika |
» 2012-12-12 19:47:35 Dzięki za pomoc, kod gdyby potomni potrzebowali #include <iostream> #include <cstdlib> #include <ctime> int wylosuj( int start, int stop ) { stop = start - stop + 1; return( rand() % stop ) + start; } int main() { srand( time( NULL ) ); int start; int stop; std::cin >> start; std::cin >> stop; int ile = 5; do { std::cout << wylosuj( start, stop ) << std::endl; ile--; } while( ile > 0 ); return 0; }
|
|
« 1 » |