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

  1. #16
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    The funny thing was I changed the movingAvg[n] to movingAvg[index] in the function call, but failed to change it in the movingAvg[index]=sum/days. I have to remember that if you change one, you have to change the other.

    I have another question. For the moving averages for Stock1, the average prices 2 through 6 is $35.50. For the moving averages for stock2, the average prices 11-15 is also $35.50, the only prices that are equal. However, the output,"The moving average of stock1 equals the moving average of stock2: 0 days" isn't correct. The code looks correct to me.

    I have one more question. Hopefully the final one
    Is there a way to "dress" up this output:

    Report 3 Apr 12 2004

    16 Five-day moving averages of Stock1 and Stock2

    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: $35.45
    The average for prices 2 through 6 is: $33.35
    The average for prices 3 through 7 is: $33.15
    The average for prices 4 through 8 is: $33.85
    The average for prices 5 through 9 is: $34.10
    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
    ==============================================
    I have been trying to get the decimals to line up so the output looks cleaner. Like maybe shift the first 5 lines to the right 2 spaces, then shift the next 4 lines over to the right 1 spaces and leave the remaining lines there. That way, the decimals are all lined up. Not sure if this was possible without a lot of code. I am not sure if the other results are correct because if the last one isnt correct, than I'm not very sure the others are. I appreciate your help again. You definitely know your C programming. Have a great day!! Tommy

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Tommy, printf() gives you a lot of control over the output display, you can left align, right align, and specify widths for each field. For example, try this:
    Code:
           printf("The average for prices %2d through %2d is: $%.2f\n",  index+1,index+days,movingAvg[n]);

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >However, the output,"The moving average of stock1 equals the moving average of stock2: 0 days" isn't correct.

    There are two problems here. The first is you are only comparing moving averages at the same index, not the entire array. To compare every location of one array with every location in the other array, you need a double nested for-loop.
    Code:
    int movAvgSame (float movAvg1[],float movAvg2[],int size)
    {
        int days = 0;
        int i, j;
    
       for (i=0;i<size;i++)
       {
          for (j=0;j<size;j++)
          {
             if (movAvg1[i] == movAvg2[j])
             {
                days++;
             }
           }
       }
       return days;
    }
    Now this may work, but there is one other issue:
    if (movAvg1[i] == movAvg2[j])
    Technically, you are not supposed to compare floats for exact equality, because they are inprecise numbers. The normal way to compare floats for equality is to check to see that they are equal to within a certain small amount:
    Code:
        float epsilon = .000001;
    .
    .
             if (fabs(movAvg1[i] - movAvg2[j]) < epsilon)

  4. #19
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    I don't know how you know all of this, but you're right on the money and it did work. Thanks again. I didnt have to check for equality. So why doesnt the nested For loop work for finding these two values:

    The number of days in which the moving average of stock1 exceeded the moving average of stock2: 10 days

    The number of days in which the moving average of stock2 exceeded the moving average of stock1: 6 days

    If it works in finding equality, then I thought it would work in finding less than and greater than
    What's happening here is that I am only comparing at the same index again, or is this what I want to do?

    Wouldn't you need to compare every location of movAvg1 array with every location of movAvg2 to find out which moving averages exceeded the other moving averages? I tried it and got a value of 125 days for number of days exceeded moving average of stock2 and 130 days for number of days exceeding moving average of stock1. This wasn't what I expected!

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well, I'm not an expert on moving averages, but I would think you'd only want to compare moving averages at the same index, so the nested for-loop should be removed. If plotted each moving average represents a point in time. The moving average at day 5 would be the average of the stock's price from days 1-5.

    In fact, you might not even want to compare moving averages from two stocks. Instead, compare the stock's price with its moving average to determine crossovers. For example when a stock's price changes direction, you want to know when it crosses its moving average as a signal to maybe buy or sell.

    So you'd compare the stock's price at day 5 with moving average at day 5 (the first one calculated). If the price is greater than the moving average, then perhaps the stock is in an uptrend, so you wait to see when the price falls below the moving average, which might signal a downtrend is in progress.

    I haven't studied it, but I do know you generally compare the moving average from the previous x days with the current price to determine trends. Some commonly used moving averages are 30-day, 50-day, and 200 day.

  6. #21
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    So this function is ok then.

    Code:
    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;
    }
    I sure know that this program was a little difficult. I am not sure if all of the output is correct or not. I know that most of the output is correct and I did learn a lot from this program. Thanks again for your help. I appreciate it. I added to your reputation as much as I was allowed to. It's telling me to spread it aorund, but your the only one that's helped me

Popular pages Recent additions subscribe to a feed