Created
August 27, 2010 19:43
-
-
Save matiit/554034 to your computer and use it in GitHub Desktop.
Dynamiczna tablica
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
#include <algorithm> | |
#include <iostream> | |
#include <ctime> | |
#include <cstdlib> | |
#include <cmath> | |
using namespace std; | |
// Zgrabna funkcja do wypelniania tablicy losowymi liczbami | |
void wypeln_los(int *tab, int n, int min, int max){ | |
for (int i=0;i<n;i++){ | |
tab[i] = rand()%(max-min+1)+min; | |
} | |
} | |
int licz_sume(int *tab, int n){ | |
int suma =0; | |
for (int i=0;i<n;i++){ | |
suma += tab[i]; | |
} | |
return suma; | |
} | |
double licz_srednia(int suma, int n){ | |
return suma/n; | |
} | |
double licz_odchylenie(int *tab,double srednia, int n) | |
{ | |
float dodaj=0; | |
float odchylenie=0; | |
for(int i=0;i<n;i++) | |
{ dodaj=dodaj+(tab[i]-srednia)*(tab[i]-srednia);} | |
odchylenie=sqrt(dodaj/19); | |
return odchylenie; | |
} | |
void wyswietl_tab(int *tab, int n){ | |
for (int i=0;i<n;i++){ | |
cout << tab[i] << endl; | |
} | |
} | |
int main(){ | |
int n; | |
cout << "Podaj liczbę od 1 do 1000" << endl; | |
cin >> n; | |
//Dynamiczne przydzielanie pamięci | |
int *tab = new int[n]; | |
//Potrzebne do "losowości" | |
srand(time(NULL)); | |
wypeln_los(tab, n, -50, 50); | |
int suma; | |
suma = licz_sume(tab,n); | |
cout << "Suma: " << suma << endl; | |
double srednia; | |
srednia = licz_srednia(suma,n); | |
cout << "Średnia: "<< srednia << endl; | |
double odchylenie; | |
odchylenie = licz_odchylenie(tab, srednia, n); | |
cout << "Odchylenie: " << odchylenie << endl; | |
sort(tab, tab+n); | |
cout << "Tablica po sortowaniu: " << endl; | |
wyswietl_tab(tab, n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment