PatrickSwayze

zadanie 4 rozdzial 10

Nov 27th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. /*
  2. 4.  Napisz program, który uzupe³ni losowymi wartoœciami tablice
  3. N-elementow¹ liczb rzeczywistych, wyœwietli j¹ na monitorze komputera,
  4.  a nastêpnie policzy medianê. W rozwi¹zaniu u¿yj funkcji.
  5. WEJŒCIE:
  6. 5
  7. { 13.3 15.7 8.0 2.0 19.0}
  8. WYJŒCIE:
  9. Mediana: 13.3
  10. */
  11. #include <iostream>
  12. #include <ctime>
  13. #include <cstdlib>
  14. #include <algorithm>
  15.  
  16.  
  17. using namespace std;
  18. void wprowadz(int &n,float *t)
  19. {
  20.  
  21.     cout<<"Wprowadz ilosc elementow: ";
  22.     cin>>n;
  23.     for (int i=0;i<n;i++)
  24.     {
  25.         t[i]=(float)rand()/(RAND_MAX);
  26.     }
  27.  
  28. }
  29. void sortt(int n,float *t)
  30. {
  31.     cout<<endl;
  32.     cout<<"Liczby posortowane rosnaco: "<<endl;
  33.     sort( t, t + n );
  34.       for(int i = 0; i < n; i++)
  35.       {
  36.           cout << endl << "T[" << i << "] : " << t[i];
  37.       }
  38.       sort( t, t + n, greater < float >() );
  39. }
  40. float med(int n,float *t,float mediana)
  41. {
  42.     //mediana=0;
  43.      if (n%2 == 0)
  44.     {
  45.         mediana = t[(n-1)/2]+t[n/2];
  46.         mediana = mediana/2;
  47.     }
  48.     else
  49.     {
  50.         mediana = t
  51.          [n/2];
  52.     }
  53.     return mediana;
  54. }
  55. void wypisz (float &mediana, int n, float *t)
  56. {
  57.     cout<<endl;
  58.     cout<<"Ilosc elementow: "<<n<<endl;
  59.  
  60.  
  61.  
  62.     cout<<"Mediana: "<<mediana;
  63. }
  64.  
  65. int main()
  66. {
  67.     int n;
  68.     float tab[10],mediana;
  69.     srand( time( NULL ) );
  70.     wprowadz(n,tab);
  71.     sortt(n,tab);
  72.  
  73.     wypisz(mediana,n,tab);
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment