Homework 12: Moving Average

Due: Tuesday 5/1 by 11:59pm


Objectives

Still more practice with arrays.


Background

When we work with volatile data — where numbers change drastically over time — it is often convenient to look at moving averages. For example, here are 12 days of closing stock prices of Apple, Inc. from October, 2011 (from October 7 to October 24) to be precise:

369.80, 388.81, 400.29, 402.19, 408.43, 422, 419.99, 422.24, 398.62, 395.31, 392.87, 405.77

As you can see it bounces around quite a bit. A 3-day moving average of these values take 3 consecutive prices and averages them. The first such average can be computed for the third day:

(369.80 + 388.81 + 400.29) / 3 = 386.3

The next two 3-day moving averages can be computed for the 4th and 5th days:

(388.81 + 400.29 + 402.19) / 3 = 397.0966667
(400.29 + 402.19 + 408.48) / 3 = 403.6366667

For all twelve days, we can compute ten 3-day moving averages (because we can't have a 3-day moving average for the first two days):

386.30, 397.09, 403.63, 410.87, 416.80, 421.41, 413.61, 405.39, 395.60, 397.98

The prices still bounce around, but not as much as the original prices. Depending on the application, a moving average is often more descriptive.


Assignment


Your assignment is to write a program that computes the 10-day moving average of Apple's closing stock prices. First, download moving.c. It has two arrays date[] and AAPL[] initialized with the 250 most recent dates and the closing stock prices of Apple stock. (AAPL is the ticker symbol for Apple on the NASDAQ stock exchange.) Since the stock market is not open every day, 250 data points correspond to roughly one year's worth of data. (Stock price data was taken from Yahoo! Finance.)

For example, date[2] holds "4/29/11" and AAPL[2] holds 350.13. This indicates the closing price of Apple was 350.13 on April 29, 2011.

Each element of the date[] array is a string. You can print out strings using the %s placeholder. Thus:

printf("AAPL price on %s is: %.2f.\n", date[249], AAPL[249]) ;

will print out


  AAPL price on 4/23/12 is: 571.70.

Your program should compute all 241 moving averages from the data. The first few lines of your output should look something like:


The 10-day moving average on 5/10/11 is 348.154000.
The 10-day moving average on 5/11/11 is 347.862000.
The 10-day moving average on 5/12/11 is 347.844000.
The 10-day moving average on 5/13/11 is 346.881000.
The 10-day moving average on 5/16/11 is 345.583000.
The 10-day moving average on 5/17/11 is 344.377000.

Notes

There are several ways to approach this problem. Think about how you would organize your for loop or for loops and what values you need to maintain from iteration to iteration. You will not be graded on the efficiency of your program, but some approaches are also easier to implement than others.

However, you will be graded on the extensibility of your approach. If we wanted to compute the 12-day moving average instead of the 10-day moving average, how easy would it be to change your program? (Hint: avoid magic numbers.) What if we wanted a 100-day moving average?

What to submit

Use the script command to record yourself compiling your program and running your program. Then submit your program and typescript file as usual:


submit cs104_chang hw12 moving.c typescript