22.03.2013 Views

Self study guide: Fortran 95 - University of Cambridge

Self study guide: Fortran 95 - University of Cambridge

Self study guide: Fortran 95 - University of Cambridge

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1.9 The stop statement<br />

We have just seen how the exit command can be used to terminate a do loop. If<br />

you wish execution <strong>of</strong> your program to cease, you can insert a stop statement; this<br />

can incorporate some text, which is output when your program halts and identifies<br />

where this happened, e.g.<br />

1.10 Arrays<br />

stop ’this is where it all ends up’<br />

A great deal <strong>of</strong> scientific computation involves the manipulation <strong>of</strong> vectors, matrices<br />

and more general arrays <strong>of</strong> numbers. In F<strong>95</strong> we can have an array <strong>of</strong> variables set up<br />

in the declarations statements.<br />

How do we specify arrays? The simplest way is to give the dimension in parentheses.<br />

real :: a(3) ! a is an array <strong>of</strong> 3 values: a vector<br />

real :: m(3,3) ! m is a rank 2 array: a matrix<br />

We call the part in parentheses a shape. Each element <strong>of</strong> the array can be addressed<br />

in the program using a similar notation. Here is a simple example:<br />

program vector<br />

implicit none<br />

real :: v(3)<br />

real :: x<br />

integer :: i<br />

v(1) = 0.25<br />

v(2) = 1.2<br />

v(3) = 0.2<br />

! compute the modulus squared <strong>of</strong> the vector<br />

x = 0.0<br />

do i=1,3<br />

x = x + v(i)*v(i)<br />

end do<br />

write(*,*) ’Modulus squared = ’,x<br />

end program vector<br />

Notice how we use a loop to compute the sum over all elements <strong>of</strong> the vector.<br />

A second example will show us how we can implement simple vector and matrix<br />

operations:<br />

17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!