Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5. bool czyUnikalna(int liczba, int liczby[], int podane)
  6. {
  7.  
  8.     int i = 0;
  9.     while(i < podane)
  10.     {
  11.         if (liczby[i] == liczba)
  12.         return false;
  13.         i++;
  14.     };
  15.     return true;
  16. }
  17.  
  18. int losowanie(int min, int max)
  19. {
  20.     return rand() % (max-min+1) + min;
  21. }
  22. int main()
  23. {
  24.     srand(time(NULL));
  25.     cout << "Podaj 10 liczb: " << endl;
  26.     int liczby[10];
  27.     int podane = 0;
  28.     do
  29.     {
  30.         cin >> liczba;
  31.     if (czyUnikalna(liczba,liczby,podane)) //sprawdza czy w tablicy na indeksach 0 do i<pobrane nie ma juz wartości liczba
  32.     {
  33.         liczby[podane] = liczba;
  34.             podane++;
  35.     }
  36.     else
  37.     {
  38.         // napisz, ze liczba nie jest unikalna
  39.     }
  40.     }while(podane<10);
  41.  
  42.     int wylosowaneIndeksy[8]; //tablica na indeksy już wylosowane
  43.     podane = 0;
  44.     do
  45.     {
  46.         int los = losowanie(0, 9);
  47.         if(czyUnikalna(los, wylosowaneIndeksy, podane)) //sprawdza
  48.         {
  49.         wylosowaneIndeksy[podane] = los;
  50.             cout << liczby[los] << endl;
  51.             podane++;
  52.         }
  53.     }while(podane<8);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement