找到最大,最小,正面和负面

时间:2013-04-25 00:58:27

标签: c++

我写了这段代码,我希望它能找到负数,最小数字,最大数字,数字平均数和数字总和的数字数

它不起作用,你能帮我使用if声明吗?我找不到最小或最大(当数字为负数时)+如果所有数字都是负数或正数或混合则无法找到。

#include <iostream>
using namespace std;

void main() {
    float num,
        average,
        sum,
        smallest,
        largest,
        positive,
        negative;

    int count;
    sum = 0;
    count = 1;
    largest = 0;
    smallest = 0;
    negative = 0;

    while (count < 11) {
        cout << "Number #" << count << ": ";
        cin >> num;

        if (num < 0) negative++;

        sum += num;
        average = (sum / 10);

        if (num > largest) largest = num;
        else if (num < smallest) smallest = num;

        count++;
    }

    cout << endl << "The smallest number is " << smallest << endl;
    cout << "The largest number is " << largest << endl;
    cout << "The average of all numbers is " << average << endl;
    cout << "The sum of all numbers is " << sum << endl;
    cout << endl;
    cout << "There are " << negative << " negative numbers." << endl;


    if (num > 0) cout << "All numbers are positive.";
    else if (sum < 0) cout << "All numbers are negative.";
    else if {
        cout << "There is a mixture of numbers.";
    }
}

// ------------------------------------ 的修改

我做了一些改变,我几乎做对了,但我仍然找不到找到最小和最大的方法

using namespace std;

void main()
{

float num,
average,
sum,
largest,
smallest,
positive,
negative;

int count;
sum = 0;
count = 1;
negative = 0;

largest= 0;
smallest = 0;

while ( count < 11)
{
  cout << "Number #" << count << ": ";
  cin >> num;

  if ( num < 0 )negative++;

  sum += num;

  if (count != 0 )
  {
     if ( num > largest )
        largest = num;
     else if ( num < smallest )
        smallest = num;
  }

  count++;
} 

average = ( sum / 10 );


cout << endl << "The smallest number is " << smallest << endl;
cout << "The largest number is " << largest << endl;
cout << "The average of all numbers is " << average << endl;
cout << "The sum of all numbers is " << sum << endl;
cout << endl;
cout << "There are " << negative << " negative numbers." << endl;

 if ( negative == 0 )
    cout << "All numbers are positive.";
 else if ( negative == 10 )
    cout << "All numbers are negative.";
 else 
 {
    cout << "There is a mixture of numbers.";
 }
}

1 个答案:

答案 0 :(得分:2)

在拥有所有数字之前,你不应该计算平均数;或者如果你想保持一个移动平均线,你必须除以你到目前为止的总和。所以而不是行

average = sum / 10;

在循环中间,尝试

average = sum / count;

在循环内部,或

average = sum / (count - 1);

循环之后。当然,因为你没有实际使用平均值直到循环完成,它实际上并不重要 - 但它有点草率。

接下来,你的代码

if ( num > 0 )
   cout << "All numbers are positive.";
else if ( sum  < 0 )
   cout << "All numbers are negative.";

有两个明显的错误。首先 - 你检查&#34; num&#34;是肯定的 - 这是输入的最后一个数字,而不是&#34;所有数字&#34;。如果你有一个非常负数的话,总和将是负数。而是使用您的负数计数:

if ( negative == 0) // all positive
if ( negative == 10) // all negative

否则,它是一种混合物。我相信你可以用这些指针来修复你的代码。

编辑#2

我决定在一个完整的程序中实现我上面提到的所有更改,以及我在评论中提到的那些更改。我最终得到的代码是:

#include <iostream>
using namespace std;

int main()
{

float num,
  average,
  sum=0,
  largest=0,
  smallest=0,
  positive=0,
  negative=0;

int count=1;

negative = 0;

while ( count < 11)
{
  cout << "Number #" << count << ": ";
  cin >> num;

  if ( num < 0 ) negative++;

  sum += num;

  if (count == 1) {
    largest = num;
    smallest = num;
  }
  else {
     if ( num > largest )
        largest = num;
     if ( num < smallest )
        smallest = num;
  }

  count++;
}

average = ( sum / (count - 1) );


cout << endl << "The smallest number is " << smallest << endl;
cout << "The largest number is " << largest << endl;
cout << "The average of all numbers is " << average << endl;
cout << "The sum of all numbers is " << sum << endl;
cout << endl;
cout << "There are " << negative << " negative numbers." << endl;

if ( negative == 0 )
  cout << "All numbers are positive."<<endl;
else if ( negative == 10 )
  cout << "All numbers are negative."<<endl;
else
{
  cout << "There is a mixture of numbers.";
}

}

这编译好;当我运行它并输入以下输入时:

Number #1: 5
Number #2: 4
Number #3: 3
Number #4: 2
Number #5: 1
Number #6: 0
Number #7: -1
Number #8: -2
Number #9: -3
Number #10: -4

我得到了这个输出:

The smallest number is -4
The largest number is 5
The average of all numbers is 0.5
The sum of all numbers is 5

There are 4 negative numbers.
There is a mixture of numbers.

让我觉得它在工作。它对你有用吗?