Thread: I have a function and I need multiple averages returned

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    78

    I have a function and I need multiple averages returned

    I have a question about this program. I created the header file called util.h and I have completed most of the 3 out of 4 reports that need to be done. This program has 2 arrays, stock1 and stock2 and they each have 20 prices. I need to write functions (1 for stock1 and the other for stock2) to find the 16 5-day moving average of the price of the stocks. This means I would find the average price for prices 1-5, then the average price from 2-6, then 3-7, 4-8, 5-9, 6-10 and so on until I get to 16-20. I am not sure how to do this. I know how to find the moving average, but I don't know how to return the values, or if they need to be returned at all. Anybody have any ideas? Functions can only return 1 value. They can probably return more, but knowing this is beyond the scope of this course. I know that writing 16 functions is absurd. How about pointers, which I am not that good at writing yet? Or can we just have the printf statements within the function? Or am I wrong all together? LOL. Here's the program:

    Code:
    #include <stdio.h> 
    #include <conio.h> 
    #include <process.h> 
    #include "util.h" 
    
    
    int numDays1(float stock1[],float stock2[],int);  /*function prototype*/   
    
    int numDays2(float stock2[],float stock1[],int);  /*function prototype*/ 
    
    int numSame(float stock1[],float stock2[],int);  /*function prototype*/ 
    
    int numAvg1(float stock1[],int);    /*function prototype*/ 
    
    int numAvg2(float stock2[],int);    /*function prototype*/ 
    
    float movAvg1(float stock1[],int);  /*function prototype*/ 
                                        
    int main() 
    { 
        system("CLS");            /*Clears the screen*/ 
    
        printf("Report 1\n"); 
                
    
         float stock1[]={34.25,40.50,36.50,40.00, 
                         30.25,30.25,35.50,36.00, 
                         34.25,37.00,34.00,35.00,        /*Arrays are declared and initialized*/ 
                         36.25,34.25,40.50,41.50, 
                         41.50,40.00,36.50,34.50}; 
         
         float stock2[]={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}; 
    
         
    
         printf("\nStock1 has exceeded the price of stock2 by:  %d days\n",numDays1(stock1,stock2,20)); 
    
         printf("\nStock2 has exceeded the price of stock1 by:  %d days\n",numDays2(stock2,stock1,20)); 
    
         printf("\nThe number of days in which the price of the stocks were the same:  %d days\n\n",numSame(stock1,stock2,20)); 
    
         PauseScreen(); 
         
         system("CLS"); 
    
         printf("Report 2\n"); 
    
         printf("\nThe number of days in which stock1 exceeded it's price is:  %d days\n",  numAvg1(stock1,20)); 
    
         printf("\nThe number of days in which stock2 exceeded it's price is:  %d days\n",  numAvg2(stock2,20)); 
    
         
         
         PauseScreen(); 
         
         system("CLS"); 
    
         printf("Report 3\n"); 
    
         printf("\nThe average for prices 1 through 5 are:  %.2f\n",  movAvg1(stock1,20)); 
    
    
    
    
    
         getch(); 
    
         return 0; 
    } 
    int numDays1(float stock1[],float stock2[],int size) 
    { 
        int index; 
    
        int days; 
    
        days; 
        days=0; 
    
        for (index = 0;index < size;index++) 
         
        if (stock1[index] > stock2[index]) 
        { 
            days++; 
        } 
         
        return days; 
    } 
    int numDays2(float stock2[],float stock1[],int size) 
    { 
        int index; 
    
        int days; 
        days=0; 
    
        for (index = 0;index < size;index++) 
         
        if (stock2[index] > stock1[index]) 
        { 
            days++; 
        } 
         
        return days; 
    }    
    int numSame(float stock1[],float stock2[],int size) 
    { 
        int index; 
    
        int days; 
        days=0; 
    
        for (index = 0;index < size;index++) 
         
        if (stock1[index] == stock2[index]) 
        { 
            days++; 
        } 
         
        return days; 
    }    
    int numAvg1(float stock1[],int size) 
    { 
        float sum; 
        sum=0; 
    
        int index; 
    
        int days; 
        days=0; 
    
        int avg; 
        avg=0; 
    
        for (index = 0;index < size;index++) 
    
        { 
            sum=sum+stock1[index]; 
        } 
        avg=(int)sum/size; 
    
        for (index = 0;index < size;index++) 
             
        if (stock1[index]> avg) 
        { 
            days++; 
        } 
    
        return days; 
    } 
    int numAvg2(float stock2[],int size) 
    { 
        float sum; 
        sum=0; 
    
        int index; 
    
        int days; 
        days=0; 
    
        int avg; 
        avg=0; 
    
        for (index = 0;index < size;index++) 
    
        { 
            sum=sum+stock2[index]; 
        } 
        avg=(int)sum/size; 
    
        for (index = 0;index < size;index++) 
             
        if (stock2[index]> avg) 
        { 
            days++; 
        } 
    
        return days; 
    } 
    float movAvg1(float stock1[],int) 
    { 
         
        float sum; 
        sum=0; 
    
        int index; 
    
        int days; 
        days=0; 
    
        float avg; 
        avg=0.0; 
         
        for (index=0;index<5;index++) 
         {
            sum=sum+stock1[index]; 
            avg=sum/5; 
         }
         
    
        return avg; 
    }
    The float movAvg1 will be the function that will find the 16 5-day moving average. I plan on having another function called float movAvg2 to create the 16 5-day moving average for stock2. I appreciate any help.....Thanks....Tommy

    P.S.--If anyone has any suggestions on improving my code, I would like to know.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    For the function to calculate your 16 moving averages, you could pass an float array of size 16 to return the values:

    void movAvg(float stock1[],int,float movingAvg[])

    Then in your main your call would be:
    Code:
    float movingAvg[16];
    movAvg( stock1,20,movingAvg );
    > printf("\nThe number of days in which stock1 exceeded it's price is: %d days\n", numAvg1(stock1,20));
    > printf("\nThe number of days in which stock2 exceeded it's price is: %d days\n", numAvg2(stock2,20));

    You don't need two separate functions for numAvg, just make one function, and call it with each array:

    printf("\nThe number of days in which stock1 exceeded it's price is: %d days\n", numAvg(stock1,20));
    printf("\nThe number of days in which stock2 exceeded it's price is: %d days\n", numAvg(stock2,20));

    And then the function would be about the same as you have:
    Code:
    int numAvg(float stock[],int size) 
    { 
        float sum; 
        sum=0; 
    
        int index; 
    
        int days; 
        days=0; 
    
        int avg; 
        avg=0; 
    
        for (index = 0;index < size;index++) 
    
        { 
            sum=sum+stock[index]; 
        } 
        avg=(int)sum/size; 
    
        for (index = 0;index < size;index++) 
             
        if (stock[index]> avg) 
        { 
            days++; 
        } 
    
        return days; 
    }
    The same is true for your other functions.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    I fixed my other functions since it seemed as if I had a lot of extra code. However, I am not too sure of how to proceed with the 16 5-day moving averages. In my function, the arithmetic will produce each of the 16 5-day averages. In my main, do I just use a printf statement for each 5 day average and use the call statement? Not sure how to go about this. Thanks again.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >In my main, do I just use a printf statement for each 5 day average and use the call statement?
    Since you are returning multiple values (in the array), not just one value, first call the function, then use a loop to print each moving average.
    Code:
    float movingAvg[16];
    
    movAvg( stock1,20,movingAvg );
    
    for (int i=0; i<16; i++)
       printf("%f\n",movingAvg[i]);

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Ok, now where do I perform the addition and division? These have to be completed 16 times...

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Actually, if all you need to do is print the moving averages, you could just print out the results in the function. Then you wouldn't need the array to store them.

    >Ok, now where do I perform the addition and division? These have to be completed 16 times...
    Within the function make nested for-loops:
    Code:
    void movAvg(float stock[],int size) 
    {      
        int days = 5; 
        float sum; 
        int index, n; 
        float avg; 
    
        for (index=0; index<size-days+1; index++)
        {
           sum = 0.;
           for (n=index;n<index+days;n++) 
           {
              //Do your calculations
           }
            //Compute average
           printf("The average for prices %d through %d are:  %.2f\n",  index+1,index+days,avg); 
        }      
    }

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Thank you for your help with 16 five-day moving average. Now I have another problem. I wrote the code and I decided to include the whole program again as I made changes. I have a problem with figuring out the number of days on which the moving average of the first stock exceeded the moving average of the second. Then, I need to figure out the number of days on which the moving average of the second stock exceeded the moving average of the first stock. Then, determine the number of days on which the moving average of the stocks was the same. The code for the function avgDay, and movAvgSame are not correct and I don't know why.
    Code:
    #include <stdio.h> 
    #include <conio.h> 
    #include <process.h> 
    #include "util.h" 
    
    
    int numDays(float stock1[],float stock2[],int);  /*function prototype*/   
    
    int numSame(float stock1[],float stock2[],int);  /*function prototype*/ 
    
    int numAvg(float stock[],int);    /*function prototype*/ 
    
    void movAvg(float stock1[],int);  /*function prototype*/ 
    
    int avgDay(float stock1[],float stock2[],int); /*function prototype*/
    
    int movAvgSame(float stock1[],float stock2[],int); /*function prototype*/
                                        
    int main() 
    { 
        system("CLS");            /*Clears the screen*/ 
    
        printf("Report 1\n"); 
                
    
         float stock1[]={34.25,40.50,36.50,40.00, 
                         30.25,30.25,35.50,36.00, 
                         34.25,37.00,34.00,35.00,        /*Arrays are declared and initialized*/ 
                         36.25,34.25,40.50,41.50, 
                         41.50,40.00,36.50,34.50}; 
         
         float stock2[]={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}; 
    
         
    
         printf("\nStock1 has exceeded the price of stock2 by:  %d days\n",numDays(stock1,stock2,20)); 
    
         printf("\nStock2 has exceeded the price of stock1 by:  %d days\n",numDays(stock2,stock1,20)); 
    
         printf("\nThe number of days in which the price of the stocks were the same:  %d days\n\n",numSame(stock1,stock2,20)); 
    
         PauseScreen(); 
         
         system("CLS"); 
    
         printf("Report 2\n"); 
    
         printf("\nThe number of days in which stock1 exceeded it's price is:  %d days\n",  numAvg(stock1,20)); 
    
         printf("\nThe number of days in which stock2 exceeded it's price is:  %d days\n",  numAvg(stock2,20)); 
    
         
         
         PauseScreen(); 
         
         system("CLS"); 
    
         printf("Report 3\n"); 
    
         printf("\n\nstock 1\n\n");
    
         movAvg(stock1,20);
    
         printf("\n\nstock 2\n\n");
    
         movAvg(stock2,20);
    
         PauseScreen();
    
         system("CLS");
    
         printf("Report 4");
    
         printf("\nThe number of days in which the moving average of stock1 exceeds the moving average of stock2:  %d days\n\n", avgDay(stock1,stock2,20));
    
           printf("\nThe number of days in which the moving average of stock1 exceeds the moving average of stock2:  %d days\n\n", avgDay(stock2,stock1,20));
    
          
          printf("\nThe moving average of stock1 equals the moving average of stock2:  %d days\n\n", movAvgSame(stock1,stock2,20));
    
         PauseScreen();
    
         return 0;
       
    } 
    int numDays(float stock1[],float stock2[],int size) 
    { 
        int index; 
    
        int days; 
        days=0; 
    
        for (index = 0;index < size;index++) 
         
        if (stock1[index] > stock2[index]) 
        { 
            days++; 
        } 
         
        return days; 
    } 
    int numSame(float stock1[],float stock2[],int size) 
    { 
        int index; 
    
        int days; 
        days=0; 
    
        for (index = 0;index < size;index++) 
         
        if (stock1[index] == stock2[index]) 
        { 
            days++; 
        } 
         
        return days; 
    }    
    int numAvg(float stock[],int size) 
    { 
        float sum; 
        sum=0; 
    
        int index; 
    
        int days; 
        days=0; 
    
        int avg; 
        avg=0; 
    
        for (index = 0;index < size;index++) 
    
        { 
            sum=sum+stock[index]; 
        } 
        avg=(int)sum/size; 
    
        for (index = 0;index < size;index++) 
             
        if (stock[index]> avg) 
        { 
            days++; 
        } 
    
        return days; 
    } 
    void movAvg(float stock1[],int size) 
    { 
         
        int days=5;
        float sum;
        int index,n;
        float avg=0;
    
        for (index=0;index<size-days+1;index++)
       {
             sum=0;
             for (n=index;n<index+days;n++) 
             {
                   sum=sum+stock1[n];
                   avg=sum/days;
             }
             printf("The average for prices %d through %d is:  %.2f\n", index+1, index+days,avg);     
        } 
    }
    int avgDay(float stock1[],float stock2[],int size)
    {
        int days=5;
        float sum,sum1;
        int index,n;
        float avg,avg1;
    
        for (index=0;index<size-days+1;index++)
       {
             sum=0.;
             sum1=0.;
             for (n=index;n<index+days;n++) 
             {
                   sum=sum+stock1[n];
                   avg=sum/days;
    
                   sum1=sum1+stock2[n]
                   avg1=sum1/days;
             }
             if (avg > avg1)
             {
                      days++;
              }
          }
          return days;
    }
    int movAvgSame (float stock1[],float stock2[],int size)
    {
        int days=5;
        float sum,sum1;
        int index,n;
        float avg,avg1;
    
        for (index=0;index<size-days+1;index++)
       {
             sum=0.;
             sum1=0.;
             for (n=index;n<index+days;n++) 
             {
                   sum=sum+stock1[n];
                   avg=sum/days;
    
                   sum1=sum1+stock2[n]
                   avg1=sum1/days;
             }
             if (avg == avg1)
             {
                      days++;
              }
          }
          return days;
    }
    The last 2 functions are not correct and I have no idea why. I also get different results when I move the return days in either or both functions down 1 bracket. Not sure why this happens either. Thanks again for your help. Tommy

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    int avgDay(float stock1[],float stock2[],int size)
    {
        int days=5;
        float sum,sum1;
        int index,n;
        float avg,avg1;
    
        for (index=0;index<size-days+1;index++)
       {
             sum=0.;
             sum1=0.;
             for (n=index;n<index+days;n++) 
             {
                   sum=sum+stock1[n];
                   avg=sum/days;
    
                   sum1=sum1+stock2[n]
                   avg1=sum1/days;
             }
             if (avg > avg1)
             {
                      days++;
              }
          }
          return days;
    }
    Look at the code closely. You are using days for two different purposes. You are using it as 5 for the moving average duration. Then you're trying to use it as the number of days the average of one stock exceeded the other. You need a new name, for example days_exceeded, or something else you choose. The same is true for function movAvgSame().

    You could avoid having to do the moving average calculations twice by going back to the original idea of passing back an array from movAvg:

    void movAvg(float stock1[],int,float movingAvg[])

    Then in avgDay and movSame, you would simply pass the movingAvg arrays for stock1 and stock2, and compare the values in the two arrays.

    Also:
    > sum1=sum1+stock2[n]
    This is missing a semicolon in both functions. Probably just a typo.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I guess days should have been declared as a constant. Then the compiler would have let you know it was being modified:
    const int days=5;

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    so void movAvg(float stock1[],int,float movingAvg[]) is the function prototype. I am not sure as to how you would pass the array back. It sounds confusing. So if I just declare a variable, say int _exceed, and then use this name instead of days, the function should work. Now I see why I can't use days...thanks for pointing that out.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I am not sure as to how you would pass the array back.
    As you calculate the moving averages, you store them into the array at the appropriate index. At the end, you have 16 moving averages. In the code below, I used the name avg[] instead of movingAvg[].
    Code:
    void movAvg(float stock1[],int size,float avg[]) 
    { 
         
        int days=5;
        float sum;
        int index,n;
    
        for (index=0;index<size-days+1;index++)
       {
             sum=0;
             for (n=index;n<index+days;n++) 
             {
                   sum=sum+stock1[n];
             }
             avg[n] = sum/days;
             printf("The average for prices %d through %d is:  %.2f\n", index+1, index+days,avg[n]);     
        } 
    }
    >So if I just declare a variable, say int _exceed, and then use this name instead of days, the function should work
    That's right.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    OK, so when I calculate the 16 five-day averages, are the averages stored in an array called avg[]. Do we have to declare this array in main, float avg[16]; ?

    Can you explain this code:
    Code:
    void movAvg(float stock1[],int size,float avg[]) 
    { 
         
        int days=5;
        float sum;
        int index,n;
    
        for (index=0;index<size-days+1;index++)
       {
             sum=0;
             for (n=index;n<index+days;n++) 
             {
                   sum=sum+stock1[n];
             }
             avg[n] = sum/days;
             printf("The average for prices %d through %d is:  %.2f\n", index+1, index+days,avg[n]);     
        } 
    }
    Where does it compare the 16 five-day moving averages from stock1 and stock2? This code looks just like the void movAvg function. Sorry I am asking a lot of questions. I am just trying to make sense of it. Thanks again for your help.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by tommy69
    OK, so when I calculate the 16 five-day averages, are the averages stored in an array called avg[]. Do we have to declare this array in main, float avg[16]; ?
    Yes, you need two arrays, one to store the moving averages for stock1, and one for stock2. So for example:
    Code:
        float movingAvg1[16], movingAvg2[16];
    .
    .
         printf("Report 3\n"); 
    
         printf("\n\nstock 1\n\n");
    
         movAvg(stock1,20,movingAvg1);
    
         printf("\n\nstock 2\n\n");
    
         movAvg(stock2,20,movingAvg2);

    Where does it compare the 16 five-day moving averages from stock1 and stock2? This code looks just like the void movAvg function.
    You're right, it doesn't. You still need your avgDay, and movAvgSame functions. But now instead of recalculating the moving averages in these two functions, all you do is compare the two moving average arrays. Just one simple for-loop. Your callls in main would look like this:
    Code:
    printf("\nThe number of days in which the moving average of stock1 exceeds the moving average of stock2:  %d days\n\n", 
    avgDay(movingAvg1,movingAvg2,16));
    
    printf("\nThe number of days in which the moving average of stock1 exceeds the moving average of stock2:  %d days\n\n", 
    avgDay(movingAvg2,movingAvg1,16));
          
    printf("\nThe moving average of stock1 equals the moving average of stock2:  %d days\n\n", 
    movAvgSame(movingAvg1,movingAvg2,16));
    The function prototypes would be similar to this:
    Code:
    int avgDay(float movAvg1[],float movAvg2[],int size)
    int movAvgSame (float movAvg1[],float movAvg2[],int size)
    Last edited by swoopy; 04-11-2004 at 06:00 PM.

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Thank you Swoopy for your help. I really appreciate your time. I have a small problem and it's with the void movAvg function. It's returning the 16 five-day moving averages of both stocks, however, the first 5 numbers of stock2 are incorrect, and the rest are correct. Don't know why this is. The numbers in the array are correct. I think everything else is ok. Not sure if the numbers are correct for the other problems because of the numbers being incorrect for the first 5 of stock2. If we use this function:

    Code:
    void movAvg(float stock[],int size) 
    {      
        int days = 5; 
        float sum; 
        int index, n; 
        float avg; 
    
        for (index=0; index<size-days+1; index++)
        {
           sum = 0.;
           for (n=index;n<index+days;n++) 
           {
              //Do your calculations
           }
            //Compute average
           printf("The average for prices %d through %d are:  %.2f\n",  index+1,index+days,avg); 
        }      
    }
    The 16 five-day moving averages are correct for both stocks. The correct numbers are:
    Stock1: 36.30, 35.50, 34.50, 34.40, 33.25, 34.60, 35.35, 35.25, 35.30, 36.00, 37.50, 38.80, 39.55, 40.00, and 38.80.
    Stock2: 35.45, 33.35, 33.15, 33.85, 34.10,35.60, 36.50, 36.40, 35.65, 36.20, 35.50, 36.35, 36.45, 37.05, 37.20, and 37.80.
    If we use this function:

    Code:
    void movAvg(float stock1[],int size,float movingAvg[])
    {
    	
    	int const days = 5; 
        float sum; 
        int index,n;
        
    
        for (index=0; index<size-days+1; index++)
        {
           sum = 0.;
           for (n=index;n<index+days;n++) 
           {
               sum=sum+stock1[n]; 
           	}
           movingAvg[n]=sum/days;
           printf("The average for prices %d through %d is: %.2f\n",  index+1,index+days,movingAvg[n]); 
        } 
    
    }
    The first 5 numbers of stock2 are: 38.93, 37.38, 37.12, 36.81, and 35.76...rest are correct. Pretty strange I think. Here's the entire code:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <process.h>
    #include "util.h"
    
    
    int numDays(float stock1[],float stock2[],int);  /*function prototype*/   
    
    int numSame(float stock1[],float stock2[],int);  /*function prototype*/ 
    
    int numAvg(float stock1[],int);	/*function prototype*/
    
    void movAvg(float stock1[],int, float movingAvg[]);  /*function prototype*/
    
    int avgDay(float movAvg1[],float movAvg2[],int);  /*function prototype*/
    
    int movAvgSame(float movAvg1[],float movAvg2[],int);    /*function prototype*/
                                       
    int main()
    {
    	system("CLS");			/*Clears the screen*/
    
    	PrintTitle();	
    
    	printf("Report 1                                                      ");
    	PrintDate();
               
    
         float stock1[]={34.25,40.50,36.50,40.00,
    					 30.25,30.25,35.50,36.00,
    					 34.25,37.00,34.00,35.00,		/*Arrays are declared and initialized*/
    					 36.25,34.25,40.50,41.50,
    					 41.50,40.00,36.50,34.50};
    	 
         float stock2[]={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 movingAvg1[16];
    	 float movingAvg2[16];
    	 
    
             printf("\n\nStock1 has exceeded the price of stock2 by:  %d days\n", numDays(stock1,stock2,20));
    
    	 printf("\nStock2 has exceeded the price of stock1 by:  %d days\n", numDays(stock2,stock1,20));
    
    	 printf("\nThe number of days in which the price of the stocks were the same:  %d days\n\n",numSame(stock1,stock2,20));
    
    	 PauseScreen();
    	
    	 system("CLS");
    
    	 printf("Report 2					              ");			
    
    	 PrintDate();
    	 
    	 printf("\n\nThe number of days in which stock1 exceeded it's average price is:  %d days\n",  numAvg(stock1,20)); 
    
    	 printf("\nThe number of days in which stock2 exceeded it's average price is:  %d days\n\n",  numAvg(stock2,20));
    
    	 PauseScreen();
    	
    	 system("CLS");
    
    	 printf("Report 3                                                     ");
    
    	 PrintDate();
    	 printf("\n\n");
    
    	 printf("\t16 five-day moving average\n\n");
    
    	 printf("\t\tStock 1\n\n");
    
     	 movAvg(stock1,20,movingAvg1);
    
    	 printf("\n\n");
    
    	 printf("\t\tStock 2\n\n");
    
    	 movAvg(stock2,20,movingAvg2);
    
    	 PauseScreen();
    	
    	 system("CLS");
    	 
    	 printf("Report 4                                                     ");
    
    	 PrintDate();
    
    	 printf("\n\n");
    
    	 printf("\nThe number of days in which the moving average of stock1 exceeds the moving average of stock2: %d days\n\n", avgDay(movingAvg1,movingAvg2,16));
    
             printf("\nThe number of days in which the moving average of stock2 exceeds the moving average of stock1: %d days\n\n", avgDay(movingAvg2,movingAvg1,16));
    
    	 printf("\nThe moving average of stock1 equals the moving average of stock2: %d days\n\n", movAvgSame(movingAvg1,movingAvg2,16));
    		 
    	 PauseScreen();
    
    	 return 0;
    }
    int numDays(float stock1[],float stock2[],int size)
    {
    	int index;
    
    	int days;
    	days=0;
    
    	for (index = 0;index < size;index++)
    	
    	if (stock1[index] > stock2[index])
    	{
    		days++;
    	}
    	
    	return days;
    }
    int numSame(float stock1[],float stock2[],int size)
    {
    	int index;
    
    	int days;
    	days=0;
    
    	for (index = 0;index < size;index++)
    	
    	if (stock1[index] == stock2[index])
    	{
    		days++;
    	}
    	
    	return days;
    }   
    int numAvg(float stock1[],int size)
    {
    	float sum=0.;
    	
    	int index;
    
    	int days;
    	days=0;
    
    	int avg;
    	avg;
    
    	for (index = 0;index < size;index++)
    
    	{
    	
    		sum=sum+stock1[index];
    	}
    	avg=(int)sum/size;
    	
    	for (index = 0;index < size;index++)
    	
    	 
    		
    	if (stock1[index]> avg)
    	{
    		days++;
    	}
    	
    	return days;
    }
    void movAvg(float stock1[],int size,float movingAvg[])
    {
    	
    	int const days = 5; 
        float sum; 
        int index,n;
        
    
        for (index=0; index<size-days+1; index++)
        {
           sum = 0.;
           for (n=index;n<index+days;n++) 
           {
               sum=sum+stock1[n]; 
           	}
           movingAvg[n]=sum/days;
           printf("The average for prices %d through %d is: %.2f\n",  index+1,index+days,movingAvg[n]); 
        } 
    
    } 
    
    int avgDay(float movAvg1[],float movAvg2[],int size)
    {
    	int index;
    
    	int days;
    	days=0;
    
    	for (index = 0;index < size;index++)
    	
    	if (movAvg1[index] > movAvg2[index])
    	{
    		days++;
    	}
    	
    	return days;
    } 
    int movAvgSame(float movAvg1[],float movAvg2[],int size)
    {
    	int index;
    
    	int days;
    	days=0;
    
    	for (index = 0;index < size;index++)
    	
    	if (movAvg1[index] == movAvg2[index])
    	{
    		days++;
    	}
    	
    	return days;
    }
    I decided to include the output.

    ////////OUTPUT
    Scott's Trading Company-----------------------Report 1
    Apr 12 2004

    Stock1 has exceeded the price of stock2 by: 9 days

    Stock2 has exceeded the price of stock1 by: 11 days

    The number of days in which the price of the stocks were the same: 0 days

    Please press any key to continue
    ==============================================
    Report 2 Apr 12 2004

    The number of days in which stock1 exceeded it's average price is: 10 days

    The number of days in which stock2 exceeded it's average price is: 12 days

    Please press any key to continue
    ==============================================
    Report 3 Apr 12 2004

    16 five-day moving average

    Stock 1

    The average for prices 1 through 5 is: 36.30
    The average for prices 2 through 6 is: 35.50
    The average for prices 3 through 7 is: 34.50
    The average for prices 4 through 8 is: 34.40
    The average for prices 5 through 9 is: 33.25
    The average for prices 6 through 10 is: 34.60
    The average for prices 7 through 11 is: 35.35
    The average for prices 8 through 12 is: 35.25
    The average for prices 9 through 13 is: 35.30
    The average for prices 10 through 14 is: 35.30
    The average for prices 11 through 15 is: 36.00
    The average for prices 12 through 16 is: 37.50
    The average for prices 13 through 17 is: 38.80
    The average for prices 14 through 18 is: 39.55
    The average for prices 15 through 19 is: 40.00
    The average for prices 16 through 20 is: 38.80


    Stock 2

    The average for prices 1 through 5 is: 38.93
    The average for prices 2 through 6 is: 37.38
    The average for prices 3 through 7 is: 37.12
    The average for prices 4 through 8 is: 36.81
    The average for prices 5 through 9 is: 35.76
    The average for prices 6 through 10 is: 35.60
    The average for prices 7 through 11 is: 36.50
    The average for prices 8 through 12 is: 36.40
    The average for prices 9 through 13 is: 35.65
    The average for prices 10 through 14 is: 36.20
    The average for prices 11 through 15 is: 35.50
    The average for prices 12 through 16 is: 36.35
    The average for prices 13 through 17 is: 36.45
    The average for prices 14 through 18 is: 37.05
    The average for prices 15 through 19 is: 37.20
    The average for prices 16 through 20 is: 37.80
    Please press any key to continue
    ==============================================
    Report 4 Apr 12 2004


    The number of days in which the moving average of stock1 exceeds the moving aver
    age of stock2: 6 days


    The number of days in which the moving average of stock2 exceeds the moving aver
    age of stock1: 10 days


    The moving average of stock1 equals the moving average of stock2: 0 days

    Please press any key to continue
    ==============================================
    Thank again for the help.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    That was an interesting bug to track. I got the same results you did. Here's the bug:

    In function movAvg:

    > movingAvg[n]=sum/days;
    > printf("The average for prices %d through %d is: %.2f\n", index+1,index+days,movingAvg[n]);

    We should be using movingAvg[index] for both of these lines. So:
    movingAvg[index]=sum/days;
    printf("The average for prices %d through %d is: %.2f\n", index+1,index+days,movingAvg[index]);

    Glad to help, but sorry about that one.

Popular pages Recent additions subscribe to a feed