Thread: Finding the closest number to the average and the farthest.

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    10

    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.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    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.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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;
    }
    [edit]
    I'm too slow I suppose.
    [/edit]
    My best code is written with the delete key.

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    GAR! Sebastiani, you beat me
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    Thanks a ton guys!

    Got it to work finally. The absolute value was killing me. I totally forgot about it.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    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?

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    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
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    Awesome, thanks.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Any way to fix this?
    fabs is the equivalent absolute value function for floating-point. It resides in math.h.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed