我有一段代码应该返回一组数组中的最高整数,每个数组包含三个整数。目前我的代码还没有运作。有人可以帮我找到我的错误吗?
这是我的代码:
#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;
}
答案 0 :(得分:2)
由于数组大小相同,因此无需单独处理每个最高级别。
让最高存储在当前的最高点,并被下一个最高点覆盖。
为什么不呢:
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。
答案 1 :(得分:1)
至少有一个问题出现在这个重要的部分
中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);
}
我建议使用vector而不是array。
答案 2 :(得分:0)
我编写了一个函数,它返回数组中的最大数字。您所要做的就是在所有阵列上运行该函数,保存每个阵列中的最高值并再次运行该函数。
#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;
}
答案 3 :(得分:0)
执行以下操作;
替换所有&#34;否则如果&#34;声明&#34;如果&#34;声明,并删除最后一个&#34;否则&#34;言。
,或者
用此替换你的功能,
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 :(得分:0)
使用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;