Mean and Standard deviation

I have a textbook exercise challenge that I am currently working on. The directions are as follows:
Write an interactive c++ program that outputs the mean and Standard Deviation of a set of 11 integer values.

PS Although the individual values are integers, the results are floating point values.

So here's what I have so far. It's giving me an error on the mean calculation line and I'm not sure what I'm doing wrong.

I'm still pretty new to this the textbook gives me a more complex solution using loops etc but i'm not there yet so I tried writing a program with what Iknow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #include <iostream>

using namespace std;

int main()
{
    // Define variables: Input numbers,mean and standard deviation
    float numberOne;
    float numberTwo;
    float numberThree;
    float numberFour;
    float numberFive;
    float numberSix;
    float numberSeven;
    float numberEight;
    float numberNine;
    float numberTen;
    float numberEleven;

    // Values Input by the user

    cout << " Enter your first value " << numberOne ;
    cout << " Enter your second value " << numberTwo ;
    cout << " Enter your third value " << numberThree ;
    cout << " Enter yourFourth value " << numberFour ;
    cout << " Enter your fifth  value " << numberFive ;
    cout << " Enter your sixth value "<< numberSix ;
    cout << " Enter you seventh value " << numberSeven ;
    cout << " Enter your eighth value " << numberEight ;
    cout << " Enter your ninth value" << numberNine ;
    cout << " Enter your tenth value" << numberTen ;
    cout << " Enter your eleventh value" << numberEleven ;

    // Will calculate the mean

    mean = ( numberOne + numberTwo + numberThree + numberFour + numberFive + numberSix + numberSeven + numberEight + numberNine + numberTen + numberEleven ) / 11) ;

    // Calculates the standard deviation

    standardDeviation = sqrt(((pow( numberOne - mean,2)) + (pow(numberTwo-mean,2)) + (pow(numberThree-mean,2)) + pow(numberFour-mean,2)) + (pow(numberFive-mean,2)) +
    pow(numberSix-mean,2)) + (pow(numberSeven-mean,2)) + pow(numberEight-mean,2)) + (pow(numberNine-mean,2)) + pow(numberTen-mean,2)) + (pow(numberEleven-mean,2)))/11);

    // output of mean and standard deviation

    cout << " The mean is: " >> mean >> " The Standard Deviation is:" >> standardDeviation >> " , " >> endl;

    return 0;

}





Last edited on
You have one closing bracket too much.
It's giving me an error on the mean calculation line

What exactly is the error?

How and where are mean and standardDeviation defined?

PS Although the individual values are integers

Then why are you using float instead of int for the individual values?
1
2
3
4
5
6
7
8
9
10
11
12
    // Define variables: Input numbers,mean and standard deviation
    float numberOne;
    float numberTwo;
    float numberThree;
    float numberFour;
    float numberFive;
    float numberSix;
    float numberSeven;
    float numberEight;
    float numberNine;
    float numberTen;
    float numberEleven;

An array would be much more efficient.

1
2
3
    // Values Input by the user

    cout << " Enter your first value " << numberOne ;

I don't see any cin.

It's giving me an error on the mean calculation line

Doing calculations on uninitialised variables will result in garbage output. Make it a habit to initialise variables. Here's the way I like to initialise my variables.
1
2
3
4
int a{}; // initialised to 0
char c{}; // initialised to null terminator '\0'
double d{}; // initialised to 0.0
std::string s{}; // don't have to do {} but it's a habit 
I see a lot of problems.
1. You try outputting the value of all your numbers. You need to get the info not print it. Use cin to input data
2. You need to define your variable mean and standardDeviation.
3. Check all your brackets for both mean and standardDeviation, there are many mistakes
4. When you are outputting with cout use << not >>. << is for output and >> is for input
5. You say "PS Although the individual values are integers, the results are floating point values." but in your program, you are using floats
6. this would be much easier using a simple loop

There may be more but this is what I see while just reading through.
Thank you so much for all of your feedback. Greatly appreciate it. After making several adjustments this worked!
xx Cristina

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

/* Cristina O

This C++ program computes the mean and standard deviation of a set of 10 integer values. */
//Program name: ALGORITHIM WORKBENCH EXERCISE 1:

int main()
{
// Begin by assigning variable names and values
float X1, X2, X3, X4, X5, X6, X7, X8, X9, X10;
float mean;
float StandardDeviation;

cout << "This program will compute the mean and standard deviation of 10 integer values \n" <<endl;
// Allow the user to enter his or her own data
cout<<"Enter the first value: ";
cin>>X1;
cout<<"Enter the second value: ";
cin>>X2;
cout<<"Enter the third value: ";
cin>>X3;
cout<<"Enter the fourth value: ";
cin>>X4;
cout<<"Enter the fifth value: ";
cin>>X5;
cout<<"Enter the sixth value: ";
cin>>X6;
cout<<"Enter the seventh value: ";
cin>>X7;
cout<<"Enter the eighth value: ";
cin>>X8;
cout<<"Enter the ninth value: ";
cin>>X9;
cout<<"Enter the tenth value: ";
cin>>X10;

// This equation is used to calculate the mean of a set of numbers
mean=(X1+X2+X3+X4+X5+X6+X7+X8+X9+X10)/10;

// Calculate the Standard Deviation of the set of values entered
StandardDeviation = sqrt(((pow(X1-mean,2)) + (pow(X2-mean,2)) +
(pow(X3-mean,2)) + (pow(X4-mean,2)) + (pow(X5-mean,2)) +
(pow(X6-mean,2)) + (pow(X7-mean,2)) + (pow(X8-mean,2)) + (pow(X9-mean,2)) + (pow(X10-mean,2)))/ 10);

// Set the maximum number of Sig Figures.

cout <<fixed;
cout<<setprecision(2) << "\nYour mean is:"<<mean<<endl;
cout<<setprecision(2) << "\n Your StandardDeviation is:\n"<< StandardDeviation<<endl;
cin.get(); cin.get();
return 0;
}

]
Last edited on
Topic archived. No new replies allowed.