This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Fortran Class Notes Lecture 8 Working with Tabular Data Today, were going to look at the use of multi-dimensional arrays and how they can be used with loops and file operations to do some very useful things. Lets say that we have a data file of three rows and three columns. We want to display the same data but with the columns and rows reversed, like a diagonal mirror image. There are a couple of ways to do this operation, but well take a general approach that could be modified for many other possible rearrangements. 1 2 3 9 8 7 4 5 6 b 6 5 4 7 8 9 3 2 1 Since the data is laid out in two dimensions (across and down), we can use a two- dimensional array to store all nine numbers in one array variable which can be controlled entirely in loops. Though you can probably think of an easier way when working with just nine numbers, this approach is extremely useful when working with larger data files. Two-dimensional arrays are declared and referenced in the same way as one-dimensional arrays, but they hold much more data. Unlike MATLAB, Fortran doesnt have internal means of displaying the contents of an array. That means its up to you to keep up with what is being stored in the array, using write statements to check often while debugging. It also means that the first dimension can represent rows and the second be columns, or vice-versa. Fortran doesnt care as long as you are consistent throughout the program. Now lets look at the program and see how the operation is performed. program rearrange integer f(3,3),g(3,3) open (unit=10,file='arranged.txt',status='old') do i=1,3 read(10,*) (f(i,j),j=1,3) end do close (10) do i=1,3 do j=1,3 g(i,j)=f(4-i,4-j) end do end do do i=1,3 write(*,*) (g(i,j),j=1,3) end do write(*,*) ' ' write(*,*) 'Data has been rearranged!' write(*,*) 'Data has been rearranged!...
View Full Document
- Spring '11