I’m having a serious output problem with a program i made for my homework. Below is the code I’m using to calculate the mean and standard deviation for a group of numbers given by the user, until a flag value of -1 is reached. I am not allowed to use arrays. I am supposed to use a formula (for the std dev part) given by my professor which is ((sum (xi)^n) – ((sum xi) ^2/ n) /(n-1)) xi is x sub i, which is each input. I am using visual studio 2010 on a win7 x64 vm on my macbook pro (doubt this has anything to do, but just in case).
below is the code (followed by the screen cap of the error)
#include<iostream>
#include <cmath>
using namespace std;
int i;
int sum_unkown_vars();
double std_dev(int sum, int n);
/*double std_dev(int sum, int n)
{
double dev;
dev = sqrt(((pow(sum, 2.0)-(pow(sum, 2.0)/n)))/(n-1));
return dev;
}*/
int sum_unkown_vars()
{
i = 0;
int n;
int sum1 = 0;
int sum_sqd = 0;
double sdev;
cout<<"This part will sum variables given by user until flag value of -1"<<endl;
cout<<"\nNext Variable Please: ";
cin >> n;
while(n != -1)
{
sum_sqd = sum_sqd + n*n;
sum1 = sum1 + n;
i++;
cout<<"\nNext Variable Please: ";
cin >> n;
}
cout <<"\nNumber of variables is "<< i <<endl
<<"Sum of variables is "<< sum1 <<endl;
sdev = sqrt(((sum_sqd*1.0)-(1.0*pow(sum1, 2.0)/n))/(n*1.0-1.0));
cout <<"\nStandard Deviation is "<< sdev << endl;
return sum1;
}
int main()
{
int sum = 0;
int j;
double avg;
double std_dev1;
cout<<"This program will take integers given by the user,"
<<"\nsum them, then find average and standard deviation\n\n";
sum = sum_unkown_vars();
//cout <<"\nPlease enter number of integers previously given: ";
//cin >> j;
avg = sum / (i*1.0);
cout <<"\nAverage is: "<<avg<<endl;
//std_dev1 = std_dev(sum, i);
//cout <<"Standard Deviation is : "<< std_dev1 <<endl;
system("pause");
return 0;
}
Thanks in advance for any help received

The variable n in the formula
And the variable ‘n’ in the function
are not the same. The n in the formula indicates the number of elements and this is defined by the variable ‘i’ in your program. Please correct this first.
Additionally number of variables should be either i+1 or you should increment i just after the initial reading.
I think there are still more errors in the code. e.g, you don’t return the standard deviation from the function, you are using integer to return the standard deviation etc. Please debug the rest yourself with a debugger. Please make an expectation for the values and cross check it in the debugger.