簡體   English   中英

需要幫助在循環/數組程序C ++中顯示值

[英]Need help displaying values in a loop / array program C++

我正在嘗試編寫一個程序來計算和顯示高於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;
}

不確定我是否完全理解您的問題所在。

從您提供的代碼中您可以清楚地知道如何:

  • 遍歷數組(你for輸入語句);
  • 決定是否大於60(您的if語句用於更新計數);
  • 輸出一個變量(您的倒數第二個cout <<語句。

因此,將它們與類似的東西組合起來應該是一件簡單的事情:

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

這是代碼:

#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最好是一個無符號的int。

嘗試使用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;
}

在這里,您可以...在空間利用率方面不是最好的選擇,但避免使用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;
        }

如果您要刪除重復項,請執行此操作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM