Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 4. Napisz program, który uzupe³ni losowymi wartoœciami tablice
- N-elementow¹ liczb rzeczywistych, wyœwietli j¹ na monitorze komputera,
- a nastêpnie policzy medianê. W rozwi¹zaniu u¿yj funkcji.
- WEJŒCIE:
- 5
- { 13.3 15.7 8.0 2.0 19.0}
- WYJŒCIE:
- Mediana: 13.3
- */
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- #include <algorithm>
- using namespace std;
- void wprowadz(int &n,float *t)
- {
- cout<<"Wprowadz ilosc elementow: ";
- cin>>n;
- for (int i=0;i<n;i++)
- {
- t[i]=(float)rand()/(RAND_MAX);
- }
- }
- void sortt(int n,float *t)
- {
- cout<<endl;
- cout<<"Liczby posortowane rosnaco: "<<endl;
- sort( t, t + n );
- for(int i = 0; i < n; i++)
- {
- cout << endl << "T[" << i << "] : " << t[i];
- }
- sort( t, t + n, greater < float >() );
- }
- float med(int n,float *t,float mediana)
- {
- //mediana=0;
- if (n%2 == 0)
- {
- mediana = t[(n-1)/2]+t[n/2];
- mediana = mediana/2;
- }
- else
- {
- mediana = t
- [n/2];
- }
- return mediana;
- }
- void wypisz (float &mediana, int n, float *t)
- {
- cout<<endl;
- cout<<"Ilosc elementow: "<<n<<endl;
- cout<<"Mediana: "<<mediana;
- }
- int main()
- {
- int n;
- float tab[10],mediana;
- srand( time( NULL ) );
- wprowadz(n,tab);
- sortt(n,tab);
- wypisz(mediana,n,tab);
- }
Advertisement
Add Comment
Please, Sign In to add comment