1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
//
const char duom[] = "d.txt";
const char rez[] = "r.txt";
const int size = 30;
//
void skaityti(int U[], int &n); // Reads var from file
void spausdinti(double &klvid, double &mvid, double &vvid); // prints to file
void vidurkis(int U[], int &n, double &klvid, double &mvid, double &vvid); // all math done here
//
int main()
{
int U[size], n, klsum, klmsum, klvsum;
double klvid, mvid, vvid;
skaityti(U, n);
spausdinti(klvid, mvid, vvid);
vidurkis(U, n, klvid, mvid, vvid);
return 0;
}
//
void skaityti(int U[], int &n)
{
ifstream in(duom);
in >> n;
for(int i = 0; i != n; i++)
{
in >> U[i];
}
}
//
void spausdinti(double &klvid, double &mvid, double &vvid)
{
ofstream out(rez);
out << "Klases vidutinis ugis yra: " << klvid << endl;
out << "Klases merginu vidutinis ugis yra: " << mvid << endl;
out << "Klases vaikinu vidutinis ugis yra: " << vvid << endl;
}
//
void vidurkis(int U[], int &n, double &klvid, double &mvid, double &vvid)
{
int m = 0, v = 0, klsum = 0, klmsum = 0, klvsum = 0;
for(int i = 0; i != n; i++)
{
klsum += abs(U[i]);
if(U[i] > 0)
{
klmsum += U[i];
m++;
}
else
{
v++;
klvsum += abs(U[i]);
}
}
klvid = (double)klsum / n;
mvid = (double)klmsum / m;
vvid = (double)klvsum / v;
}
|