Math problem

My brain is about to explode. I really can’t get my mind wrapped around this little problem I have. This is in scripting because I’m trying to make a script to calculate this thing.

So here goes:
I have values A, B and C. The individual numbers associated with these can range from 1-10.

For example, lets say that when I add the current numbers together, I get 15 (so the average is 5).

If all of the numbers are equal to the average (all are 5), then that is good.
If one of the numbers is slightly larger than the others (A is 7, B is 4, C is 4), then that is bad.
If one of the numbers is significantly larger than the others (A is 10, B is 1 and C is 4), then that is really bad.

How could I check if one of them is larger than the others and if so, how much is it bigger than the average?

So we want to contain these in something. I would use a basic array for speed’s sake. So we initialize an array of size three. Next we pull these numbers out and average them, storing that in a float. Next we do a for loop so:

for (int i=0;i<3;i++) {
 if (array[i]  == average) {
   //Good
 }
 else if (Mathf.abs(array[i]-average) <= (average+2)){
   //Not so good
  }
  else if (Mathf.abs(array[i]-average) > (average+2) {
   //Bad
  }

I didn’t check all the cases/code, but you should get the jist of the maths.

Thank you for that quick answer. But with that I can know if the individual value is good or bad (by the way, no combination of values produces the “bad” result).

I need to know if ONE of the numbers is BIGGER than the average.

If two numbers are bigger, then it is no problem.

I’m not a very experience coder, but I can usually work around a problem (with the occasional help from the internet wonderland). The problem is that my math skills really suck. I really can’t get it into my head how to do that needed comparison.

How about something like this:

	public int intA, intB, intC;
	int avg, intBig, intDelta, numGT;

	void Start () {
		//Calculate average of 3 numbers
		avg = (intA + intB + intC) / 3;
		
		//Check each if bigger. If so, increase numGT (greater-than-avg counter) and set biggest
		if(intA > avg) { numGT += 1; intBig = intA; }
		if(intB > avg) { numGT += 1; intBig = intB; }
		if(intC > avg) { numGT += 1; intBig = intC; }
		
		//If only 1 number is greater than avg, calculate difference to avg
		intDelta = numGT == 1 ? intBig - avg : 0;
		
		//Print largest number and difference to avg
		print(intBig + "  " + intDelta);
	}

You can convert it to taking an array if you need to.

You can use my code and add in a counting variable that increments if one is bigger than, then when you’re done checking the values, check to see it’s value, if it’s not one, you’re fine.

With a little bit of linq this becomes simple.

        private static bool CheckForOnlyOneValueGreaterThanAverage(IEnumerable<int> list, out float difference)
        {
            difference = 0;
            var values = list as int[] ?? list.ToArray();
            var avg = (float)values.Average();
            var isValid = false;

            var result = values.Where(i => i > avg);
            if (result.Count() == 1)
            {
                difference = result.FirstOrDefault() - avg;
                isValid = true;
            }

            return isValid;
        }

The nice thing is that this way you can pass in any number of values either as an array or as a list to the method and it will return true only if a single value is larger than average. The parameter difference will return how much grater than the average that value was or it will return zero.

By actually combining suggestions from both Slev and JumpDog (as Slev actually suggested) I was able to do exactly what I needed.

Thank you. My brain is saved from exploding.

I didn’t actually test the code chronos78 gave, but thank you too for the input.