# Moving Average Question

• 04-23-2007
GCNDoug
Moving Average Question
I have figured out everything I need for this program except for one thing. I need to create a moving average. I'm working with 2 stocks and I need to keep a moving average for each one. And then store the 16 values in an array.

I'm am working with stock prices of two difference stocks for 20 days.

So for instance
stock 1 avg day 1 through 5
stock 1 avg day 2 through 6
...... and so on until all 16 values for the array are filled.

and

stock 2 avg day 1 through 5
stock 2 avg day 2 through 6
and so on...

below is my entire program I will bold the function I am having the issue with.

(It's the calc_Mov_Average function)

Any help would be great! Due in an hour and a half.

Code:

```//Doug Miller //Project #4 - Working with stock information and organizing them. #include <stdio.h> #include <conio.h> #include "util.h" #define MAX_STOCKS 20 #define MOV_MAX 16 float stock1[MAX_STOCKS] = {34.25,40.50,36.50,40.00, 30.25,30.25,35.50,36.00, 34.25,37.00,34.00,35.00, 36.25,34.25,40.50,41.50, 41.50,40.00,36.50,34.50}; float mov_Stock1[MOV_MAX] = {0.0}; float  stock2[MAX_STOCKS] = {40.25,38.50,34.50,33.50, 30.50,29.75,37.50,38.00, 34.75,38.00,34.25,37.00, 34.25,37.50,34.50,38.50, 37.50,37.25,38.25,37.50}; float mov_Stock2[MOV_MAX] = {0.0}; //Report 1 Functions int first_Greater (float stock1[], float stock2[]); int second_Greater (float stock1[], float stock2[]); int same_Price (float stock1[], float stock2[]); void print_Report1(int first_Greater, int second_Greater, int same_Price); //END OF REPORT 1 FUNCTIONS //Report 2 Functions double average_First (float stock1[]); double average_Second (float stock2[]); int exceeded_Average_First (float stock1[], double average_First); int exceeded_Average_Second (float stock2[], double average_Second); void print_Report2 (double average_First, double average_Second, int exceeded_Average_First, int exceeded_Average_Second); //END OF REPORT 2 FUNCTIONS //Report 3 Functions void calc_Mov_Average (float stock[], float movAvg[]); void print_Report3 (float mov_Stock1[], float mov_Stock2[]); //END OF REPORT 3 FUNCTIONS //Report 4 Functions int first_Mov_Greater (float mov_Stock1[], float mov_Stock2[]); int second_Mov_Greater (float mov_Stock1[], float mov_Stock2[]); int mov_Same (float mov_Stock1[], float mov_Stock2[]); void print_Report4 (int first_Mov_Greater, int second_Mov_Greater, int mov_Same); //END OF REPORT 4 FUNCTIONS   int main() {                                                                                         //BEGINNING OF REPORT 1 VARIABLES         int first_Greater_C = 0;         int second_Greater_C = 0;         int same_C = 0;                                                         //END OF REPORT 1 VARAIABLES                                                                                                         //BEGINNING OF REPORT 2 VARIABLES         double average_First_C = 0.0;         double average_Second_C = 0.0;         int exceeded_Average_CF = 0;         int exceeded_Average_CS = 0;                                                         //END OF REPORT 2 VARAIABLES                                                         //BEGINNIGN OF REPORT 4 VARIABLES         int mov_First_Greater_C = 0;         int mov_Second_Greater_C = 0;         int mov_Same_C = 0;                                                         //END OF REPORT 4 VARAIBALES                                                                                                                                 //BEGINNING OF REPORT 1 CALLS         //Print Title         PrintTitle();         //Print Date         PrintDate();         //Find First Stock Being Greater         first_Greater_C = first_Greater(stock1, stock2);         //Find Second Stock Being Greater         second_Greater_C = second_Greater(stock1, stock2);         //Finding The Stocks To Be The Same         same_C = same_Price(stock1, stock2);         //Printing Report 1         print_Report1(first_Greater_C, second_Greater_C, same_C);         //Pause Screen         PauseScreen();                                                         //END OF REPORT 1 CALLS                                                         //BEGINNING OF REPORT 2 CALLS                 //Print Title         PrintTitle();         //Print Date         PrintDate();                 //Find Average FIRST DAY         average_First_C = average_First(stock1);         //Find Average SECOND DAY         average_Second_C = average_Second(stock2);         //Exceeded Average FRIST DAY         exceeded_Average_CF = exceeded_Average_First(stock1, average_First_C);         //Exceeded Average SECOND DAY         exceeded_Average_CS = exceeded_Average_Second(stock2, average_Second_C);         //Printing Report 2         print_Report2(average_First_C, average_Second_C, exceeded_Average_CF, exceeded_Average_CS);         //Pause Screen         PauseScreen();                                                         //END OF REPORT 2 CALLS                                                         //BEGINNING OF REPORT 3 CALLS                 //Print Title         PrintTitle();         //Print Date         PrintDate();                 //Find floating average first stock         calc_Mov_Average(stock1, mov_Stock1);         //Find floating average second stock         calc_Mov_Average(stock2, mov_Stock2);         //Printing Report 3         print_Report3(mov_Stock1, mov_Stock2);         //Pause Screen         PauseScreen();                                                         //END OF REPORT 3 CALLS                                                         //BEGINNING OF REPORT 4 CALLS         //Print Title         PrintTitle();         //Print Date         PrintDate();         //Find First Stock MOVING AVERAGE Being Greater         mov_First_Greater_C = first_Mov_Greater(mov_Stock1, mov_Stock2);         //Find Second Stock MOVING AVERAGE Being Greater         mov_Second_Greater_C = second_Mov_Greater(mov_Stock1, mov_Stock2);         //Finding MOVING AVERAGE To Be The Same         mov_Same_C = mov_Same(mov_Stock1, mov_Stock2);         //Printing Report 4         print_Report4(mov_First_Greater_C, mov_Second_Greater_C, mov_Same_C);         //Pause Screen         PauseScreen();                                                         //END OF REPORT 4 CALLS _getch(); return 0; }//END OF MAIN int first_Greater (float stock1[], float stock2[]) {         int i = 0;         int f_Greater_Count = 0;         for (i = 0; i < MAX_STOCKS; i++)         {                 if (stock1[i] > stock2[i])                         {                                        f_Greater_Count = f_Greater_Count + 1;                         }         }         return f_Greater_Count; }//END OF COUNTING THE FIRST STOCK BEING GREATER int second_Greater (float stock1[], float stock2[]) {         int i = 0;         int s_Greater_Count = 0;         for (i = 0; i < MAX_STOCKS; i++)         {                 if (stock1[i] < stock2[i])                         {                                        s_Greater_Count = s_Greater_Count + 1;                         }         }         return s_Greater_Count; }//END OF COUNTING THE SECOND STOCK BEING GREATER int same_Price (float stock1[], float stock2[]) {         int i = 0;         int same_Count = 0;         for (i = 0; i < MAX_STOCKS; i++)         {                 if (stock1[i] == stock2[i])                         {                                        same_Count = same_Count + 1;                         }         }         return same_Count; }//END OF FIND THE STOCKS BEING THE SAME void print_Report1(int first_Greater, int second_Greater, int same_Price) {         printf("REPORT ONE\n\n");         printf("The amount of days stock one is greater is: &#37;d\n\n", first_Greater);         printf("The amount of days the stock two is greater is: %d\n\n", second_Greater);         printf("The amount of days the stocks are the same is: %d\n\n", same_Price); }//END OF PRINTING REPORT 1 double average_First (float stock1[]) {         double average = 0;         double sum = 0;                 for (int i = 0; i < MAX_STOCKS; i++)                 {                                sum = (stock1[i] + sum);                 }                 average = (sum/(double)MAX_STOCKS);         return average; }//END OF FINDING AVERAGE double average_Second (float stock2[]) {         double average = 0;         double sum = 0;                 for (int i = 0; i < MAX_STOCKS; i++)                 {                                sum = (stock2[i] + sum);                 }                 average = (sum/(double)MAX_STOCKS);         return average; }//END OF FINDING AVERAGE int exceeded_Average_First (float stock1[], double average_First) {                 int i = 0;         int first_Greater_C = 0;                 for (i = 0; i < MAX_STOCKS; i++)         {                 if (stock1[i] > average_First)                         {                                 first_Greater_C = (first_Greater_C + 1);                                 //printf("Stock[%d]: %f Count: %d\n\n", i, stock1[i], first_Greater_C);                         }         }         return first_Greater_C; }// END OF EXCEEDED FIRST AVERAGE int exceeded_Average_Second (float stock2[], double average_Second) {                 int i = 0;         int second_Greater_C = 0;                 for (i = 0; i < MAX_STOCKS; i++)         {                 if (stock2[i] > average_Second)                         {                                 second_Greater_C = (second_Greater_C + 1);                         }         }         return second_Greater_C; }// END OF EXCEEDED FIRST AVERAGE void print_Report2 (double average_First, double average_Second, int exceeded_Average_First, int exceeded_Average_Second) {         printf("REPORT 2\n\n");         printf("The average of the first stock is %.2lf\n\n", average_First);         printf("The average of the second stock is %.2lf\n\n", average_Second);         printf("The first stock exceeded it's average %d times\n\n", exceeded_Average_First);         printf("The second stock exceeded it's average %d times\n\n", exceeded_Average_Second); }// END OF PRINTING REPORT 2 void calc_Mov_Average (float stock[], float movAvg[]) {         int i = 0;         int mov_Index = 0;         int prime_Count = 0;         float running_Sum = 0;         for (i = 0; i < MAX_STOCKS; i++)         {                 //Prime running sum with first 5 elements                 if (prime_Count < 5)                 {                         running_Sum = (stock[i] + running_Sum);                         //On fifth element calc first moving average                         if (prime_Count = 4)                         {                                         movAvg[mov_Index] = (running_Sum/5);                                         mov_Index++;                         }                         prime_Count = (prime_Count + 1);                 }                 else                 {                         //Calc running sum on remaining elements                         //        By adding the next elements in the sum and subtracting the element before the sum                         running_Sum = ((running_Sum + stock[i]) - stock[i-5]);                         movAvg[mov_Index] = (running_Sum/5);                         mov_Index++;                 }                 }//END OF FOR }//END OF CALC MOVING AVERAGE void print_Report3 (float mov_Stock1[], float mov_Stock2[]) {         int i = 0;         int start = 1;         int stop = 5;         for (i = 0; i < MOV_MAX; i++)         {                                 printf("REPORT 3\n\n");                 printf("Day %d to %d for stock 1: %f\n\n", start, stop, mov_Stock1[i]);                 printf("Day %d to %d for stock 2: %f\n\n", start, stop, mov_Stock2[i]);                                 start++;                 stop++;         }//END OF FOR                 }//END OF PRINTING REPORT 3 int first_Mov_Greater (float mov_Stock1[], float mov_Stock2[]) {         int i = 0;         int movf_Greater_Count = 0;         for (i = 0; i < MOV_MAX; i++)         {                 if (mov_Stock1[i] > mov_Stock2[i])                         {                                        movf_Greater_Count = movf_Greater_Count + 1;                         }         }         return movf_Greater_Count; }//END OF COUNTING THE FIRST STOCK BEING GREATER int second_Mov_Greater (float mov_Stock1[], float mov_Stock2[]) {         int i = 0;         int movs_Greater_Count = 0;         for (i = 0; i < MOV_MAX; i++)         {                 if (mov_Stock1[i] < mov_Stock2[i])                         {                                        movs_Greater_Count = movs_Greater_Count + 1;                         }         }         return movs_Greater_Count; }//END OF COUNTING THE SECOND STOCK BEING GREATER int mov_Same (float mov_Stock1[], float mov_Stock2[]) {         int i = 0;         int mov_Same_Count = 0;         for (i = 0; i < MOV_MAX; i++)         {                 if (mov_Stock1[i] == mov_Stock2[i])                         {                                        mov_Same_Count = mov_Same_Count + 1;                         }         }         return mov_Same_Count; }//END OF FIND THE STOCKS BEING THE SAME void print_Report4(int first_Mov_Greater, int second_Mov_Greater, int mov_Same) {         printf("REPORT FOUR\n\n");         printf("The amount of days the moving average was greater for the first stock is: %d\n\n", first_Mov_Greater);         printf("The amount of days the moving average was greater for the second stock is: %d\n\n", second_Mov_Greater);         printf("The amount of days the moving average is the same for both stocks is: %d\n\n", mov_Same); }//END OF PRINTING REPORT 4```
• 04-23-2007
MacGyver
Am I missing something or did you just post this over here?