Exercise 3: Calculating moving average and standard deviation
You can implement the moving average by iterating over each element of the input array, slicing that array over a subrange determined by the input window parameter, and applying the general average function to that slice, as shown in the following listing. Listing 5.19 Calculating moving average of a real array x over window w ❶ Result array with […]
Exercise 2: Reversing an array
The solution to this exercise has only two steps and you can write it as a single-line function (not counting the declaration code). First, we know that since we’re just reversing the order of elements, the resulting array will always be of the same size as the input array. The size will also correspond to […]
Exercise 1: Convenience (de)allocator subroutines
Start with the allocator subroutine alloc. For the key functionality to work, our subroutine needs to do the following: The following listing demonstrates the implementation. Listing 5.16 Allocating an array with error handling ❶ Array to allocate ❷ Array size ❸ Integer status code ❹ Character string to store the error message ❺ Free if already allocated ❻ Allocates with error handling […]
Answer key
This section contains solutions to exercises in this chapter. Skip ahead if you haven’t worked through the exercises yet.
Finding good times to buy and sell
Can we use historical stock market data to determine a good time to buy or sell shares of a stock? One of the commonly used indicators by traders is the moving average crossover. Consider that the simple moving average is a general indicator of whether the stock is going up or down. For example, a 30-day simple […]
Identifying risky stocks
One of the ways to estimate how risky a stock is at some time is by looking at volatility. Stock volatility can be quantified as the standard deviation of the stock price relative to its average. Standard deviation is a statistical measure that tells you how much individual array elements deviate from the average. To […]
Indexing and slicing arrays
Did you notice that the stock data in the CSV files are ordered from most recent to oldest? This means that when we read it into arrays from top to bottom, the first element will correspond to the most recent stock price. Let’s reverse the arrays so they’re oriented in a more natural way, going […]
Implementing the CSV reader subroutine
Having covered the detailed mechanics of allocating and deallocating arrays, including the built-in error handling, we finally arrive at implementing the CSV file reader subroutine, as shown in the following listing. Listing 5.10 Reading stock price data from CSV files and storing them into arrays ❶ Finds the number of records (lines) in a file ❷ Allocates […]
Catching allocation and deallocation errors
Your allocations and deallocations will occasionally fail. This can happen if you try to allocate more memory than available, allocate an object that’s already allocated, or free an object that has been freed. When it happens, the program will abort. However, the allocate statement also comes with built-in error handling if you want finer control over what happens […]