在c ++中查找中间值

时间:2018-02-05 07:36:22

标签: c++

我的代码中有一部分让我感到困惑。首先,我试图找到4个给定数字的最小值和最大值(随机)。然后我需要找到中间数字的平均值,这是我的代码,任何帮助将不胜感激!

我想我应该使用我在第一部分中所做的最小和最大功能?但我不知道该怎么做

#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;


}

4 个答案:

答案 0 :(得分:1)

由于您只有四个号码,您可以这样做:

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

答案 1 :(得分:1)

首先对您的数字进行某种排序,然后您将轻松获得最小值和最大值。之后只需添加中间数字并将其与中间数字的总数相减。

答案 2 :(得分:1)

您应该开始学习标准模板库,您可以这样做:

#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";
}

输出:

middle of 1,2,3,4: 2.5

使用stl算法可以使您的代码更具表现力,更易于理解。我建议你从一开始就学习它们

答案 3 :(得分:0)

您可以累积minmax函数值:

  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;

或者如果您使用的是c ++ 11,则可以这样做:

 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;

在此处采取行动:https://ideone.com/69nRvo