/
q11part1.cpp
81 lines (62 loc) · 1.26 KB
/
q11part1.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iterator>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
float average( const vector<int>& myVector)
{
float tot;
for (int i=0; i<myVector.size(); i++)
{
tot += myVector[i];
}
float avg = tot/myVector.size();
return avg;
}
float stand (const vector<int>& myVector)
{
float temp = 0, avg;
avg = average (myVector);
for ( int i=0; i<myVector.size(); i++)
{
temp += pow(myVector[i]-avg, 2.0);
}
float standard = sqrt(temp/myVector.size());
return standard;
}
void readnumbers (string names)
{
ifstream inFile;
inFile.open(names);
vector<int> list;
int value, contador = 0, sum = 0;
if (inFile.good())
{
while ( ! inFile.eof()){
inFile >> value;
list.push_back(value);
contador++;
}
cout<< "The total of lines is: " << contador <<endl;
for (unsigned int i = 0; i < list.size(); i++)
{
sum += list[i];
}
cout<< "The sum of the numbers is: " << sum <<endl;
cout<< "The average of the list is: " << average(list) <<endl;
cout<< "The standard deviation is: " <<stand(list) <<endl;
}
else{
cout<< "error" <<endl;
}
}
int main ()
{
string name;
cout<< "Name of the file";
cin>> name;
readnumbers(name);
return 0;
}