Fortran

Guide To Learn

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

Checking for allocation status

It will, at times, be useful to know the allocation status of a variable; that is, whether it’s currently allocated or not. To do this, we can use the built-in allocated function, as shown in the following listing. Listing 5.9 Checking for allocation status ❶ Will print F ❷ Will print T ❸ Will print F Trying to allocate an […]

Cleaning up after use

When we’re done working with the array, we can clean it from memory like this: After issuing deallocate, you must allocate array a again before using it on the right side of assignments. We’ll apply this mechanism to reuse arrays between different stocks. Automatic deallocation An allocatable array is automatically deallocated when it goes out of scope. For example, […]

Allocating an array from another array

It’s also possible to dynamically allocate an array based on the size of another array. The allocate statement accepts two optional arguments: For example, allocating a from b using mold will reserve the space in memory for a but won’t initialize its elements: ❶ Allocating from mold won’t initialize a. ❷ Initializes values separately However, if we allocate a from b using source, it will be allocated and initialized with values […]

Allocating arrays of a certain size or range

In the previous few sections, we’ve learned how to declare and initialize dynamic arrays. However, what if we need to assign values to individual array elements, one by one, in a loop? This will be the case as we load the data from CSV files into arrays–we’ll iterate over records in files, and assign values […]

Scroll to top