Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void pobierz(int tab[], int n)
  6. {
  7.     for (int i=0; i<n; i++)
  8.     {
  9.         cout<<"Wprowadz element o indeksie "<<i<<" "<<endl;
  10.         cin>>tab[i];
  11.     }
  12. }
  13. void wypisz(int tab[], int n)
  14. {
  15.     for (int i=0; i<n; i++)
  16.     {
  17.         cout<<"Element o indeksie "<<i<<" to: "<<tab[i]<<endl;
  18.     }
  19. }
  20. void zamienMale(int tab[], int n)
  21. {
  22.     int x=tab[0];
  23.     for (int i=0; i<n; i++)
  24.     {
  25.         if (tab[i]<x)
  26.         {
  27.             x=tab[i];
  28.         }
  29.     }
  30.     for (int i=0; i<n; i++)
  31.     {
  32.         if (tab[i] == x)
  33.         {
  34.            for (int j = i; j>0; j--)
  35.            {
  36.                swap(tab[j],tab[j-1]);
  37.            }
  38.         }
  39.     }
  40. }
  41.  
  42. void ZamienNaIloczynNastepnych(int tab[], int n)
  43. {
  44. int iloczyn =1;
  45.     for (int i=0; i<n-2; i++)
  46.     {
  47.         if (i==n-1) return;
  48.         iloczyn =1;
  49.         for (int j=i+1; j<n; j++)
  50.         {
  51.            iloczyn*=tab[j];
  52.  
  53.         }
  54.     tab[i]=iloczyn;
  55.     }
  56. }
  57. int *NajdluzszaPodtablicaMniejszychOdSredniej (int *tab, int dlugosc, int &rozmiar2)
  58. {
  59. int suma;
  60. for(int i = 0; i <dlugosc; i++)
  61. {
  62.     suma+=tab[i];                    /// LICZE SREDNIĄ
  63. }
  64. int srednia = suma/dlugosc;
  65.  
  66. int ile =0;
  67. int stareIle =0;
  68. int poczatek;
  69.  
  70.     for(int i =0; i<dlugosc; i++)
  71.         {
  72.           if(tab[i]<srednia)
  73.           {
  74.               ile++;
  75.               for(int j=i+1; j<dlugosc; j++)
  76.               {
  77.                   if (tab[j]<srednia){
  78.                     ile++;
  79.                   }
  80.                   if (tab[j]>srednia||j==dlugosc-1)
  81.                   {
  82.                       if (ile > stareIle)
  83.                       {
  84.                           stareIle = ile;
  85.                           poczatek = i;
  86.                       }
  87.                       break;
  88.                   }
  89.               }
  90.           }
  91.           ile =0;
  92.         }
  93.         int *tmp = new int[stareIle];
  94.  
  95.         int licznik=0;
  96.  
  97.         for (int i=poczatek; i<poczatek+stareIle; i++)
  98.         {
  99.             tmp[licznik] = tab[i];
  100.             licznik ++;
  101.         }
  102.  
  103.      rozmiar2 = stareIle;
  104.  
  105.      return tmp;
  106. }
  107.  
  108. int main()
  109. {
  110.     int n;
  111.     int rozmiar2;
  112.     cout<<"Wprowadz rozmiar tablicy: ";
  113.     cin>>n;
  114.     int *tab= new int [n];
  115.     pobierz(tab, n);
  116.     ZamienNaIloczynNastepnych(tab, n);
  117.     /*int *tablica2 = NajdluzszaPodtablicaMniejszychOdSredniej(tab, n, rozmiar2);
  118.     wypisz(tablica2,rozmiar2); */
  119.     wypisz(tab,n);
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement