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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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.

Popular pages Recent additions subscribe to a feed