2013-04-23 90 views
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; 
} 

在上面,我要吐了学生的#超过60英寸,学生在55in的#,平均高度,最高高度,并在最短的高度。C++最小的序列号不显示

一切工作正常,除了最短的高度。我为这部分代码返回零输出。

这是简单的代码,所以我想这是一个简单的问题,我忽略了。任何输入赞赏。

+0

任何原因,你在使用'X = X + 1'而不是说,'+ + x'? – tadman 2013-04-23 19:24:53

+2

任何原因***所有这些限制都不计算在阵列的*相同*单程中。 – WhozCraig 2013-04-23 19:25:52

+0

使用x = x + 1,因为这就是本章所有例子中的用法(这是C++类的介绍)。所以我尽量让它保持在课程目前的状态。 – Brocktoon 2013-04-23 19:26:34

回答

3

你的循环更改为

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

或从height阵列有第一元件初始化shortest

你的代码不会工作,因为没有高度小于零。

+0

我今天太冷静了,还是不会'最短== 0'在最初的传球中将eval缩短为真? **编辑:**我的坏。我正在看*你的代码*而不是他的。对不起(和+1) – WhozCraig 2013-04-23 19:28:08

+0

而不是测试'最短的== 0'我宁愿测试'x == 0'这更清楚地表达了“*在第一次迭代*”的含义和意图,但也许这只是我。 – syam 2013-04-23 19:33:22

0

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

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

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

以最短的零为例,永远不会有一个学生小于那个(除非你有来自外界的负高度的学生;))。你需要与height[0];
最短初始化此外,在这种情况下,你可以从1个

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

谢谢!这工作,我甚至明白为什么它是错的! – Brocktoon 2013-04-23 19:34:55

1

shortest初始值开始迭代学生是零

double shortest = 0.0;

和循环找不到任何高度小于0

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

被初始化为0.0,如果你的height元素均不小于0.0小,然后你会看到shortest没有改变。请尝试以下操作:

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

Initializa tallestshortestheight[0]height数组作为输入后...