Finding mean of the doubles using vector!

Hi,

I'm new to C++...
I need to write a C++ code for taking values of 'x' and 'y' coordinates from the user using a space between the two values of the pair.
value of 'x' needs to be a integer and 'y' needs to be an double and
80>x>-5 and 100>y>0
User input is taken until he enters a value which is not of the required type or is out of a specific range of 'x' and 'y'.

And then, I need to print the average (arithmetic mean) of just the y coordinates.

Input needs to be like this:

3 4.3 // here 3 is x-coordinate and 4.3 is y-coordinate
4 2.1 // here 4 is x-coordinate and 2.1 is y-coordinate
a 2 // program ends coz of wrong 'type' of the input
-11 4.4 // program ends coz x is out of range...(x>-5) , etc.

I somehow wrote the void function(not very appropriate) for input!!

But I wrote a separate function for calculating mean using vectors.

but I don't know exactly how to calculate the mean of just the y coordinates! I think I probably need to use vectors but don't know how to use them here in this situation.

Please help,
even a pseudocode may help.
I would be very thankful to you,
Pam.
just for finding the mean you don't need to store all the numbers. the simple equation is Mean(n) = ((Mean(n-1)*(n-1))+Value(n)) / n. here is the code which calculates the sum of all values and prints out the average finally.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int x;
double y;
double ySum;
int numberOfValues;
while () {
    std::cin>>x>>y;
    if (valid x and valid y) {
        ySum += y;
        numberOfValues++;
    } else {
        std::cout<<ySum/numberOfValues<<std::endl;
        break;
    }
}

here is a code which continuously updates the average
and finally prints it out finally.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int x;
double y;
double yAverage;
int numberOfValues;
while () {
    std::cin>>x>>y;
    if (valid x and valid y) {
        yAverage *= numberOfValues;
        yAverage += y;
        numberOfValues++;
        yAverage /= numberOfValues;
    } else {
        std::cout<<yAverage<<std::endl;
        break;
    }
}

Topic archived. No new replies allowed.