#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++ - C++数组中最小的数字不显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16177552/

10-11 21:17