Skip to content

Commit

Permalink
start adding some array functions
Browse files Browse the repository at this point in the history
  • Loading branch information
milancurcic committed May 23, 2018
1 parent 85237f5 commit 31a47ff
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

all: stock_prices

stock_prices:
stock_prices: src/stock_prices.f90
$(MAKE) --directory=src $@
cp src/$@ .

Expand Down
3 changes: 2 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# stock-prices Makefile

FC = gfortran
OBJS = mod_io.o
OBJS = mod_arrays.o mod_io.o

all: stock_prices

Expand All @@ -16,6 +16,7 @@ stock_prices: stock_prices.f90 $(OBJS)
$(FC) $< $(OBJS) -o $@

# modules
mod_arrays.o: mod_arrays.f90
mod_io.o: mod_io.f90

.PHONY:
Expand Down
25 changes: 25 additions & 0 deletions src/mod_arrays.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module mod_arrays

! Utility functions that operate on arrays.

implicit none

private
public :: mean, reverse

contains

pure real function mean(x)
! Returns a mean of x.
real, intent(in) :: x(:)
mean = sum(x) / size(x)
end function mean

pure function reverse(x)
! Reverses the order of elements of x.
real, intent(in) :: x(:)
real :: reverse(size(x))
reverse = x(size(x):1:-1)
end function reverse

end module mod_arrays
20 changes: 16 additions & 4 deletions src/stock_prices.f90
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
program stock_prices

use mod_arrays, only: mean, reverse
use mod_io, only: read_stock

implicit none

character(len=4), allocatable :: symbols(:)
character(len=10), allocatable :: time(:)
real, allocatable :: open(:), high(:), low(:), close(:), adjclose(:), volume(:)
integer :: i
integer :: i, im, n

symbols = ['AAPL', 'AMZN', 'CRAY', 'CSCO', 'HPQ ',&
'IBM ', 'INTC', 'MSFT', 'NVDA', 'ORCL']

do i = 1, size(symbols)
call read_stock('data/' // trim(symbols(i)) // '.csv', time,&
write(*,*) 'Symbol, Open, Close, Average, Max. growth [%]'

do n = 1, size(symbols)

call read_stock('data/' // trim(symbols(n)) // '.csv', time,&
open, high, low, close, adjclose, volume)
write(*,*) symbols(i), maxval((close - open) / open * 100)

open = reverse(open)
close = reverse(close)

im = size(close)

write(*,*) symbols(n), open(1), close(im), mean(open),&
maxval((close - open) / open * 100)

end do

end program stock_prices

0 comments on commit 31a47ff

Please sign in to comment.