0

所以我无法从主函数(一个包含您输入的变量的数组)调用数据,并且不知道如何将它传递给一个浮点 getTotal函数。这是我的代码:

#include <iostream>
#include <iomanip>

using namespace std;

float getTotal(float [], int )
   {
     double total = 0;
     for (int i=1; i<ARRAYSIZE; i++)
        {
           total += inputs[i];
        }
     cout << "The total rainfall for the year is " << total << "inches." << endl;
     return total;
   }

float getAverage(float [], int)
   {
     //code goes here
   }

int main()
   {
     const int ARRAYSIZE = 13;
     int inputs[ARRAYSIZE], i=1;
     do
        {
           cout << "Enter the rainfall (in inches) for month #" << i << ": ";
           cin >> inputs[i];
           if ( inputs[i] < 0 )
              {
                  cout << "Please enter a non-negative number for rainfall in month " << i << " :";
                  cin >> inputs[i];
              }
           i++;
        }
     while (i < 13);

     float getTotal(float inputs[], int ARRAYSIZE);
     float getAverage(float inputs[], int ARRAYSIZE);
   }

所以我想从 main 调用数组数据并计算 getTotal 部分中的总数。我尝试了各种方法,但都没有奏效。

4

4 回答 4

0

我们的源代码中有很多错误。例如“int 输入[ARRAYSIZE]”。您的数组是 int 数据类型,但您将此数组作为浮点数据类型传递。存在编译器
错误。其次,您应该在主函数之外声明函数及其数据类型,但
您在主函数内部执行 float getTotal(float inputs[], int ARRAYSIZE); 还有另一个编译器错误。调用函数时,您不需要指定参数的数据类型。所以我写了一个可能的代码,希望它会对你有所帮助。

#include <iostream>
#include <iomanip>

using namespace std;

float getTotal(int *inputs, int ARRAYSIZE )
  {
   double total = 0;
     for (int i=1; i<=ARRAYSIZE; i++)
      {
        total += inputs[i];
      }
      cout << "The total rainfall for the year is " << total << "inches." << endl;
  return total;

}

 float getAverage(int *inputs, int ARRAYSIZE)
    {
     //code goes here

    return 1.4;
   }

 int main()
    {
     const int ARRAYSIZE = 3;
     int inputs[ARRAYSIZE], i=1;
     do
       {
       cout << "Enter the rainfall (in inches) for month #" << i << ": ";
       cin >> inputs[i];
       if ( inputs[i] < 0 )
          {
             cout << "Please enter a non-negative number for rainfall in month " << i     
                 << " :";
              cin >> inputs[i];
          }
       i++;
    }
 while (i <= 3);

     getTotal(inputs, ARRAYSIZE);
     getAverage(inputs, ARRAYSIZE);

return 0;

}

     While calling function, the return value you can store in variable but data type of   

      variable should be the same of return type of the function 

      e.g  float a = getTotal(inputs, ARRAYSIZE);
     float b = getAverage(inputs, ARRAYSIZE);
      or you can directly call this function in cout output command statement.like 

cout<< getTotal(输入,ARRAYSIZE);

于 2013-10-21T06:27:34.343 回答
0
float getTotal(float inputs[], int ARRAYSIZE)
   {
     double total = 0;
     for (int i=1; i<ARRAYSIZE; i++)
        {
           total += inputs[i];
        }
     cout << "The total rainfall for the year is " << total << "inches." << endl;
     return total;
   }

float getAverage(float inputs[], int ARRAYSIZE)
   {
     //code goes here
   }

int main()
   {
     const int ARRAYSIZE = 13;
     int inputs[ARRAYSIZE], i=1;
     do
        {
           cout << "Enter the rainfall (in inches) for month #" << i << ": ";
           cin >> inputs[i];
           if ( inputs[i] < 0 )
              {
                  cout << "Please enter a non-negative number for rainfall in month " << i << " :";
                  cin >> inputs[i];
              }
           i++;
        }
     while (i < 13);

     float fTotal = getTotal(inputs, ARRAYSIZE);
     float fAverage = getAverage(inputs, ARRAYSIZE);
   }
于 2013-10-21T05:20:23.883 回答
0

替换你的函数声明:

float getTotal(float [], int )
float getAverage(float [], int)

对此

float getTotal(int* inputs, int ARRAYSIZE)
float getAverage(int* inputs, int ARRAYSIZE)

而 main() 函数中的这一行:

float getTotal(float inputs[], int ARRAYSIZE);
float getAverage(float inputs[], int ARRAYSIZE);

对此

float getTotal(inputs, ARRAYSIZE);
float getAverage(inputs, ARRAYSIZE);
于 2013-10-21T05:21:41.053 回答
0

嗨@user2901840 对代码稍作修改使其运行良好:

#include <iostream>
#include <iomanip>

using namespace std;

float getTotal(float floatarray[], int size)
{
    float total = 0.0f;
    for (int i=0; i<size; i++)
    {
        total += floatarray[i];
    }
    return total;
}

float getAverage(float floatarray[], int size)
   {
        return (getTotal(floatarray, size))/size;
   }

int main()
   {
        int ARRAYSIZE = 12, i=0;
        float inputs[ARRAYSIZE];
        do
        {
            cout << "Enter the rainfall (in inches) for month #" << i << ": ";
            cin >> inputs[i];
            if ( inputs[i] < 0.0 )
                {
                    cout << "Please enter a non-negative number for rainfall in month " << i << " :";
                    cin >> inputs[i];
                }
            i++;
        }
        while (i < ARRAYSIZE);
        cout << "\n\nThe total rainfall for the year is " << getTotal(inputs, ARRAYSIZE) << " inches." << endl;
        cout << "The average rainfall per month during the year was: " << getAverage(inputs, ARRAYSIZE) << " inches."<< endl;
   }

您需要正确地预定义类,并且需要修改调用。为了澄清我的代码: - 重新编号 i 值以匹配常见的 C++ 约定(对于 12 个值运行 0 到 11) - 重新利用函数以最小化代码 - 将数组重新声明为浮点数组(你最初拥有它作为整数数组)

希望这可以帮助:)

如果您需要更多详细信息或解释,请告诉我,我可以填写更多信息。

于 2013-10-21T09:13:20.797 回答