Can anyone help me to program standard deviation? Here is the instructions for this program:

Write a program that takes as input five integers and outputs the mean and standard deviation of the numbers. If the numbers are val1, val2, val3, val4 and val5, the mean = (val1 + val2 + val3 + val4 + val5) / 5. The standard deviation is:

deviation =

Your program must contain at least the following functions: a function that gets one number from the user (this will be called 5 times), a function that calculates and returns the mean, and a function that calculates and returns the standard deviation.

You need to post some code, so that we can help you.

I don't know where to begin

#include <iostream>

int main()
{
   //declare the variables like this
   int val1;//this is type "int" because it will be an integer
   float deviation=0.0;//this is float because it will not be an integer
   //then you need to ask for them to the user
  cout<<"asking message";//cout imputs in screen what is in the " "
  cin>>val1;//cin takes in what the user tiped
  //here after you get all the data do the operation
  //then another cout of the result
  return 0;
}

This is as much help i can give you, the rest you should get from your teacher

This is not an assignment, I am just reading the book to see how hard programming is before I decide to take a course this fall.

o well either way the best way to learn is to try to do it why dont you try to make the program with what i gave you, shouldn't be that hard

Hello Poncho4all,

Here is the code I have so far, but I need to know how to calculate the standard deviation? Thanks...

#include <iostream>
using namespace std;

int main()
{
    system("color 4");
    int value1, value2, value3, value4, value5;
    int sum=0, mean;
    
    cout << "This program will take 5 inputed integers and outputs the mean and"
            " standard deviation of the numbers. \n";

    cout << "Enter value1 = ";
    cin >> value1;
    
    cout << "Enter value2 = ";
    cin >> value2;
    
    cout << "Enter value3 = ";
    cin >> value3;
    
    cout << "Enter value4 = ";
    cin >> value4;
    
    cout << "Enter value5 = ";
    cin >> value5;
    
    sum += value1 + value2 + value3 + value4 + value5;
    mean = (double) sum /5;
    
    cout << "The mean is: " << mean << endl << endl;
    
   system ("pause");
    return 0;
}

I'm not sure what you mean by deviation?
I thought it was something like a average since its a sum and then divided into the amount of numbers that where sumed.

ok now back to the code:

#include <iostream>

using namespace std;

int main()
{
int sum,val;
float mean;
sum=0;                 //initializes sum in 0 so it doesn't get trash
cout<<"Enter the values: "<<endl;
for(int i=0;(i<5);i++)                   //used a loop called for because its easier and //its cleaner
{                 //the loop is also a good thing to learn beacuse you  //can implement your program with just changing one variable
cout<<"Value "<<(i+1)<<" : ";
cin>>val;                    //gets the value on each time the loop comes
tot+=val;//the the += is valid because tot is gonna be all the values
//on your program it could be only = beacuse each val enters at //the same time
}
mean=(float)sum/5;         //this was a great way of focing the //variable
cout<<"The mean is: "<<mean<<endl;
system("pause");
return 0;
}

I hope this actually helps you and not confuse you

I need to know how to calculate the standard deviation?

Do you know what the standard deviation is and how to calculate it on paper? The standard deviation of a set shows how the items in the set are dispersed. If the standard deviation is low, the items tend toward the mean, and if the standard deviation is high, the items are all over the place.

You calculate the standard deviation by finding the mean, then subtracting the mean and squaring the result for each item to create a new set. The square root of the average of the new set is the standard deviation:

// code intentionally advanced to deter cheaters
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
#include <cmath>

template <typename T>
struct SquareOfDiff:
    public std::binary_function<T, double, T>
{
    T operator()(T x, double mean) const
    {
        x -= (T)mean;
        return x *= x;
    }
};

int main()
{
    std::vector<double> set;
    double mean;

    std::cout << "Distriution set: ";
    std::copy(
        std::istream_iterator<double>(std::cin),
        std::istream_iterator<double>(),
        std::back_inserter(set));

    mean = std::accumulate(set.begin(), set.end(), 0.0) / set.size();
    std::transform(
        set.begin(), set.end(), 
        set.begin(), std::bind2nd(SquareOfDiff<double>(), mean));
    mean = std::accumulate(set.begin(), set.end(), 0.0) / set.size();

    std::cout << "Standard deviation: " << sqrt(mean) << '\n';
}
commented: Doesn't really help the OP considering his level. -4
commented: Works for me, the OP has already lost anyway. +36
// code intentionally advanced to deter cheaters
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
#include <cmath>

template <typename T>
struct SquareOfDiff:
    public std::binary_function<T, double, T>
{
    T operator()(T x, double mean) const
    {
        x -= (T)mean;
        return x *= x;
    }
};

int main()
{
    std::vector<double> set;
    double mean;

    std::cout << "Distriution set: ";
    std::copy(
        std::istream_iterator<double>(std::cin),
        std::istream_iterator<double>(),
        std::back_inserter(set));

    mean = std::accumulate(set.begin(), set.end(), 0.0) / set.size();
    std::transform(
        set.begin(), set.end(), 
        set.begin(), std::bind2nd(SquareOfDiff<double>(), mean));
    mean = std::accumulate(set.begin(), set.end(), 0.0) / set.size();

    std::cout << "Standard deviation: " << sqrt(mean) << '\n';
}

I get that what you posted is more safe. But how is that code supposed
to help the OP ( a beginner ) understand what he needs, when he most likely,
has no clue of what some of the code does.

@Jake :
This might be of help.
Link

But how is that code supposed
to help the OP ( a beginner ) understand what he needs, when he most likely,
has no clue of what some of the code does.

It is not, obviously. If I gave an example that could be turned in as homework, I would get slammed by every net nanny on Daniweb for giving away code. Of course, now I get slammed for not being helpful enough. There is no winning this game. The only solution is not to post at all. :@

Anyway, I explained the algorithm as well as gave code. There is sufficient information for him to figure it out.

Member Avatar for iamthwee

Hi Tommy,

How's it going chicken? First, thank you for returning the red candy, I appreciate that.

Second, you are a good poster, no doubt about it. But the code you posted serves no purpose WHATSOEVER. OK, so the OP isn't going to be able to hand it in. Agreed. But what is he honestly going to do with it? Look at it, compile it, get his results. Then what?

I totally agree that you did good by trying to explain an algorithm to him. But to me I neg repped you because it seemed like you were just posting code for the sake of posting code.

commented: Can't you take this offline? -5
commented: go away you whining weener -7

But what is he honestly going to do with it? Look at it, compile it, get his results. Then what?

I like to give examples because I think everyone learns better with code. If I have to post code that is too advanced because of unreasonable restrictions by you people, then he will just have to go the extra mile to learn from it.

I have already seen what happens when I try to give examples at the right level. Everybody attacks me for giving away code. If you want to be useful, show me how to post helpful code without everyone having a hissy fit. Otherwise your criticism is unconstructive will be ignored.

commented: Agree! +11
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.