Advertisement
MeehoweCK

Untitled

Dec 10th, 2020
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. const short N = 6;
  8.  
  9. bool czy_juz_jest(short liczba, short* tablica, short n)
  10. {
  11.     for(short i = 0; i < n; ++i)
  12.         if(liczba == tablica[i])
  13.             return true;
  14.     return false;
  15. }
  16.  
  17. void wypelnij_tablice(short* tablica)
  18. {
  19.     srand(time(nullptr));
  20.     short losowana;
  21.     for(short i = 0; i < N; ++i)
  22.     {
  23.         do
  24.         {
  25.             losowana = 1 + rand() % 49;
  26.         } while(czy_juz_jest(losowana, tablica, i));
  27.         tablica[i] = losowana;
  28.     }
  29. }
  30.  
  31. void wypisz_tablice(short* tablica)
  32. {
  33.     for(short i = 0; i < N; ++i)
  34.         cout << tablica[i] << '\t';
  35.     cout << endl;
  36. }
  37.  
  38. void bubblesort(short* tablica)
  39. {
  40.     short n = N - 1;
  41.     for(short i = 0; i < n; ++i)
  42.         for(short j = 0; j < n - i; ++j)
  43.             if(tablica[j] > tablica[j + 1])
  44.                 swap(tablica[j], tablica[j + 1]);       // zamiana
  45. }
  46.  
  47. int main()
  48. {
  49.     short tablica[6];
  50.     wypelnij_tablice(tablica);
  51.     bubblesort(tablica);
  52.     wypisz_tablice(tablica);
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement