0

我正在尝试编写一个程序来计算和显示身高超过 60 英寸的学生的身高。例如:我需要它来统计和显示身高超过 60 英寸的学生人数并显示他们各自的身高。我不确定如何存储单独的值并显示它们的高度。我已经让程序计算身高超过 60 英寸的学生人数,但我需要帮助来显示他们的具体身高。

#include <iostream>

using namespace std;

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

    cout << "You are asked to enter heights of 10 students. "<< endl;
    for (int x = 0; x < 10; x = x + 1)
    {
        cout << "Enter height of a student: ";
        cin >> height[x];
    if (height[x] > 60)
    {
        count60 = count60 + 1;
    }       
    }

    cout << "The number of students taller than 60 inches: "<< count60 << endl;
    cout << "The heights of these students are: "

    system("pause"); 
    return 0;
}
4

5 回答 5

3

不确定我是否完全理解您的问题所在。

从您提供的代码中可以清楚地看出,您知道如何:

  • 遍历一个数组(您for的输入语句);
  • 确定某物是否大于 60(您if的更新计数声明);和
  • 输出一个变量(你的倒数第二个cout <<语句。

因此,将它们与以下内容结合起来应该是一件简单的事情:

for (int x = 0; x < 10; x = x + 1) {
    if (height[x] > 60) {
        cout << height << '\n';
    }
}
于 2013-04-17T02:35:41.720 回答
1

这是代码:

#include <iostream>

using namespace std;

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

cout << "You are asked to enter heights of 10 students. "<< endl;
for (int x = 0; x < 10; x = x + 1)
{
    cout << "Enter height of a student: ";
    cin >> height[x];
if (height[x] > 60)
{
    count60 = count60 + 1;
}       
}

cout << "The number of students taller than 60 inches: "<< count60 << endl;
cout << "The heights of these students are: ";
for(int i=0;i<10;++i)
    if(height[i]>60)
        cout<<' '<<height[i];
cout<<endl;

return 0;

}

顺便说一句,我认为 count60 最好是一个无符号整数。

于 2013-04-17T02:40:42.643 回答
1

尝试使用std::vector. 它们基本上是数组的包装器,允许您动态添加值。在这种情况下,您将添加代码:

#include <vector> // obviously with the rest of the includes.

std::vector<int> tallPeople;
for (int x = 0; x < 10; x = x + 1)
{
    if (height[x] > 60)
    {
        count60 = count60 + 1;
        tallPeople.push_back(height[x]);
    }   
}

//...

for (int num = 0; num < tallPeople.size(); num++)
{
    cout << tallPeople[num] << endl;
}
于 2013-04-17T02:40:54.707 回答
1

给你...在空间利用率方面不是最好的,但避免了 STL。

        #include <iostream>

        using namespace std;

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

            cout << "You are asked to enter heights of 10 students. "<< endl;
            for (int x = 0; x < 10; x = x + 1)
            {
                cout << "Enter height of a student: ";
                cin >> height[x];
            if (height[x] > 60)
            {
                maxheight[count60] = height[x];
                count60 = count60 + 1;
            }       
            }

            cout << "The number of students taller than 60 inches: "<< count60 << endl;

            for (int i = 0; i < count60; i = i + 1)
            {
               cout<<"The heights of these students are: "<< maxheight[i] << endl;
            }

            system("pause"); 
            return 0;
        }
于 2013-04-17T02:42:25.683 回答
0

如果要删除重复项,请执行此操作

//bubble sort
for(int i=0;i<9;i++)
{
    for(int j=i+1;j<10;j++)
    {
        if(height[i]>height[j])
        {
            double temp = height[i];
            height[i] = height[j];
            height[j] = temp;
        }       
    }
}   
//print
for(int i=0;i<10;i++)
{
    if(height[i]>60 && (i==0 || height[i]!= height[i-1]))   
    {
        cout << ' ' << height[i];
    }   
}
于 2013-04-17T02:54:14.167 回答