Skip to content

Instantly share code, notes, and snippets.

@samir96

samir96/q11p1.cp

Created Apr 30, 2015
Embed
What would you like to do?
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <cmath>
#include <vector>
using namespace std;
float desviacion( vector<int> v, int tam, int suma){
float desviacion;
int i;
float Suma_dif =0.0;
float prom = promedio(suma, tam);
for (i=0; i < tam;i++ ){
Suma_dif += ((v[i] -prom) *(v[i] - prom));
}
desviacion = sqrt(Suma_dif/tam);
return desviacion;
}
float promedio(float sum, int lines){
float promedio;
promedio=sum/(lines);
return promedio;
}
void readNumbers(string namefile){
ifstream file (namefile);
string line;
vector<int> number;
int line_number=0, suma =0;
if (file.is_open()){
while (getline(file, line)){
int x = atoi(line.c_str());
number.push_back(x);
suma += number [line_number];
line_number++;
}
}
cout << "The total of the values is "<< suma << endl;
cout << "La suma total es: "<< line_number << endl;
cout << "El promedio es: "<< promedio(suma,line_number) << endl;
cout << "La desviacion estandar es " << desviacion(number, line_number, suma) << endl;
}
int main(){
readNumbers("random_numbers.txt");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.