Yunxiao Jia
Yunxiao Jia

Reputation: 29

Finding middle value in c++

There's a part of my code that I got confused about. First I tried to find the min and max value of 4 given numbers(randomly). Then I need to find the average value of middle numbers, here's my code, any help will be appreciated thanks!

I think I am supposed to use min and max functions I did in the first part? but I am not sure how to do that

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int result;

int min(int a, int b, int c, int d)
{
    result = a;
    if (b < result) result = b;
    if (c < result) result = c;
    if (d < result) result = d;
    return result;
}

int max(int a, int b, int c, int d)
{
    int result = a; 
    if (b > result) result = b;
    if (c > result) result = c;
    if (d > result) result = d;
    return result;
}

int main()
{
    int Min,Max;
    Min = min(2,6,3,4);
    cout << " The result for minimum is " << Min <<endl;
    Max = max(2,6,3,4);
    cout << " The result for maximum is " << Max;
}

/**
  Computes the average of the middle values of four given values
  (that is, without the largest and smallest value).
  Hint: Use the given min function. You may also want to define a
  max helper function or take advantage of the fact that max can be
  computed from the min of the negative values.
**/

/** things got confused after this...the first part is actually finished with the help from my other question **/

    int mv1;

double middle(int a, int b, int c, int d)
{
    int  mv1=a;
    if(a<=b)
        return a;
}

int main1()
{

    int x;
    int y;
    int z;
    int n;
    int md;

    cout << "Please enter a number: ";
    cin>>x;
    cout << "Enter another number:  ";
    cin>>y;
    cout << "Enter the third number:  ";
    cin>>z;
    cout << "Enter the fourth number:  ";
    cin>>n;

    md=middle(x,y,z,n);
    cout << endl<<"middle is "<<md <<endl;
    return 0;


}

Upvotes: 0

Views: 6824

Answers (4)

bashrc
bashrc

Reputation: 4835

You can accumulate the min and max function values:

  int max(int a, int b, int c, int d) { return std::max(std::max(a,b),                   
                                                        std::max(c, d));
                                      }
  int min(int a, int b, int c, int d) { return std::min(std::min(a,b),                   
                                                        std::min(c, d));
                                      }
  int mid = ((a+b+c+d) - max(a,b,c,d) - min(a,b,c,d))/2;

Or if you are on c++11 you can do this as:

 auto values = {a,b,c,d};
 decltype(values.begin()) mn, mx;
 std::tie(mn, mx) = std::minmax_element(values.begin(), values.end());
 auto median = (std::accumulate(values.begin(), values.end(), 0) - *mn - *mx) / 2;

In action here: https://ideone.com/69nRvo

Upvotes: 1

Blasco
Blasco

Reputation: 1745

You should start learning the standard template library, you could do something like this:

#include <vector>
#include <algorithm>
#include <iostream>

double median(int a, int b, int c, int d)
{
    std::vector<int> v{a,b,c,d};
    std::sort(v.begin(),v.end());
    return static_cast<double>(v[1]+v[2])/2;
}

int main() {
    std::cout << "median of 1,2,3,4: " << median(1,2,3,4) << "\n";
}

which outputs:

middle of 1,2,3,4: 2.5

Using the stl algorithms will make your code more expressive and easier to understand. I would recommend you to learning them from the beginning

Upvotes: 4

Paresh Dhandhukiya
Paresh Dhandhukiya

Reputation: 212

do some kind of sorting on your numbers first,then you will get minimum and maximum easily.After that just add middle numbers and subtract it with total count of middle numbers.

Upvotes: 2

Sorin
Sorin

Reputation: 11968

Since you only have four numbers you can do:

float median = (sum(...) - min(...) - max(...)) / 2.0;

Upvotes: 1

Related Questions