2013-04-17 42 views
0

我正在嘗試編寫一個程序來計算和顯示高於60英寸的學生的高度。例如:我需要它來計算並顯示高於60英寸的學生人數並顯示他們各自的高度。我不確定如何存儲單獨的值並顯示它們的高度。我已經掌握了計算高於60英寸的學生人數的計劃,但我需要幫助來顯示他們的具體身高。需要幫助顯示循環/數組程序中的值C++

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

你應該考慮寫增量爲 「++ X」,而不是 「X = X + 1」。這是更好的風格。另外,count60應該是一個整數,而不是雙倍。 – Muscles

回答

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; 
     } 
3

不知道我完全明白你的問題所在。

這是從你給的代碼清楚你知道如何:

  • 迭代通過一個數組(您for語句輸入);
  • 決定是否大於60(您的if語句更新計數);以及
  • 輸出變量(你倒數第二cout <<聲明

因此,它應該是一個簡單的事情,喜歡的東西結合起來的:

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

只需添加,使用'\ n'而不是endl將意味着緩衝區在每次調用後都不會被刷新。 – iambeanie

+0

是的,我懷疑,像C一樣,如果它要發送到終端(如果它可以「確定指向交互設備」),它將被刷新爲每行。如果不是,則不需要換行。在任何情況下,這都是無關緊要的,因爲不需要衝洗(在這種情況下endl效率不高)。 – paxdiablo

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

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