我在将数组的第一个输入移动到最后时遇到问题,这样我就可以计算Window输入数的平均值。我必须使用数组(不是矩阵)来计算权重的移动平均值。问题:如果我将权重数量设置为5,输入5个权重,然后执行算术我得到第1天的正确平均值,但是,每天之后不正确。任何人都可以在这里提供指导和/或指向我允许我执行我的代码行的程序或网站,这样我就可以更有效地解决这个问题。谢谢!
#include <iostream>
#include <cstdlib>
using namespace std;
const int MAX_NUM_WEIGHTS = 50;
// Function prototypes:
void movingAverage(int& numWeights, int weightArray[], int& Window);
int main()
{
int numWeights, Window;
int weightArray[MAX_NUM_WEIGHTS];
movingAverage(numWeights, weightArray, Window);
return 0;
}
void movingAverage(int& numWeights, int weightArray[], int& Window)
{
int i = 0, j = 0, day = 0, sum = 0, temp;
double avg;
cout << "Enter the number of weights: ";
cin >> numWeights;
cout << endl;
cout << "Enter the weights: ";
for(i = 0; i < numWeights; i++) {
cin >> weightArray[i];
}
cout << "Enter the length of the window: ";
cin >> Window;
cout << endl << endl;
while(k < numWeights - 1) {
for(j = 0; j < Window; j++) {
sum += weightArray[j];
}
avg = static_cast<double>(sum) / Window;
day = day + 1;
cout << day << ": " << avg << endl;
int temp = weights[i];
weights[i] = weights[numWeights];
if(k >= (numWeights - 1)) {
break;
}
}
}