Advertisement
MeehoweCK

Untitled

May 6th, 2021
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 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_istnieje(int, int*, int);  // prototyp funkcji
  10.  
  11. void wypelnianie(int tab[])
  12. {
  13.     int n;
  14.     srand(time(nullptr));
  15.  
  16.     for(int i=0; i<N; ++i)
  17.     {
  18.         do
  19.         {
  20.             n = 1+rand()%49;
  21.         } while(czy_istnieje(n, tab, i));
  22.  
  23.         tab[i] = n;
  24.     }
  25. }
  26.  
  27. void wypisz(int tab[])
  28. {
  29.     for(int i=0; i<N; ++i)
  30.     {
  31.         cout << tab[i] << "\t";
  32.     }
  33. }
  34.  
  35. void bubblesort(int* tab)
  36. {
  37.     for(int i = 0; i < N - 1; ++i)
  38.         for(int j = 0; j < N - i - 1; ++j)
  39.             if(tab[j] > tab[j + 1])
  40.                 swap(tab[j], tab[j + 1]);       // zamiana
  41. }
  42.  
  43. bool czy_istnieje(int a, int tab[], int n)
  44. {
  45.     for(int i=0; i<n; ++i)
  46.     {
  47.         if(tab[i]==a) return 1;
  48.     }
  49.     return 0;
  50. }
  51.  
  52. int main()
  53. {
  54.     int tab[N];
  55.  
  56.     wypelnianie(tab);
  57.     bubblesort(tab);
  58.     wypisz(tab);
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement