calculating avarage without knowning the sum of numbers but there are clues and the pattern repeats.

Hello everyone!

Lets imagine that we have a vector full of numbers:
1
2
3
4
5
6
vector<double> nums
nums.push_pack( 0.5 );
nums.push_pack( -0.3 );
nums.push_pack( 0.9 );
nums.push_pack( 0.01 );
nums.push_pack( -0.75 );

Totally random numbers between -1.0 - 1.0

Now instead of making a function, we will make a class instead.
Each vector has its own class.

1
2
3
4
5
class guess_avarage
{
public:
    double guess( double in ) { return 0.0; }
}

Having class allows us to have variables to help us guess it better.


This is how i would feed the function:
1
2
3
while(1) {
    for( int a = 0; a < nums.size(); a++ ) guess_avarage.guess( nums[a] );
}

I put there a while loop to show you that patern will repeat.
Now our function should return the avarage of the numbers inside the nums vector. There is a catch.
The numbers inside the nums vector are always changing.
We can't control how they are changing but the closer the numbers get to 0
the better our returned avarage was.
That means if we will do well with guessing avarage then all numbers, inside the vector, will turn into 0 eventually.

The numbers can always only be between -1.0 - 1.0 no matter how bad we're doing but -1.0 and 1.0 is the worse.
The closer our returned value to 0 is, the less the numbers may change, inside the vector.

Also one other thing is that sometimes the new numbers may appear in vector or some may go away.
Number can pop in or away between one pattern meaning that sometimes
we can't even finish the pattern without having a new number in it or a one number less. But the pattern will repeat itself often.

Now the rules are that we can not see what is inside the nums vector.
We don't know what was the previous number or what will be the next number however we can create a variable inside the class for previous num value.
We can have pretty much anything inside the class but
doing something like adding a vector to a class and checking if pattern repeats
its a bad idea cause that vector will grow too large and i will run out of memory fast.

The goal is to guess the number what is close to actual current avarage.
Any solution to my problem?
Thanks you!



Last edited on
> We can't control how they are changing
> The closer our returned value to 0 is, the less the numbers may change
So you do control how much they change.

> if we will do well (...) then all numbers (...) will turn into 0 eventually.
then you do not want an average.
If the vector was {-1, 1, -1, 1, -1, 1, ..., -1, 1} then the average would be 0 and so the numbers will not change

Moreover, ¿what if you output nonsense (like 42) so the numbers change a lot? After all, they can't leave the [-1, 1] range.


It seems that by eliminating the context of the problem you changed its rules.
Last edited on
> So you do control how much they change.
Yes and the change depends on the difference of real avarage and the one that returned.
Real avarage doesn't actually exist anywhere but its always there by theory.
It's like a ball radius exists even without measuring it.

> then you do not want an average.
> If the vector was {-1, 1, -1, 1, -1, 1, ..., -1, 1} then the average would be 0 and so the
> numbers will not change
It is alright, if situation what you described happens then the avarage being 0 is correct.
It won't be the problem of guess avarage function, it will be the code's problem what is using the function,

> Moreover, ¿what if you output nonsense (like 42) so the numbers change a lot? After all, they > can't leave the [-1, 1] range.
they will change but will not changes being more than 1 or less than -1.
It might start giving the nan values at some point when returning large numbers tho.


The guess avarage function job would be worring about getting the avarage as close as possible to real avarage value.

It doesn't have to worry about being stuck while returning 0.
If the avarage should be 0 then it should be stuck and it will be alright.
Given the while loop that you're using, you can compute the average:
1
2
3
4
5
6
7
8
9
10
11
12
13
class guess_avarage
{
private:
    unsigned count;
    double sum;
public:
    guess_average() : count(0), sum(0) {}
    double guess( double in ) {
        sum += in;
        ++count;
        return sum/count;
    }
};
but since it's a while loop, it doesn't have the end.
We can assume that double 'sum' will eventually lose its precision because it will be getting
larger and larger.

Not the mention of unsigned 'count' getting 0 after reaching the limit.
The program will crash.
Last edited on
We can assume that double 'sum' will eventually lose its precision because it will be getting larger and larger.
It will probably maintain the same precision, which is around 8 most significant digits. Only the digits with very low significance will become noisy. Of course, this happens at all magnitudes for all floating point types.

Not the mention of unsigned 'count' getting 0 after reaching the limit.
The program will crash.
If count happens to be zero at the point of the division, its value will first be promoted to a double, following type promotion rules. Using floating point arithmetic, dividing by zero evaluates to an infinity without throwing an exception, at least by default.
Changing the type of guess_average::count to double eliminates even this problem, since floating point values don't overflow when repeatedly in/decremented.
> Not the mention of unsigned 'count' getting 0 after reaching the limit.
I thought that you wanted to average `nums.size()' numbers.
¿What if you simply limit it to that?


I insist, ¿what's your physic problem?
I insist, ¿what's your physic problem?

I agree. You've posted a very strange solution to a very ordinary problem. Given the infinite loop, how is this supposed to terminate? What is the output?
Topic archived. No new replies allowed.