0

I'm having problems moving the first input of my array to the end so that I can compute the average of the Window number of inputs. I have to use an array (not a matrix) to compute the moving average of weights. Question: if I set my number of weights to 5, input 5 weights, and then execute the arithmetic I get the correct average for the 1st day, however, everyday after that is incorrect. Can anyone lend guidance here and/or point me to a program or website that will allow me to execute my code line for line so that I can trouble shoot this problem more effectively. Thanks!

#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;
         }
     }
 }
BBlove
  • 9
  • 1

0 Answers0