【问题标题】:Need help displaying values in a loop / array program C++需要帮助在循环/数组程序 C++ 中显示值
【发布时间】:2013-04-17 02:32:26
【问题描述】:

我正在尝试编写一个程序来计算和显示身高超过 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;
}

【问题讨论】:

  • 您应该考虑将增量写为“++x”,而不是“x = x + 1”。这是更好的风格。此外,count60 应该是 int,而不是 double。

标签: c++ arrays loops output


【解决方案1】:

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

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

  • 遍历数组(您的for 语句作为输入);
  • 决定是否大于 60(您的 if 更新计数语句);和
  • 输出一个变量(倒数第二个cout &lt;&lt; 语句。

因此,将这些与以下内容结合起来应该很简单:

for (int x = 0; x < 10; x = x + 1) {
    if (height[x] > 60) {
        cout << height << '\n';
    }
}

【讨论】:

  • 只是补充一下,使用 '\n' 而不是 endl 将意味着每次调用后缓冲区不会刷新。
  • 是的,我怀疑,像 C 一样,如果它要去一个终端(如果它可以“确定引用一个交互式设备”),它将每行刷新。如果不是,则不需要换行。无论如何,这里都无关紧要,因为不需要刷新(在这种情况下 endl 效率低下)。
【解决方案2】:

给你...在空间利用率方面不是最好的,但避免了 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;
        }

【讨论】:

    【解决方案3】:

    代码如下:

    #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 最好是 unsigned int。

    【讨论】:

      【解决方案4】:

      尝试使用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;
      }
      

      【讨论】:

        【解决方案5】:

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

        //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];
            }   
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-25
          • 1970-01-01
          相关资源
          最近更新 更多