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

In the above, I need to spit out the # of students over 60in, the # of students over 55in, the average height, the tallest height, and the shortest height.

Everything works fine except for the shortest height. I return an output of zero for that portion of the code.

This is simple code so I imagine it's a simple problem which I'm overlooking. Any input is appreciated.

4

6 回答 6

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

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

shortest = height[0];
for (int x = 1; x < 10; x = x + 1)
{
    if (height[x] > tallest)
    {
                  tallest = height[x];
    }
}
于 2013-04-23T19:26:29.353 回答
3

将循环更改为

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

或使用数组shortest中的第一个元素进行初始化。height

您的代码将不起作用,因为没有小于零的高度。

于 2013-04-23T19:25:55.810 回答
1

Initial value of shortest is zero

double shortest = 0.0;

and the loop can't find any height which is less than 0.

于 2013-04-23T19:28:04.933 回答
1
for (int x = 0; x < 10; x = x + 1)
{
    if (height[x] < shortest)
    {
         shortest = height[x];
    }
}

since shortest is initialized to 0.0, if your height elements are all not smaller than 0.0, then you will see shortest not changed. try the following:

shortest = height[0];
for (int x = 1; x < 10;  ++x)
{
    if (height[x] < shortest)
    {
         shortest = height[x];
    }
}
于 2013-04-23T19:29:00.613 回答
0

Initializa tallest and shortest to height[0] after you get height array as input...

于 2013-04-23T19:29:14.207 回答
0

除非您有负身高的学生,否则您的比较可能是错误的。

将“最短”高度设置为更大的值,否则比较永远不会成立。

于 2013-04-23T19:26:28.850 回答