grodek118

Losowanie bez powtorzen

Aug 24th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. bool czy_byla_wylosowana (int iLiczba, int tab[], int ile)
  8. {
  9. if( ile <= 0)
  10. return false;
  11.  
  12. int i = 0;
  13. do
  14. {
  15. if (tab [i] == iLiczba)
  16. return true;
  17. i++;
  18. } while ( i < ile );
  19.  
  20. return false;
  21. }
  22.  
  23. int wylosuj()
  24. {
  25. return(rand () % 10) + 1;
  26. }
  27.  
  28.  
  29. int main()
  30. {
  31. srand( time(0) );
  32. int wylosowane[5];
  33. int wylosowanych = 0;
  34. do
  35. {
  36. int liczba = wylosuj();
  37. if( czy_byla_wylosowana(liczba, wylosowane, wylosowanych) == false)
  38. {
  39. wylosowane[wylosowanych] = liczba;
  40. wylosowanych++;
  41. }
  42. } while (wylosowanych < 5);
  43.  
  44. wylosowanych = 0;
  45. do
  46. {
  47. cout << wylosowane[wylosowanych] << endl;
  48. wylosowanych++;
  49. }while (wylosowanych < 5 );
  50.  
  51. return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment