【问题标题】:Array function doesn't seem to be finding the highest value数组函数似乎没有找到最高值
【发布时间】:2017-05-05 06:02:08
【问题描述】:

我有一段代码应该从一组包含三个整数的数组中返回最大的整数。目前我的代码不起作用。有人可以帮我找到我的错误吗?

下面是我的代码:

#include <iostream>
using namespace std;

void HighestScore(int[], int[], int[]);

int main() {
    const int SIZE = 3;
    int Stu1[SIZE] = {70, 80, 90},
        Stu2[SIZE] = {71, 81, 91},
        Stu3[SIZE] = {72, 82, 92},
        Stu4[SIZE] = {73, 83, 93},
        Stu5[SIZE] = {74, 84, 94};


    HighestScore(Stu1,Stu2,Stu3);


    return 0;
}



void HighestScore(int St1[], int St2[], int St3[])
{
    const int SIZE =3;
    int count;

    int high1 = St1[0];
    int high2 = St2[0];
    int high3 = St3[0];
    int highest =0;

    for (count=1;count<SIZE;count++)
    {
        if(St1[count] > high1)
            {high1 = St1[count];}
        if(St2[count] >high2)
            {high2 = St2[count];}
        if(St3[count] >high3)
            {high3 = St3[count];}

    }

    if(high1>high2)
        {highest=high1;}
    else if (high1>high3)
        {highest=high1;}
    else if (high2>high1)
        {highest=high2;}
    else if (high2>high3)
        {highest=high2;}
    else if (high3>high1)
        {highest=high3;}
    else if (high3>high2)
        {highest=high3;}
    else
        {highest=-1;}

    cout << highest;
    return;
}

【问题讨论】:

  • high1high2high3 是不必要的。您可以拥有SIZE 的单个文件静态副本。考虑使用向量。与未知数量的一维数组相比,二维数组更容易扩展以添加学生(或二维向量)。
  • 你的老师还没讲过二维数组吗?
  • 你做的工作量太大,宁愿接受标准算法并了解std::max_element。它以一种更安全、更简单、更符合 C++ 习惯的方式为您完成所有这些工作。

标签: c++ arrays function


【解决方案1】:

由于数组大小相同,您无需分别处理每个最大值。

highest 存储此刻的最高值并被下一个最高值覆盖。

为什么不只是:

 int HighestScore(int St1[], int St2[], int St3[])
{
const int SIZE =3;
int count;
//make sure you have #include <limits.h> in the beginning
int highest = INT_MIN; //---lowest value of an integer in C;


for (count=0;count<SIZE;count++)
{
    if(St1[count] > highest)
        highest = St1[count];
    if(St2[count] > highest) 
        highest = St2[count];
    if(St3[count] > highest) 
        highest = St3[count];
}
cout << highest;
return highest;
}

对于最高的初始值,取整数的最低可能值,这样可以保证覆盖。

整数的最低可能值也可以在limits.h中作为INT_MIN获得。

【讨论】:

  • 不应数 = 0?
  • 已编辑。谢谢。
  • @dev8080 此代码不适用于仅由负值组成的数组。例如。 [-5, -3, -2], [-1, -1, -1], [-6, -10, -11] 您的代码将返回 0。这些不是无符号数组。
  • @paweldac 是的。已编辑,现在使用整数的最小值。
  • @M.M 最好使用 numeric_limits lib en.cppreference.com/w/cpp/types/numeric_limits 而不是某些 c 宏。
【解决方案2】:

这个大的if部分至少有一个问题

if(high1>high2)
    {highest=high1;}
else if (high1>high3)
    {highest=high1;}
else if (high2>high1)
    {highest=high2;}
else if (high2>high3)
    {highest=high2;}
else if (high3>high1)
    {highest=high3;}
else if (high3>high2)
    {highest=high3;}
else
    {highest=-1;}

假设high1 = 3,high2 = 2,high3 = 10,if部分选择high1作为最大值,因为下面的分支虽然最高应该是high3

if(high1>high2)
    {highest=high1;}

更好的方法是创建一个辅助函数 find_max,它返回数组的最大值,然后使用它来查找 3 个数组的最大值 伪代码应该是

int find_max(int a[]);

int HighestScore(int st1[], int st2[], int st3[]){
    int tmp[] = {find_max(st1), find_max(st2), find_max(st3)};
    return find_max(tmp);
}

我建议使用向量而不是数组。

【讨论】:

    【解决方案3】:

    执行以下操作;

    用“if”语句替换所有“else if”语句,并删除最后一个“else”语句。

    或者,

    用这个替换你的函数,

    void HighestScore(int St1[], int St2[], int St3[])
    {
    const int SIZE =3;
    int count;
    
    int high1 = St1[0];
    int high2 = St2[0];
    int high3 = St3[0];
    int highest;
    
    for (count=1;count<SIZE;count++)
    {
        if(St1[count] > high1)
            {high1 = St1[count];}
        if(St2[count] >high2)
            {high2 = St2[count];}
        if(St3[count] >high3)
            {high3 = St3[count];}
    }
    
    highest=high1;
    if(high2>highest)
        {highest=high2;}
    if (high3>highest)
        {highest=high3;}
    
    
    cout << highest;
    return;
    

    }

    【讨论】:

      【解决方案4】:

      使用 std,您可以这样做:

      const auto highest = std::max({
              *std::max_element(std::begin(Stu1), std::end(Stu1)),
              *std::max_element(std::begin(Stu2), std::end(Stu2)),
              *std::max_element(std::begin(Stu3), std::end(Stu3)),
          });
      std::cout << highest << std::endl;
      

      Demo

      【讨论】:

        【解决方案5】:

        我写了一个函数,它返回数组中的最大数字。您所要做的就是在所有数组上运行该函数,保存每个数组中的最高值并再次运行该函数。

        #include <iostream>
        using namespace std;
        
        int HighestScore(int Stu[], int length);
        
        int main() {
            const int SIZE = 3;
            int Stu1[SIZE] = {70, 80, 90};
            int Stu2[SIZE] = {71, 81, 91};
            int Stu3[SIZE] = {72, 82, 92};
        
            int highestArr[3];
            int highest;
        
            highestArr[0] = HighestScore(Stu1, SIZE);
            highestArr[1] = HighestScore(Stu2, SIZE);
            highestArr[2] = HighestScore(Stu3, SIZE);
        
            highest = HighestScore(highestArr, 3);
        
            cout <<  highest << std::endl;
        
            return 0;
        }
        
        int HighestScore(int Stu[], int length) {
            int highest = 0; // Init to 0 since mark can't be lower than 0
            for(int i = 0; i < length; i++) {
                if(highest < Stu[i]) {
                    highest = Stu[i];   
                }
            }
        
            return highest;
        }
        

        【讨论】:

        • (但是,如果你想花哨,我建议std::numeric_limits&lt;int&gt;::min()而不是试图猜测。)
        • @Donnie 感谢您的帮助!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-06
        • 1970-01-01
        • 2017-11-13
        • 1970-01-01
        • 2012-10-04
        相关资源
        最近更新 更多