【问题标题】:Sum of positive integers, using functions.正整数之和,使用函数。
【发布时间】:2014-10-11 14:17:59
【问题描述】:

我试图将这个问题分解为函数,但我的问题是当我打印出结果时,我总是得到不同的总和、正数和负数。

谁能给我一个提示?

编写一个程序,读取十个整数并输出其中所有正数之和。程序应忽略所有小于或等于 0 的数字。程序还应显示正数的计数和负数或零的计数。

#include <iostream>

using namespace std;
void input(int number, int positiveCount, int negativeCount, int sum);
void output(int positiveCount, int negativeCount, int sum);

int main()
{
int number, positiveCount, negativeCount, sum;
input(number, positiveCount, negativeCount, sum);
output(positiveCount, negativeCount, sum);


return 0;
}
void input(int number, int positiveCount, int negativeCount, int sum)
{
cout << "Enter 10 integers: " << endl;
for (int i = 0; i < 10; i++)
{

    cin >> number;
    if (number > 0)
    {
        positiveCount++;
        sum = sum + number;
    }
    else
    {
        negativeCount++;
    }
 }


}
void output(int positiveCount, int negativeCount, int sum)
{
     cout << sum << endl;
     cout << positiveCount << endl;
     cout << negativeCount << endl;

}

【问题讨论】:

  • 能否详细说明,问题出在哪里

标签: c++ function


【解决方案1】:

您的input() 函数需要通过引用获取其参数,以便它可以修改它们。并且您需要在开始时将所有这些 int 初始化为 0,否则它们包含垃圾。

【讨论】:

  • 没有初始化就是UB,不仅仅是垃圾。
  • @Deduplicator:有点像。我认为第一次更改将阻止 UB,使用引用而不是 input() 函数的值传递。这依赖于获取引用等同于获取其地址的想法,这排除了 UB 并且只给出了一个不确定的值。请参阅此处:stackoverflow.com/questions/11962457/… - 但是我是否正确地通过引用保存我们在这里保存我们的变量,我不确定,并且在任何情况下没有初始化程序都是错误的。
  • 链接的问答是针对 C 而不是 C++。在 C++ 中,使用不确定值是直接的 UB。不需要来自无记忆变量即可。 (你是对的,这不会改变程序是否正确的问题,只会改变它的错误程度。)
【解决方案2】:

您在 input() 函数中所做的操作丢失了,因为变量的范围仅在函数内部。

在将参数传递给 input() 函数时,您需要使用指针或引用,以免使用本地副本。

在使用指针时,您还需要取消引用。 并在传递给函数之前将变量初始化为0。

【讨论】:

    【解决方案3】:

    因为你的程序有严重错误。您在函数main() 中定义了四个局部变量,并在调用函数input() 时按值发送它们。此函数不要修改函数main() 中定义的变量。它只是修改他们的副本。这些副本会在您失去功能时被删除input()

    要修改它们,您应该使用参考:

    void input(int &number, int &positiveCount, int &negativeCount, int &sum);
    

    但是在函数main() 中创建四个整数并在函数input()output() 中发送它们是没有意义的。您可以在input() 中创建四个局部变量,然后在此函数中打印它们。那么你不应该定义函数output(),你可以在你的代码中删除它。 IE。你应该修改你的程序。

    【讨论】:

      【解决方案4】:

      在你的作业中写着:

      编写一个程序,读取十个整数并输出总和 其中所有的正数

      所以没有必要为这个简单的程序编写单独的函数。它看起来像

      #include <iostream>
      
      int main() 
      {
          const size_t N = 10;
      
          std::cout << "Enter " << N << " integers: ";
      
          size_t i = 0, count = 0;
          long long sum = 0;
      
          int num;
      
          for ( ; i < N && std::cin >> num; i++ )
          {
              if ( num > 0 )
              {
                  sum += num;
                  ++count;
              }
          }
      
          std::cout << "You have entered " << count << " positive numbers\n"
                    << "and " << i - count << " negative numbers or seroes\n"
                    << "Sum of positive numbers is " << sum << std::endl;
      
          return 0;
      }
      

      如果你想编写单独的函数,例如函数输入可以声明为

      long long input( size_t &positive_count, size_t &negative_count );
      

      long long input( size_t &total_count, size_t &positive_count );
      

      long long input( size_t *positive_count, size_t *negative_count );
      

      long long input( size_t *total_count, size_t *positive_count );
      

      【讨论】:

        猜你喜欢
        • 2016-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-18
        • 1970-01-01
        • 2014-01-03
        • 1970-01-01
        • 2013-09-16
        相关资源
        最近更新 更多