Write a program that takes its input from a file of numbers
Solution
#include <iostream>
 #include <fstream>
 #include<math.h>
 using namespace std;
 float standdv(float[], int);
 int main()
 {
 std::fstream f(\"G:\\\\float.txt\",std::ios_base::in); //open the file make sure file is present in the given path.
 float a[100]; //initialise the required array with required size
 float n;   
 int size=0;
 while (f >> n) //read out the floats ne by one
 {
 a[size]=n; //store those floats into array
 size++;
 }
 cout<<\"standard deviation is:\"<<standdv(a,size); //caluclate the standard deviation by calling the function
return 0;
 }
float standdv(float a[], int size)
 {
    //caluclating standard deviation.
    float mean=0.0, sum=0.0;
 int i;
 for(i=0; i<size;++i)
 {
 mean+=a[i];
 }
 mean=mean/size;
 for(i=0; i<size;++i)
 sum+=(a[i]-mean)*(a[i]-mean);
 return sqrt(sum/size);
 }
Example file content:-
3.7
 7.8
 1.2
example output:-
Standard deviation is:2.7207

