gpt4 book ai didi

C++ 数组中的最小数字不显示

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:27:09 25 4
gpt4 key购买 nike

#include <iostream>

using namespace std;

int main()
{
double sixty = 0.0;
double fiftyfive = 0.0;
double height[10];
double tallest = 0.0;
double shortest = 0.0;
double average = 0.0;
double total = 0.0;

for (int x = 0; x < 10; x = x + 1)
{
height[x] = 0.0;
}

cout << "Please enter the heights of ten students. "<< endl;
for (int x = 0; x < 10; x = x + 1)
{
cout << "Enter height of a student: ";
cin >> height[x];
}

for (int x = 0; x < 10; x = x + 1)
{
if (height[x] > 60)
{
sixty = sixty + 1;
}
}
for (int x = 0; x < 10; x = x + 1)
{
if (height[x] < 55)
{
fiftyfive = fiftyfive + 1;
}
}
cout << "The number of students over 60 inches in height: " << sixty << endl;
cout << "The number of students under 55 inches in height: " << fiftyfive << endl;

for (int x = 0; x < 10; x = x + 1)
{
if (height[x] > tallest)
{
tallest = height[x];
}
}
cout << "The tallest student is: " << tallest << endl;

for (int x = 0; x < 10; x = x + 1)
{
if (height[x] < shortest)
{
shortest = height[x];
}
}
cout << "The shortest student is: " << shortest << endl;

for (int x = 0; x < 10; x = x + 1)
{
total = total + height[x];
}
average = total / 10;
cout << "The average student height is: " << average << endl;



system("pause");
return 0;
}

在上面,我需要吐出60in以上学生人数,55in以上学生人数,平均高度,最高高度,最矮高度。

除最短高度外,一切正常。我为该部分代码返回零输出。

这是一个简单的代码,所以我想这是一个我忽略的简单问题。感谢任何输入。

最佳答案

    if (height[x] < shortest)
{
shortest = height[x];
}

如果最矮为零,则永远不会有比这更小的学生(除非你有来自外部领域的负高度的学生;))。您需要使用 height[0];
初始化最短同样在这种情况下,您可以从 1 开始迭代学生

shortest = height[0];
for (int x = 1; x < 10; x = x + 1)
{
if (height[x] > tallest)
{
tallest = height[x];
}
}

关于C++ 数组中的最小数字不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16177552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com