Fortran

Guide To Learn

Core elements of Fortran

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 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 […]

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 […]

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 […]

Scroll to top