Created
March 23, 2018 17:29
-
-
Save KrzysiekWyka/83236528dbfa8b64943e508a71afce2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ConsoleApplication3.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <cstdlib> | |
#include <iostream> | |
using namespace std; | |
class Tablica { | |
public: | |
Tablica(int rozmiar) { n = rozmiar; wsk = new float[n]; } // Entliczek | |
~Tablica(); // Pentliczek | |
Tablica(Tablica &t); // Zielony stoliczek | |
float getWsk(int index) { return wsk[index]; } | |
void setWsk(int index, float value) { wsk[index] = value; } | |
void wczytaj(); | |
void wypisz(); | |
float avg(); | |
float min(); | |
float max(); | |
void posortuj(); | |
float mediana(); | |
private: | |
int n; | |
float *wsk; | |
}; | |
Tablica::~Tablica() { | |
delete wsk; | |
} | |
Tablica::Tablica(Tablica &t) { | |
n = t.n; | |
wsk = new float[n]; | |
for (int i = 0; i < n; i++) { | |
wsk[i] = t.wsk[i]; | |
} | |
} | |
void Tablica::wczytaj() { | |
for (int i = 0; i < n; i++) { | |
cout << "Podaj " << i + 1 << " element tablicy: "; | |
cin >> wsk[i]; | |
} | |
cout << "Podales juz wszystkie elementy!" << endl; | |
} | |
void Tablica::wypisz() { | |
for (int i = 0; i < n; i++) { | |
cout << "Element o indexie " << i << " jest rowny " << wsk[i] << endl; | |
} | |
cout << "To juz wszystkie elementy twojej tablicy" << endl; | |
} | |
float Tablica::avg() { | |
float sum = 0; | |
for (int i = 0; i < n; i++) { | |
sum += wsk[i]; | |
} | |
return sum / n; | |
} | |
float Tablica::min() { | |
float min = wsk[0]; | |
for (int i = 1; i < n; i++) { | |
if (wsk[i] < min) | |
min = wsk[i]; | |
} | |
return min; | |
} | |
float Tablica::max() { | |
float max = wsk[0]; | |
for (int i = 1; i < n; i++) { | |
if (wsk[i] > max) | |
max = wsk[i]; | |
} | |
return max; | |
} | |
// Sortowanie | |
// Mediana | |
void Tablica::posortuj() { | |
for (int i = 0; i<n; i++) | |
for (int j = 1; j<n - i; j++) | |
if (this->wsk[j - 1] > this->wsk[j]) { | |
float stara = this->wsk[j - 1]; | |
this->wsk[j - 1] = this->wsk[j]; | |
this->wsk[j] = stara; | |
} | |
} | |
float Tablica::mediana() { | |
float* posortowana = wsk; | |
for (int i = 0; i<n; i++) | |
for (int j = 1; j<n - i; j++) | |
if (this->wsk[j - 1] > this->wsk[j]) { | |
float stara = this->wsk[j - 1]; | |
this->wsk[j - 1] = this->wsk[j]; | |
this->wsk[j] = stara; | |
} | |
if (n % 2 != 0) { | |
int index = ceil(n / 2); | |
return posortowana[index]; | |
} | |
else | |
{ | |
int index = n / 2; | |
return (posortowana[index - 1] + posortowana[n - index]) / 2; | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
Tablica t1(4); | |
t1.wczytaj(); | |
t1.wypisz(); | |
cout << "Maksymalna wartosc to " << t1.max() << endl; | |
cout << "Minimalna wartosc to " << t1.min() << endl; | |
cout << "Srednia wartosc to " << t1.avg() << endl; | |
cout << "Mediana to " << t1.mediana() << endl; | |
t1.posortuj(); | |
t1.wypisz(); | |
system("PAUSE"); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment