# Finding the closest number to the average and the farthest.

• 11-20-2003
Xenofizz
Finding the closest number to the average and the farthest.
Ok, I'm doing this program for class and have tried a million times to do this, but I just can't. I use an array of 20 numbers, ask the user to enter them, and then take the average. I did that all fine and peachy -- it's easy.

Now he wants us to have the program determine the closest number to the average and the farthest number from the average. I have tried all kinds of for loops and if tests. I just can't get it to work correctly.

Any help would be appreciated. Thanks.
• 11-20-2003
Sebastiani
Post the code you have tried first.

The psuedo code for it would be:

- Cycle through the array
- Subtract the current element from the average.
- if the absolute value of the result is less than the current minimum, assign the value to minimum.
- if the absolute value of the result is greater than the current maximum, assign the value to maximum.

That's it.
• 11-20-2003
neandrake
ok, you will need two variables to start with (along with the array). One to hold the closest, and other for furthest. Then you assign the first number of the array to each. Loop through the array, if the abs(Avg-Closest) > abs(Avg-array[i]) then Closest=array[i]. Flip the greater than sign along with using the Furthest variable and it should also get the furthest number. After the loop, just display Closest and Furthest.
• 11-20-2003
Prelude
Something like this will find the first closest and farthest values from average:
Code:

```#include <iostream> #include <stdlib.h> using namespace std; int main() {   int numbers[] = {1,2,3,4,5,6,7,8,9};   int sum = 0;   int avg;   int i;   for ( i = 0; i < 9; i++ )     sum += numbers[i];   avg = sum / 9;   cout<<"Average: "<< avg <<'\n';   int closest = numbers[0];   int farthest = numbers[0];   for ( i = 1; i < 9; i++ ) {     if ( abs ( avg - numbers[i] ) < abs ( avg - closest ) )       closest = numbers[i];     if ( abs ( avg - numbers[i] ) > abs ( avg - farthest ) )       farthest = numbers[i];   }   cout<<"First closest to average: "<< closest <<'\n';   cout<<"First farthest from average: "<< farthest <<endl; }```

I'm too slow I suppose. :D
[/edit]
• 11-20-2003
neandrake
GAR! Sebastiani, you beat me
• 11-20-2003
Xenofizz
Thanks a ton guys!

Got it to work finally. The absolute value was killing me. I totally forgot about it.
• 11-20-2003
Xenofizz
Crap, one more problem though. I was supposed to use floats and the absolute value is automatically converting them to ints and I'm losing data. Any way to fix this?
• 11-20-2003
neandrake
Not sure, make your own absolute function

Code:

```float absolute(float number) { if (number < 0)   number *= -1; return number; }```
just use absolute() instead of abs() and it should work fine
• 11-20-2003
Xenofizz
Awesome, thanks.
• 11-20-2003
Prelude
>Any way to fix this?
fabs is the equivalent absolute value function for floating-point. It resides in math.h.