#include <iostream>
#include<iomanip>
#include <cmath>
usingnamespace std;
int main()
{

char choice;
const int arraysize=20;
int num[arraysize];
do
{

cout<<"I will give you the Sum,Mean,Var & the Std Dev of any series of numbers?Y/N:"<<endl;
cin>>choice;
if(choice =='Y'||choice =='y')
{

cout<<"How many numbers will you enter? (up to 20)?"; cin>>num[arraysize];
for (int j=0;j<num[arraysize];j++)
{
cout<<"Enter Number"<<j+1<<":"; cin>> num[j];
}

cout << " You have entered the following:"<<endl;
for (j = 0; j < num[arraysize]; j++)
{

cout << num[j]<<" ";
}
int sum=0;
for (j=0;j<num[arraysize];j++)
sum+= num[j];
cout<<"\nThe Sum is "<<sum<<endl;
float mean= (sum/num[arraysize]);
cout << showpoint << fixed << setprecision (2);
cout<<"The Mean is "<<mean<<endl;
float var=((num[0]-mean)*pow(num[0]-mean,2) + (num[1]-mean)*pow(num[1]-mean,2) + + (num[2]-mean)*pow(num[2]-mean,2))/ (num[arraysize-1]);
cout << showpoint << fixed << setprecision (2);
cout<<"The Varience is " <<var<<endl;
float sqrtvar=sqrt(var);
cout << showpoint << fixed << setprecision (2);
cout<<"The Standard deviation is "<<sqrtvar<<endl;
}
}while(choice!='N'&& choice!='n');
return 0;

}

Recommended Answers

All 6 Replies

And the question is?

#include <iostream>
#include<iomanip>
#include <cmath>
usingnamespace std;
int main()
{

    char choice;
    int total_numbers = 0;
    const int arraysize = 20;
    int num[arraysize];
    do {

    cout << "I will give you the Sum,Mean,Var & the Std Dev of any series of numbers?Y/N:" << endl;
    cin >> choice;
    if (choice == 'Y' || choice == 'y') {

        cout << "How many numbers will you enter? (up to 20)?";
// cin>>num[arraysize];  this is wrong it means entering value in 20th element
        cin >> total_numbers;   // this is correct way of accpeting single number
        for (int j = 0; j < total_numbers; j++) {
        cout << "Enter Number" << j + 1 << ":";
        cin >> num[j];
        }

        cout << " You have entered the following:" << endl;
        for (j = 0; j < total_numbers; j++) {
        cout << num[j] << " ";
        }
        int sum = 0;
        for (j = 0; j < total_numbers; j++)
        sum += num[j];
        cout << "nThe Sum is " << sum << endl;
        float mean = (sum / total_numbers);
        cout << showpoint << fixed << setprecision(2);
        cout << "The Mean is " << mean << endl;
        float var = ((num[0] - mean) * pow(num[0] - mean, 2) + (num[1] - mean) * pow(num[1] - mean, 2) + +(num[2] - mean) * pow(num[2] - mean, 2)) / (num[arraysize - 1]);
        cout << showpoint << fixed << setprecision(2);
        cout << "The Varience is " << var << endl;
        float sqrtvar = sqrt(var);
        cout << showpoint << fixed << setprecision(2);
        cout << "The Standard deviation is " << sqrtvar << endl;
    }
    } while (choice != 'N' && choice != 'n');
    return 0;

}

I think you gettting the fundamentals of programming wrong. Why you taking in the number of elements entered by the user into an array. Why not just use a simple int variable like "total_numbers". I have made some changes in teh program, try running it now and see what you get. And also format your code a bit so it can be read by other people and put the code in the tags.

same result

See also http://www.daniweb.com/techtalkforums/thread56253.html
> usingnamespace std;
Now I don't know whether this is an endemic problem with your coding, but it should be using namespace std; , that is, there is a space in there.

Now maybe it's you or maybe it's some damn fool code colouring script you're running which is bugged to hell and back. Either way, it's not helpful to have to second guess what the difference is between what you posted and what we see.

Just post your code between

tags, direct from your source code editor. We don't need colouring in a variable width font if the price is mangled code. Keep it nice and simple in a monospaced font.

I think you are getting the formulas for deviation and variace wrong.

See this code for reference;

#include <iostream>
#include<iomanip>
#include <cmath>
using namespace std;

int main()
{
    float deviation, var ;
    char choice;
    int total_numbers  = 0 ;
    const int arraysize=20;
    int num[arraysize];
    do
    {
        cout<<"I will give you the Sum,Mean,Var & the Std Dev of any series of numbers?Y/N:"<<endl;
        cin>>choice;
        if(choice =='Y'||choice =='y')
        {
            cout<<"How many numbers will you enter? (up to 20)?";
            cin >> total_numbers ;  // this is correct way of accpeting single number
            for (int j=0; j < total_numbers; j++)
            {
                cout<<"Enter Number"<<j+1<<":"; cin>> num[j];
            }
            cout << " You have entered the following:"<<endl;
            for ( int j = 0;  j < total_numbers; j++)
            {
                cout << num[j]<<" ";
            }

            int sum=0;
            for (int j=0; j <  total_numbers; j++)
                sum+= num[j];
            cout<<"\nThe Sum is "<<sum<<endl;
            float mean= (sum/ total_numbers);
            cout << showpoint << fixed << setprecision (2);
            cout<<"The Mean is "<<mean<<endl;

            if (total_numbers > 1)
            {
                for (int i = 0; i < total_numbers; ++i )
                {
                    var += ((num [i] - mean) * (num [i] - mean)) ;
                }
                var /= (total_numbers - 1) ;
                deviation =sqrt(var);
            }

            else
            {
                var = 0.0 ;
                deviation = 0.0 ;
            }
            cout << showpoint << fixed << setprecision (2);
            cout<<"The Varience is " <<var<<endl;
            cout << showpoint << fixed << setprecision (2);
            cout<<"The Standard deviation is "<<deviation<<endl;
        }
    }
    while(choice!='N'&& choice!='n');
    return 0;

}

Maybe for the formula you should look here;

http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap08/means-8.html

Hope it helped, bye.

Like what ~S.O.S~ have said. The formula for variance in your coding is wrong. The formula of a variance is like this. Take the example that user input 3 numbers 1,2,3 and the mean is 2:( (1-2)^2+(2-2)^2+(3-2)^2)/3. Your variance formula seems to be fixed up to 3 numbers. Therefore, the answer will be incorrect no matter how many numbers are input. Below is one of the way to correct the calculation.

float var=0.0;
for (j=0;j<num[arraysize];j++)
{
var += (float)pow(num[j]-mean,2);
}
cout << "array=" << num[arraysize] << " Var=" << var << endl;
var /= num[arraysize];

Hope it helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.