Skip to content

Instantly share code, notes, and snippets.

@samir96
Created April 30, 2015 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samir96/89edc6be9cc2936097dd to your computer and use it in GitHub Desktop.
Save samir96/89edc6be9cc2936097dd to your computer and use it in GitHub Desktop.
#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