Advertisement
MeehoweCK

Untitled

May 6th, 2021
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 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.         }
  22.         while(czy_istnieje(n, tab, i));
  23.  
  24.         tab[i] = n;
  25.     }
  26. }
  27.  
  28. void wypisz(int tab[])
  29. {
  30.     for(int i=0; i<N; ++i)
  31.     {
  32.         cout << tab[i] << "\t";
  33.     }
  34.     cout << endl;
  35. }
  36.  
  37. void bubblesort(int* tab)
  38. {
  39.     for(int i = 0; i < N - 1; ++i)
  40.         for(int j = 0; j < N - i - 1; ++j)
  41.             if(tab[j] > tab[j + 1])
  42.                 swap(tab[j], tab[j + 1]);       // zamiana
  43. }
  44.  
  45. bool czy_istnieje(int a, int tab[], int n)
  46. {
  47.     for(int i=0; i<n; ++i)
  48.     {
  49.         if(tab[i]==a) return 1;
  50.     }
  51.     return 0;
  52. }
  53.  
  54. void podawanie(int tab1[])
  55. {
  56.     int p;
  57.     for(int i=0; i<N; ++i)
  58.     {
  59.         cout << "Podaj liczbe: ";
  60.         cin >> p;
  61.         while(czy_istnieje(p, tab1, i) || p < 1 || p > 49 || cin.fail())
  62.         {
  63.             if(cin.fail())
  64.             {
  65.                 cout << "Wpisz liczbe od 1 do 49: ";
  66.                 cin.clear();
  67.                 cin.ignore(99999, '\n');
  68.             }
  69.             else if(czy_istnieje(p, tab1, i))
  70.                 cout << "Podana liczba juz zostala wczesniej podana. Wpisz jeszcze raz: ";
  71.             else
  72.                 cout << "Podana liczba nie miesci sie w zakresie. Wpisz liczbe od 1 do 49: ";
  73.             cin >> p;
  74.         }
  75.         tab1[i]=p;
  76.     }
  77. }
  78.  
  79.  
  80. int ile_trafionych(int tab[], int tab1[])
  81. {
  82.  
  83.     int count=0;
  84.     for(int i=0; i<N; ++i)
  85.     {
  86.         if(czy_istnieje(tab[i], tab1, N))
  87.             ++count;
  88.     }
  89.     return count;
  90. }
  91.  
  92.  
  93. int main()
  94. {
  95.     int tab[N], tab1[N];
  96.  
  97.     podawanie(tab1);
  98.     bubblesort(tab1);
  99.     wypelnianie(tab);
  100.     bubblesort(tab);
  101.     cout << "Tablica podana:  ";
  102.     wypisz(tab1);
  103.     cout << "Tablica wylosowana:  ";
  104.     wypisz(tab);
  105.     cout << ile_trafionych(tab, tab1);
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement