所以我无法从主函数(一个包含您输入的变量的数组)调用数据,并且不知道如何将它传递给一个浮点 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 部分中的总数。我尝试了各种方法,但都没有奏效。