vertc

25-04

Apr 25th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int zakres(int x) {
  8.     cin>>x;
  9.     return x;
  10. }
  11.  
  12. int iloscele() {
  13.    
  14.     int n;
  15.     cout<<"Podaj ilosc elementow n: "<<endl;
  16.     cin>>n;
  17.     return n;
  18. }
  19.  
  20. void gen(int tab[], int n, int a, int b) {
  21.     for(int i=0;i<n;i++) {
  22.         tab[i] = rand()%(b+1)+a;
  23.     }
  24.     cout<<endl<<"Liczby zostaly wygenerowane."<<endl;
  25. }
  26.  
  27. int wyswietl(int tab[], int n) {
  28.     cout<<"Wygenerowane liczby to: ";
  29.     for(int i=0;i<n;i++) {
  30.         cout<<tab[i]<<" ";
  31.     }
  32. }
  33.  
  34. void sortuj_wybor(int tab[], int n) {
  35.     for(int j=0;j<n-1;j++) {
  36.         int temp, pmin = j;
  37.         for(int i=j+1;i<n;i++)
  38.             if(tab[i]<tab[pmin])    pmin = i;
  39.         temp = tab[pmin];
  40.         tab[pmin] = tab[j];
  41.         tab[j] = temp;
  42.     }
  43.     cout<<"Liczby zostaly posortowane.";
  44. }
  45.  
  46. void sortuj_babel(int tab[], int n) {
  47.      for (int i=0; i<9; i++)
  48.         for (int j=0; j<9; j++)
  49.             if (tab[j]>tab[j+1])
  50.                 swap(tab[j], tab[j+1]);
  51.     cout<<"Liczby zostaly posortowane.";
  52. }
  53.  
  54. void sortuj_wstawianie(int tab[], int n) {
  55.         for(int j=n-2;j>=0;j--) {
  56.         int x = tab[j];
  57.         int i = j+1;
  58.         while((i<n) && (x>tab[i])) {
  59.             tab[i-1] = tab[i];
  60.             i++;
  61.         }
  62.     tab[i-1] = x;
  63.     }
  64.     cout<<"Liczby zostaly posortowane.";
  65. }
  66.  
  67. void maksymalna(int tab[], int n, int a) {
  68.     int max=a;
  69.     for(int i=0;i<n;i++) {
  70.         if(tab[i]>max) {
  71.             max = tab[i];
  72.         }
  73.     }
  74.     cout<<"Liczba maksymalna to: "<<max<<endl;
  75. }
  76.  
  77. void minimalna(int tab[], int n, int b) {
  78.     int min=b;
  79.     for(int i=0;i<n;i++) {
  80.         if(tab[i]<min) {
  81.             min = tab[i];
  82.         }
  83.     }
  84.     cout<<"Liczba minimalna to: "<<min<<endl;
  85. }
  86.  
  87. void srednia(int tab[], int n) {
  88.     int suma=0, srednia=0;
  89.     for(int i=0;i<n;i++) {
  90.         suma = suma + tab[i];
  91.     }
  92.     srednia = suma / n;
  93.     cout<<"Srednia liczb wynosi: "<<srednia<<endl;
  94. }
  95. /*
  96. void wystapienia() {
  97. }
  98. */
  99.  
  100. int main() {
  101.     srand(time(0));
  102.     int tab[1000], n, a, b, wybor=12;
  103.         while(wybor!=0) {
  104.         cout<<endl<<"______________________________"<<endl;
  105.         cout<<"|0. Wyjscie                   |" <<endl;
  106.         cout<<"|1. Zakres liczb              |" <<endl;
  107.         cout<<"|2. Ilosc elementow n         |" <<endl;
  108.         cout<<"|3. Generowanie tablicy       |" <<endl;
  109.         cout<<"|4. Wyswietlanie              |" <<endl;
  110.         cout<<"|5. Sortowanie (wybor)        |" <<endl;
  111.         cout<<"|6. Sortowanie babelkowe      |" <<endl;
  112.         cout<<"|7. Sortowanie (wstawianie)   |" <<endl;
  113.         cout<<"|8. Liczba maksymalna         |" <<endl;
  114.         cout<<"|9. Liczba minimalna          |" <<endl;
  115.         cout<<"|10. Srednia liczb            |" <<endl;
  116.         cout<<"|11. Ilosc wystapien          |" <<endl;
  117.         cout<<"|_____________________________|" <<endl;
  118.         cout<<endl<<"Wybierz dzialanie: ";
  119.         cin>>wybor;
  120.         switch(wybor) {
  121.         case 0:
  122.             cout<<endl<<"Wychodzenie z programu...";
  123.             break;
  124.         case 1:
  125.             cout<<"Podaj zakres minimalny: ";
  126.             a=zakres(a);
  127.             cout<<"Podaj zakres maksymalny: ";
  128.             b=zakres(b);
  129.             break;
  130.         case 2:
  131.             n = iloscele();
  132.             break;
  133.         case 3:
  134.             gen(tab, n, a, b);
  135.             break;
  136.         case 4:
  137.             wyswietl(tab, n);
  138.             break;
  139.         case 5:
  140.             sortuj_wybor(tab, n);
  141.             break;
  142.         case 6:
  143.             sortuj_babel(tab, n);
  144.             break;
  145.         case 7:
  146.             sortuj_wstawianie(tab, n);
  147.             break;
  148.         case 8:
  149.             maksymalna(tab, n, a);
  150.             break;
  151.         case 9:
  152.             minimalna(tab, n, b);
  153.             break;
  154.         case 10:
  155.             srednia(tab, n);
  156.             break;
  157.         /*case 11:
  158.             wystapienia();
  159.             break;*/
  160.         default:
  161.             cout<<endl<<"Nieprawidlowe polecenie";
  162.             break;
  163.         }
  164.     }
  165.     return 0;
  166. }
Add Comment
Please, Sign In to add comment