Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. void dodaj(int tab[], int dlugosc)
  4. {
  5.     for(int i=0; i<dlugosc; i++)
  6.     {
  7.         cout << "Podaj element " << i << ": ";
  8.         cin >> tab[i];
  9.     }
  10. }
  11.  
  12. void wyswietl(int tab[], int dlugosc)
  13. {
  14.     for(int i=0; i<dlugosc; i++)
  15.         cout << "Element " << i << ": " << tab[i] << endl;
  16.  
  17. }
  18.  
  19. int NajczestszaWartosc(int tab[], int dlugosc)
  20. {
  21.     int najwiecej=0, wynik;
  22.     for(int x=0; x<dlugosc; x++)
  23.     {
  24.         int licznik=0;
  25.         for(int y=x; y<dlugosc; y++)
  26.         {
  27.             if(tab[x]==tab[y])
  28.             {
  29.                 licznik++;
  30.                 if(licznik>najwiecej)
  31.                 {
  32.                     najwiecej=licznik;
  33.                     wynik=tab[x];
  34.                 }
  35.             }
  36.         }
  37.     }
  38.     return wynik;
  39. }
  40.  
  41. int main()
  42. {
  43.     cout << "Ile elementow ma posiadac tablica?: ";
  44.     int ile;
  45.     cin >> ile;
  46.     int tab[ile];
  47.     dodaj(tab, ile);
  48.     wyswietl(tab, ile);
  49.     cout << NajczestszaWartosc(tab,ile);
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement