Fortran Interview Questions & Answers

Posted On:June 19, 2019, Posted By: Latest Interview Questions, Views: 1934, Rating :

Best Fortran Interview Questions and Answers

Dear Readers, Welcome to Fortran Interview Questions and Answers have been designed specially to get you acquainted with the nature of questions you may encounter during your Job interview for the subject of Fortran. These Fortran Questions are very important for campus placement test and job interviews. As per my experience good interviewers hardly plan to ask any particular questions during your Job interview and these model questions are asked in the online technical test and interview of many IT companies. 

1. How should one spell FORTRAN/Fortran? 

FORTRAN is generally the preferred spelling for discussions of versions of the language prior to the current one (‘90”). Fortran is the spelling chosen by X3J3 and WG5. In this document a feeble effort has been made to capitalize accordingly (e.g. vast existing software ... FORTRAN vs. generic Fortran to mean all versions of the standard, and specifically the modern dialect, ISO 

1539:1991). 

Interview Questions On Fortran

2. how does Fortran 90 relate to FORTRAN 77? 

With a few minor exceptions, Fortran 90 is a superset of X3.9-1978 FORTRAN. 

But this does not mean that all ‘77” codes will port sans changes. Many (if not most) programmers employed constructs beyond the ‘77 standard, or rely on unspecified behavior (say, assuming that an OPEN of an existing file will position the file pointer to just past the last record already written) which has changed (that is to say, has become specified). 

 

3. Why do you put so many lines of empty space in your programs? 

I hope the lines aren’t totally empty. They should contain a “c” in column one. These “blank” lines are just to make the comments stand out from Fortran code lines or to highlight key blocks of a program. 

 

4. how do you use a logical variable? What is stored there? 

Most frequently, logical variables are used in association with IF statements. When you want to 

set a logical variable LVAR to true you use “LVAR=.TRUE.”. For false use “LVAR=.FALSE.” 

In practice the computer usually stores an integer( ) in memory for false and integer 1 for true. 

The normal logical variable occupies 1 byte of space. 

 

5. where can i get a Fortran Compiler for IBM PC? 

We can pick up one on the internet from the GNU project, but get a better package from MOC for about S80.00. 

 

6. how do we know where various steps go in a Fortran program? 

Some commands have special locations, but most are located by the needs of the specific program. The PROGRAM card is always first Statements giving variable types (INTEGER, REAL, LOGICAL, CHARACTER) should precede “executable” statements. The END card must always be at the end of the program unit. 

 

7. Why does not Fortran have intrinsic functions for something as simple as factorial? 

Two reasons. Factorial isn’t all that common in heavy duty scientific and engineering applications. When it does occur, it almost always in a context where it is more computationally efficient to generate it as you go. You need 2? first then 3!, then 4!, etc. You are basically stuck doing a factorial within the context of a do loop unless you get really good and learn to write “recursive functions”, but then you arejust fooling yourself and writing another form of do loop. When you are taking the factorial of a large number and don’t need an exact answer you can resort to Stirling’s Approximation. A Fortran statement that will load the value of this approximation into the variable nfact is. 

 

8. What is the advantage of an array over a spreadsheet format? 

Both can store similar types of information m a neatly labeled and organized way. The advantage lies in where they are used. You have more control over how Fortran arrays are used than how the contents of a spreadsheet are used. In addition for any given operation on an array of numbers, once the Fortran is written, it will do the job much faster than a spreadsheet. On the other hand, when operations are not complex and computer execution time is not a problem using the spreadsheet is probably your best bet. 

 

9. Do spaces mater in Fortran equations? 

No. Spaces are generally added for clarity Some compilers get upset if you write things like” 

INTEGERI,J” rather than INTEGER I,J”. Simple neatness will keep you out of these problems. 

Remember that a space is required in column 6 if you aren’t continuing from the previous line. 

The following are all equivalent: 

x=x*y**2*sin(x) 

x=x * y2 * sin(x) 

x= x * y**2 * sin ( x) 

 

10. Why doesn't Fortran have intrinsic functions for something as simple as factorial? 

Two reasons. Factorial isn't all that common in heavy duty scientific and engineering applications. When it does occur, it almost always in a context where it is more computationally efficient to generate it as you go. You need 2! first then 3!, then 4!, etc. You are basically stuck doing a factorial within the context of a do loop unless you get really good and learn to write "recursive functions", but then you are just fooling yourself and writing another form of do loop. When you are taking the factorial of a large number and don't need an exact answer you can resort to Stirling's Approximation. A Fortran statement that will load the value of this approximation into the variable fact.

 

11. What is the advantage of an array over a spreadsheet format? 

Both can store similar types of information in a neatly labeled and organized way. The advantage lies in where they are used. You have more control over how Fortran arrays are used than how the contents of a spreadsheet are used. In addition for any given operation on an array of numbers, once the Fortran is written, it will do the job much faster than a spreadsheet. On the other hand, when operations are not complex and computer execution time is not a problem using the spreadsheet is probably your best bet.

 

12. How do you use a logical variable? What is stored there? 

Most frequently, logical variables are used in association with IF statements. When you want to set a logical variable LVAR to true you use "LVAR=.TRUE.". For false use "LVAR=.FALSE." In practice the computer usually stores an integer 0 in memory for false and integer 1 for true. The normal logical variable occupies 1 byte of space.

 

13. How do we know where various steps go in a Fortran program? 

Some commands have special locations, but most are located by the needs of the specific program. The PROGRAM card is always first. Statements giving variable types (INTEGER, REAL, LOGICAL, CHARACTER, ...) should precede "executable" statements. The END card must always be at the end of the program unit.

 

14. What is the difference between IF, THEN and DO WHILE statements. 

IF THEN combined with GO TO statements will let you do anything you want. The DO WHILE and other DO constructs allow you to loop through certain portions of code many times without ever writing GO TO statements. This makes coding slightly simpler, definitely clearer.

 

15. Where can I get a Fortran Compiler for an IBM PC? 

You can pick up one on the internet from the GNU project, but get a better package from MOC for about $80.00.

 

16. What is the difference between a Function Subprogram and a Subroutine? 

Some of the comments in the textbook are misleading on this subject. There are really only two differences. The biggest difference is that a subroutine never returns a value that is associated with its name. This means that you never need to declare a subroutine name in a type statement (REAL, INTEGER ...). All information coming back from a subroutine passes through the argument list, or something called a COMMON block (later). However, there is nothing in these communications channels that can't be used by a Function Subprogram. A secondary difference is that a Subroutine need not have an argument list. 

 

17. What is the difference between DO, DO WHILE, and IF ( ) GOTO Loops. 

In terms of what the computer actually does, there is generally no difference. You can structure all three so that they do the same thing. When properly indented, the DO structures tend to be easier to follow. The DO WHILE structure can produce slightly more compact coding, combining a straight DO with the option for some extra comparison logic. In a vector and/or parallel computer, DO's send strong hints to the compiler that it should be looking for ways to feed a pipeline or spread calculations over multiple processors.

 

18. What can I do if my lines wrap around to the next line? 

You have to get a feel for the location of the 72nd character position on the screen, or do a lot ofcounting. Once you hit column 72, you must hit the RETURN key, put some character (I like & or #) in column 6 of the next line then pick up typing where you left off. I usually stop before column 72 at some natural break point between variables:

123456789012345678901234567890123456789012345678901234567890123456789012

IF ( (X.LT.2.0.AND.Y.GT.30.0).OR.(X.GT.1000.0.AND.Y.LT.-40.))

& PRINT *,' CALCULATION IS IN TROUBLE'

Since Fortran tends to ignore blanks, the extra ones in the above 2 lines don't cause problems.

 

19. Can you give us a complete list of the Fortran commands and what they do? 

Sorry, but there are too many pages involved, and copyright problems with the standard Fortran manuals sold by computer software vendors. You're stuck with the text and Web pages, buying something else from a bookstore, or buying a Fortran package for your PC.

 

20. How can I format my output to look nicer (clear screen, double space, etc.) 

The best tools within Fortran are the in the FORMAT statement. "X" to add spaces in a line, and "/" to add blank lines on the output. Fortran doesn't know directly about your terminal type, so can't issue a specific screen clear command. You can brute force with "////////////////////////" (24 "/"'s) or call the Unix system as in the following example (note the extra call to get a listing of files after the screen is cleared). When clearing screens it is also useful to learn the Fortran "PAUSE" command to pause execution until you hit the RETURN (ENTER) key. Note that 'call system' is specific to the RS6000's. Other systems often have similar Unix connections.

program test:

call system('clear')

call system('ls -alF')

pause

call system('whoami')

end

 

21. What does "COMMON/CONTROL/ A, B, C" do to the values of A, B, C? 

It doesn't do anything to the values. It just establishes a place in memory for the values to be stored and retrieved. Every subroutine and function in your program that contains the line "COMMON/CONTROL/ A, B, C" will agree that operations using variables A, B, or C will get numbers from or put numbers in the same appropriate location in memory. That is, they agree that a reference to "A" means the same in all routines with common CONTROL, etc.. To give A, B, or C a value, you have two options. You can assign values at compilation time with a BLOCK DATA routine:

BLOCK DATA ABCVAL

COMMON/CONTROL/ A, B, C

DATA A,B,C / 1.0, 2.0, 3.0/

END

You can also assign and use values with executable statements. Say subroutines SUB1 and SUB2 contain this common block. If SUB1 contains the lines "A=1.0", and "B=2.0", and SUB2 has the line "C=A+B", then the value of C after this line in SUB2 is executed is 3.0.

 

22. What does "COMMON/CONTROL/ A, B, C" do to the values of A, B, C? 

It doesn't do anything to the values. It just establishes a place in memory for the values to be stored and retrieved. Every subroutine and function in your program that contains the line"COMMON/CONTROL/ A, B, C" will agree that operations using variables A, B, or C will get numbers from or put numbers in the same appropriate location in memory. That is, they agree that a reference to "A" means the same in all routines with common CONTROL, etc.. To give A, B, or C a value, you have two options. You can assign values at compilation time with a BLOCK DATA routine:

BLOCK DATA ABCVAL

COMMON/CONTROL/ A, B, C

DATA A,B,C / 1.0, 2.0, 3.0/

END

You can also assign and use values with executable statements. Say subroutines SUB1 and SUB2 contain this common block. If SUB1 contains the lines "A=1.0", and "B=2.0", and SUB2 has the line"C=A+B", then the value of C after this line in SUB2 is executed is 3.0.

 

23. Why doesn't Fortran have intrinsic functions for something as simple as factorial? 

Two reasons. Factorial isn't all that common in heavy duty scientific and engineering applications. When it does occur, it almost always in a context where it is more computationally efficient to generate it as you go. You need 2! first then 3!, then 4!, etc. You are basically stuck doing a factorial within the context of a do loop unless you get really good and learn to write "recursive functions", but then you are just fooling yourself and writing another form of do loop. When you are taking the factorial of a large number and don't need an exact answer you can resort to Stirling's Approximation. A Fortran statement that will load the value of this approximation into the variablenfact is:

nfact = sqrt(2*3.1415963*n)*(n/2.71828)**n

by 20! this has a 0.4% error. Try larger numbers on the machine for a better feeling of the approximation.

 

24. Do we need to prompt the user for input on our programs? 

Always! In this class, any programmed "read" from the terminal must be preceded by writing some intelligible message asking for input.

 

25. What directory is used by the compiler for compiling a Fortran Program? Where does f77 live? 

For the work in this class, you should assume that everything happens in whatever directory you are in when you type the "f77". Type "pwd" if you don't know the answer to this question. The executable file called "f77" resides both in /bin and /usr/bin on these machines. This is very unusual. To locate an executable file use the "whereis" command (e.g. "whereis f77"). Unfortunately the manual pages on f77 aren't connected properly and are listed under IBM's other name for their compiler, "xlf". Try "man xlf" for more information on the compiler, but don't expect too much. IBM likes to force people to buy manuals and special CD-ROM packages.

 

26. Why do you put so many lines of empty space in your programs? 

I hope the lines aren't totally empty. They should contain a "c" in column one. These "blank" lines are just to make the comments stand out from Fortran code lines or to highlight key blocks of a program.

 

27. Do spaces mater in equations? 

No. Spaces are generally added for clarity. Some compilers get upset if you write things like " INTEGERI,J" rather than INTEGER I,J". Simple neatness will keep you out of these problems. Remember that a space is required in column 6 if you aren't continuing from the previous line. The following are all equivalent:

x=x*y**2*sin(x)

x=x * y**2 * sin(x)

x = x * y ** 2 * sin ( x )

 

28. Is there any way to use variables in a format statement? 

Yes, but you have to use one format statement to build a second using a write to a character string. For example if you want to include the value of "n" as the number of real numbers per line you would do the following:

PARAMETER (N=4)

CHARACTER FORM1*16

REAL A(N,N)

DO 10 I=1,N

DO 10 J=1,N

A(I,J)=J*100+I

10 CONTINUE

WRITE(FORM1,2000)N

2000 FORMAT('(1X,',I3,'F6.1)')

WRITE(6,FORM1) A

STOP

END

Is it possible to lay out a two dimensional array with the double DO loops: 

DO 10 J=1,4

DO 20 K=1,4

A(J,K)=SUM(T(1:N)**(J+K-2))

20 CONTINUE

10 CONTINUE

yes, if you have a Fortran 90 compiler.

 

29. About CALL statements. Should they be within subroutines that they are calling for? 

Think of a CALL as a GO TO statement. If you say "CALL INPUT1", Fortran effectively looks for the statement called "SUBROUTINE INPUT1" and goes there to execute more instructions. Just before taking the jump, the program makes some notes so that the code in the SUBROUTINE knows about location of arguments, and knows that when it hits a RETURN statement, it should GO TO the statement in your code following "CALL INPUT1". You can see when thinking of this as a GO TO operation, there are too many opportunities for an infinite loop if SUBROUTINE INPUT1 contains a line that says "CALL INPUT1". There are also some subtle data management nightmares. To avoid all of this, older Fortran standards refused to let you include "CALL INPUT1" in SUBROUTINE INPUT1. This continues to be a good idea. However, for those who like to live on the edge Fortran 90 will let you include "CALL INPUT1" in SUBROUTINE INPUT1. This process is called recursion. I never use it, and don't recommend it. You will get another story from a certified Computer Scientist. Those folks really love recursion, and to be fair can do some fairly slick things with it.

 

30. Can you give us a complete list of the Fortran commands and what they do? 

No, but there are too many pages involved, and copyright problems with the standard Fortran manuals sold by computer software vendors. You're stuck with the text and Web pages, buying something else from a bookstore, or buying a Fortran package for your PC.

 

31. What does // do? 

It sticks 2 character strings together into a single string. If char1='file1.in' and char2='file2.out' then when char3=char1(1:5)//char2(6:9) the contents of char3 are 'file1.out'

 

32. What can I do if my lines wrap around to the next line? 

You have to get a feel for the location of the 72nd character position on the screen, or do a lot of counting. Once you hit column 72, you must hit the RETURN key, put some character (I like & or #) in column 6 of the next line then pick up typing where you left off. I usually stop before column 72 at some natural break point between variables:

123456789012345678901234567890123456789012345678901234567890123456789012

IF ( (X.LT.2.0.AND.Y.GT.30.0).OR.(X.GT.1000.0.AND.Y.LT.-40.))

& PRINT *,' CALCULATION IS IN TROUBLE'

Since Fortran tends to ignore blanks, the extra ones in the above 2 lines don't cause problems.

Explain Function Subprograms. 

What you have so far is just meant as an introduction. I'll be talking about them several more times during the semester. In the mean time, play with my example in newton3.f . It will get you safely through the homework. As an exercise try adding a second function at the end of the file called FUNCTION G(x), that sets G(x)=x4-2x2+1, and add lines in the main program to evaluate G(0), G(1), G(2) and print these values.

 

33. What does the function REAL(x) do?

Not much if x is already a real variable. If x is and integer, the output of REAL is a real (floating point number with the same value. REAL(3) is 3.00000E+00. If x is a complex number REAL returns the real part (as opposed to the imaginary part) of x.

 

34. Is there a way to go back to the top of the error list after a failed compilation? 

The Unix script command will save things for you in a file. If you are running a terminal emulatorwith a "scroll bar" on the side of the window. Try clicking in the scroll bar to see old stuff. Most of you probably won't have that option, so try one of the following:

f77 test.f >& testout

f77 -qsource test.f

The first dumps everything that would go to the screen (& picks up error messages which are treated as special cases by Unix) and puts the output into a file called "testout". It will complain and fail if "testout" already exists. The second will create a nice listing file called "test.lst" containing your source code and error messages.

 

35. If you use a BLOCK DATA statement to initialize a common block, do you lose the advantage of a smaller executable file? 

No, and Yes. You don't lose anything because the disk space advantage was already gone when you decided to use the named common blocks rather than blank common. BLOCK DATA will only assign values to contents of named common blocks. The disk space decision takes place when you choose to place variables in named common or blank common. Please remember that you can have lots of different named common blocks, but there is only one blank common block.

 

36. When is the common command not used correctly in a Fortran Statement?

You're asking for quite a bit here. There are lots of ways to introduce errors. The most obvious is to try inserting a common in the midst of executable statements. It belongs up with DIMENSIONs, PARAMETER's, SAVE's, and the rest of the non-executables. A more subtle problem is the use of multiple COMMON statements for blank common or the same named common. Fortran will accept the following program:

program testcom

common a,b

common c,d

a=1.0

b=2.0

c=3.0

d=4.0

call sub1

stop

end

subroutine sub1

common c,d

print *, c, d

return

end

However, the values printed out are 1.0 and 2.0. The second common statement in the main program just tacks the variables "c" and "d" onto blank common after "b". The two commons together are equivalent to "common a,b,c,d". In the subroutine you are saying that "c" is the first element in blank common, so Fortran associates "c" in "sub1" with the same address in memory as assigned to "a" in the main program. Also recall that mixing reals and integers (common/blk1/a,b,i,c) when the reals are double precision may cause errors on some machines, and is not a good idea on any 32 bit machine.

 

37. If you're eventually going to use X(1:N) with a chosen N what's the point in using X(*)? 

Given what you know now, no point at all. If N is available to me in the argument list, I would generally use "REAL X(N)" rather than "REAL X(*)". Two exceptions to this. If I pass N as an argument to give the range of a DO loop associated with X, but also make a special reference to X(N+1) later in the subroutine, I would use "X(*)" to note that the array is really longer than N. Some smart compilers will force me to do this. The second exception is related to Fortran 77. As mentioned below, I can pass N to my subroutine through a COMMON block and not include it in the argument list. If that is how I do it, pure Fortran 77 won't accept "REAL X(N)", and I'm stuck with "REAL X(*)". Fortran 90 and most late model improved Fortran 77 compilers will permit "REAL X(N)" when N is in a COMMON block.

 

38. What's the use of a blank common as opposed to a common with a name? 

The major use is in cutting the size of the executable file produced by "f77" (usually a.out). It won't seem like a big deal to you now, but in applications containing many arrays with thousands to millions of elements each, this can make a huge difference in the amount of disk space you soak up. A secondary use is that, when the program starts, the space finally allocated to blank common in memory is at the very end of the program. If you are tricky enough, you can take advantage of this to dynamically extend the size of a single array in blank common as your space requirements grow during execution.

 

39. If we write a subroutine for someone else, how can we compile it to see if it will run before giving it to them? 

This question gets to the heart of good programming practices. I'll give you an introduction here, and emphasize some of these points in later lectures. The easy part of the answer was hidden in the demonstration where I compiled 2 parts of a program separately and made a program on the 2nd partial compile. If you make a subroutine in a file called "subr1.f", you can compile to just test for Fortran errors with the command "f77 -c subr1.f". The "-c" option says just go through the compilation steps but don't make an executable code for me. This will leave a file behind called "subr1.o", which is almost machine instructions, and is ready to be linked to other stuff to make a full program. You're really not done yet. You should write a very short driver main program, that feeds some numbers to subr1 (call subr1(...)) for which you know the returned answer. You thencheck the known answers against the subroutine's answers before declaring victory. This testing is often easier said than done. Most useful subroutines do some fairly complicated calculations and contain IF tests that can result in different options being used depending on the input conditions. Designing test cases and checking methods can be challenging, but is essential. When approached systematically, this testing process is science at its best. You are in the business of constructing and interpreting controlled experiments. When using computers, most people simply treat them as an extension to theoretical science, another way to solve some equations. By recognizing the organized experimental aspects of creating and using computer programs you can give yourself a competitive advantage in this business.

 

40. What is the advantage of an array over a spreadsheet format? 

Both can store similar types of information in a neatly labeled and organized way. The advantage lies in where they are used. You have more control over how Fortran arrays are used than how the contents of a spreadsheet are used. In addition for any given operation on an array of numbers, once the Fortran is written, it will do the job much faster than a spreadsheet. On the other hand, when operations are not complex and computer execution time is not a problem using the spreadsheet is probably your best bet.

 

41. How do we know where various steps go in a Fortran program? 

Some commands have special locations, but most are located by the needs of the specific program. The PROGRAM card is always first. Statements giving variable types (INTEGER, REAL, LOGICAL, CHARACTER, ...) should precede "executable" statements. The END card must always be at the end of the program unit.

 

42. When accessing a data file in a program can I change directories? 

Yes if you have a subdirectory called "test" under the location that your program, you can open the file "my.data" in "test" for reading on unit 11 with the command: OPEN(11,file='test/my.data')

 

43. Subprograms. What do they do and how do the help program. 

They do anything that the main program can do. They help you organize your total program (main program + subprograms) by grouping specific tasks in a well defined location. They can save you repeating similar program structures at several places in your code. Also, for many tasks someone has already written a subprogram to do the job and you save a lot of work by picking up their subprogram and plugging it into your work. You will see an example of this when we start solving systems of linear equations (say 10 equations and 10 unknowns). You will define the equations, then let somebody else's subprogram solve them.

 

44. How do we know where various steps go in a Fortran program?

Some commands have special locations, but most are located by the needs of the specific program. The PROGRAM card is always first. Statements giving variable types (INTEGER, REAL, LOGICAL, CHARACTER, ...) should precede "executable" statements. The END card must always be at the end of the program unit.

 

45. Could you give us sample questions on determining output of code containing recent commands? 

DIMENSION A(5),B(5),C(5)

DATA A/1.,2.,3.,4.,5./,B/5*2/,C/5*0.0/,IMIN,IMAX,ISTEP/2,5,2/

DO 100 I=IMIN,IMAX,ISTEP

100 C(I)=A(I)*B(I)

WRITE(6,2000) I,(C(J),J=1,5)

2000 FORMAT(' I=',I5,' C=',/, (1P,4E15.7))

STOP

END

What is the value of "I" printed out. What are the values of "C" printed? Also check my sample "essay" question on the back of this handout.

 

46. When accessing a data file in a program can I change directories?

Yes if you have a subdirectory called "test" under the location that your program, you can open the file "my.data" in "test" for reading on unit 11 with the command: OPEN(11,file='test/my.data')

 

47. What a DATA statement does. 

It simply puts a specific value into a Fortran variable. "DATA A/1.0/" is the same as "A=1.0" in terms of results. The only difference is that the DATA statement sets A to 1.0 before the program starts to execute. The biggest advantage occurs when you are trying to give a variable a value in a FUNCTION or SUBROUTINE that is used a large number of times. The program on the left is faster than the program on the right below.

Do 100 i=1,1000000 do 100 i=1,1000000

100 y(i)=addon(x(i)) 100 y(i)=addon(x(i))

stop stop

end end 

function addon(x) function addon(x)

data b/1.12345/ b=1.12345

addon=x+b addon=x+b

return return 

end end

Yes, its a stupid example, but applies to real programs.

 

48. Can more than one variable be stored in a Data Statement? 

Yes. For example you can set initial values for A, B, and C with either of the following statements.

DATA A/1.0/,B/2.0/,C/3.0/

DATA A,B,C/1.0,2.0,3.0/

About the exact use of arrays and their purpose 

The simple answer is "pay attention to the lectures and examples for the rest of the semester." Arrays are used when you have a large number of numbers on which you want to do identical or similar analysis. Let's say I have the temperature and pressure from 10,000 measuring stations in the US, and want to calculate the air density at each of these points. I load the temperatures and pressures into arrays with a dimension of 10000, and pair by pair I march through and evaluate a function to give density in another array.

dens(i)=rho(t(i),p(i))

Yes, I could have read the data in a pair at a time and printed results one line at a time without using an array. However, what if this is just the beginning? What if I'm going to take all off my temperatures, pressures, densities and several other measurements from each station and use it as part of a complicated calculation to predict the weather for the next 24 hours? I have to keep all of the data in the computer. Arrays provide a convenient way of storing and retrieving all of this information within a program. Please note that while the program is executing, information contained in arrays, like other Fortran variables, is kept in the machine memory where access times are much faster than when the information is in a disk file.

 

49. Could you go over colons again in dealing with arrays? 

Within Fortran 77 there is only one use for colons associated with arrays, and that is when establishing the size and permitted range of indices in a DIMENSION or type (REAL, INTEGER, ...)statement. For example "DIMENSION A(-4:10)" assigns 15 words of memory to the array A, and makes a note that A(-4) corresponds to the first of these sequential words. For Fortran 90 using a colon within an executable statement is just shorthand for writing a DO loop involving just one line.

C(1:9)=A(1:9)+B(1:9) is the same as: DO 100 I=1,9

100 C(I)=A(I)+B(I)

C(1:9:2)=A(1:9:2)+B(1:9:2) is the same as DO 100 I=1,9,2

100` C(I)=A(I)+B(I)

Write a few 5-10 line programs until you are comfortable with how this behaves.

 

50. Are We Actually Going to Need Character Variables? Why are they used? 

You could probably get through all of the programming applications in this class without resorting to character variables. However, if you are going to create programs with legible output and/or flexible input/output you will need them. You have already seen me use them to store names of files for use in input (look at the sample program trig3.f). That is one of the most common uses for simple programs. Also, take a look at the tricks I play in plot1.f and plot2.f to generate creative output with the help of character variables. When you start driving graphics subroutines to view your data, you will find that character variables a useful and generally mandatory.

Postscript: My daughter (Senior, physics) burst out laughing when she saw this question while trying to throw me off of the family PC. Yes, she is cruel, but her experience may be closer to what you will use soon than the crazy things that I do. She is doing quite a bit of Fortran programming for Dr. Garrison in the Chem department. She says she couldn't get along without charactervariables to keep track of file names and manage printed and graphical output.

 

51. What directory is used by the compiler for compiling a Fortran Program? Where does f77 live? 

For the work in this class, you should assume that everything happens in whatever directory you are in when you type the "f77". Type "pwd" if you don't know the answer to this question. The executable file called "f77" resides both in /bin and /usr/bin on these machines. This is very unsual. To locate an executable file use the "whereis" command (e.g. "whereis f77"). Unfortunately the manual pages on f77 aren't connected properly and are listed under IBM's other name for their compiler, "xlf". Try "man xlf" for more information on the compiler, but don't expect too much. IBM likes to force people to buy manuals and special CD-ROM packages.

 

52. How do you use a logical variable? What is stored there? 

Most frequently, logical variables are used in association with IF statements. When you want to set a logical variable LVAR to true you use "LVAR=.TRUE.". For false use "LVAR=.FALSE." In practice the computer usually stores an integer 0 in memory for false and integer 1 for true. The normal logicalvariable occupies 1 byte of space.

 

53. Do spaces mater in equations? 

No. Spaces are generally added for clarity. Some compilers get upset if you write things like " INTEGERI,J" rather than INTEGER I,J". Simple neatness will keep you out of these problems. Remember that a space is required in column 6 if you aren't continuing from the previous line. The following are all equivalent:

x=x*y**2*sin(x)

x=x * y**2 * sin(x)

x = x * y ** 2 * sin ( x )

 

54. Why are go to statements bad, and what can we do to avoid them? 

GO TO is bad only to the extent that it makes coding difficult to read. I think they are an important part of the language, but should be used sparingly. Use of DO loops and IF, THEN, ELSE constructs (along with good indentation habits) whenever practical will take care of most of the problem. In extreme cases where you have many nested IF statements, with many lines contained in each THEN/ELSE option set, even good indentation habits will not improve code clarity. Consider breaking code off into subroutines, or go back to GOTO constructs where you can at least trace labelnumbers with an editor.