Fortran

Guide To Learn

Core elements of Fortran

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

Reading stock data from files

Now that we have the list of stock symbols that we’ll work on, let’s use this information to load the data from file and store it in our newly declared dynamic arrays. The prototype of our main loop should look like the following listing. Listing 5.6 Reading stock data from a file using a read_stock subroutine ❶ For […]

Finding the best and worst performing stocks

Before we do any data analysis, we need to take care of the logistics. These steps will generally apply to each of the three challenges in this exercise: To start, we can estimate the performance of different stocks by calculating their gain over the whole period of the time series data–from January 2000 to May […]

Getting the data and code

The full code for the stock analysis exercise is available as a GitHub repo at https:// github.com/modern-fortran/stock-prices. If you use git, you can clone it directly from the command line: Otherwise, you can download it as a zip file from http://mng.bz/OMZo. The repository already includes the stock price data needed for this exercise in the data directory. However, if […]

Scroll to top