--Originally published at OlafGC / Class #TC1017
Here it is, the first time I used vectors. You have to get used to it because the logic of calling a function and then the parameter is not something we are pretty used to, so I need a lot of practice. Also, a friend of mine helped me realizing I have committed a mistake: calling a function “sum”. Please, if you are going to name functions, use another name. If you need help, just send me a Twitter.
Code
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
float suma (vector<float> list)
{
float suma=0, n=0;
for (int count=0; count<10; count++)
{
suma+=list[count];
} return suma;
}
float average(vector<float> list)
{
float average;
average=suma(list)/10;
return average;
}
float stdv(vector<float> list)
{
float sigma=0, stdv=0;
for (int count=0; count<10; count++)
{
sigma+=pow(list [count]-average(list), 2);
}
stdv=sqrt(sigma/10);
return stdv;
}
int main()
{
vector <float> list;
float n,av,dev,sum;
cout<<“Please insert 10 numbers:”<<endl;
for(int count=0; count<10; count++)
{
cin>>n;
list.push_back(n);
}
cout<<“The sum of the numbers is “<<suma(list)<<“.”<<endl;
cout<<“The average of the numbers is “<<average(list)<<“.”<<endl;
cout<<“The standart deviation of the numers is “<<stdv(list)<<“.”<<endl;
}