Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- const short N=6;
- bool czy_istnieje(int, int*, int); // prototyp funkcji
- void wypelnianie(int tab[])
- {
- int n;
- srand(time(nullptr));
- for(int i=0; i<N; ++i)
- {
- do
- {
- n = 1+rand()%49;
- }
- while(czy_istnieje(n, tab, i));
- tab[i] = n;
- }
- }
- void wypisz(int tab[])
- {
- for(int i=0; i<N; ++i)
- {
- cout << tab[i] << "\t";
- }
- cout << endl;
- }
- void bubblesort(int* tab)
- {
- for(int i = 0; i < N - 1; ++i)
- for(int j = 0; j < N - i - 1; ++j)
- if(tab[j] > tab[j + 1])
- swap(tab[j], tab[j + 1]); // zamiana
- }
- bool czy_istnieje(int a, int tab[], int n)
- {
- for(int i=0; i<n; ++i)
- {
- if(tab[i]==a) return 1;
- }
- return 0;
- }
- void podawanie(int tab1[])
- {
- int p;
- for(int i=0; i<N; ++i)
- {
- cout << "Podaj liczbe: ";
- cin >> p;
- while(czy_istnieje(p, tab1, i) || p < 1 || p > 49 || cin.fail())
- {
- if(cin.fail())
- {
- cout << "Wpisz liczbe od 1 do 49: ";
- cin.clear();
- cin.ignore(99999, '\n');
- }
- else if(czy_istnieje(p, tab1, i))
- cout << "Podana liczba juz zostala wczesniej podana. Wpisz jeszcze raz: ";
- else
- cout << "Podana liczba nie miesci sie w zakresie. Wpisz liczbe od 1 do 49: ";
- cin >> p;
- }
- tab1[i]=p;
- }
- }
- int ile_trafionych(int tab[], int tab1[])
- {
- int count=0;
- for(int i=0; i<N; ++i)
- {
- if(czy_istnieje(tab[i], tab1, N))
- ++count;
- }
- return count;
- }
- int main()
- {
- int tab[N], tab1[N];
- podawanie(tab1);
- bubblesort(tab1);
- wypelnianie(tab);
- bubblesort(tab);
- cout << "Tablica podana: ";
- wypisz(tab1);
- cout << "Tablica wylosowana: ";
- wypisz(tab);
- cout << ile_trafionych(tab, tab1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement