Examples
- Write (edit) a FORTRAN program source file using MATLAB's editor.
i. Start MATLAB.
ii. Open a blank document in the editor.
iii. Save the new document astempconv.f
.
iv. Type the following in the document and save the file.
C C Program to convert Fahrenheit temps to Celsius C C234567 5 program FtoC 6 real f,c 7 write(*,*) "What is the temp in F? " 8 read(*,*) f 9 c = (f-32)*5/9 10 write(*,*) "The temp in Celsius is ", c 11 end
Lines 1-4: The first three lines start with the character "C". This could be any letter. The "C" is commonly used to indicate this is a comment line.
Line 4: A comment line that shows where column 7 is. This line is optional, but often included.
Line 5: First line of program source code. FORTRAN command lines (code) must start in the 7th column of a non-comment line. The first program code line starts with the word
program
. FORTRAN is NOT case-sensitive. So, it could start with pRogrAm too. Please don't do this. You will lose friends!Note: The line numbers are optional and if you choose to use line numbers be sure to number with space in between so that if you need to add a new code lines you won't have to change all remaining line numbers. For example, use 50, 60, 70, etc. instead of 5,6,7 as we show for this example.
Line 6: Declares space for two variables f and c to be of type real. This just means that they will be floating point decimal values and not integers. If your variables will only contain integers, then you should declare them as type integer instead.
Line 7: Displays prompt for the temperature to the user of the program.
Line 8: Waits for some input from the user and stores this value in the variable f.
Line 9: Calculates a result and saves it in the variable c.
Line 10: Displays the results of our calculation to the user.
Line 11: Ends our first FORTRAN program.
- Try creating a source file using a UNIX editor.
i. Login to UNIX.
ii. Type vi average.f
iii. Type i to enter the INSERT mode.
iv. Type the following in the document with as few mistakes as possible.
C C Get three values from user and compute the average C C234567 50 program average3.f 60 real val,sum 70 write(*,*) "Enter the first value: " 80 read(*,*) val 90 sum = val 100 write(*,*) "Enter the second value: " 110 read(*,*) val 120 sum = sum + val 130 write(*,*) "Enter the third value: " 140 read(*,*) val 150 sum = sum + val 170 write(*,*) "The average is ", sum/3 180 end
v. Type Esc key and then Shift-ZZ to save and exit the file.
vi. If you make a mistake, there are many (hundreds of) ways to fix it. cw - change word, dw - delete word, x - delete character, r - replace character, R - replace characters until Esc is pressed, i - to enter insert mode again, are just a few of them. Programmers who likevi
like it because of its powerful (if cryptive) editing features. Programmers who don't likevi
don't know what they're missing.Many programmers find a favorite text-only editor and try to only use this editor. But, all programmers should strive to learn many editors and use the one that is most convenient for the type of programming they are doing. There are very sophisticated (expensive) text-only editors available to help programmers write code more easily. MATLAB's text editor will be fine for any programs we write in this course.
- Try creating a source file using
pico
.i. Login to UNIX.
ii. Type pico bmi.f
iii. Type the following in the document with as few mistakes as possible.
C C Program that converts height in feet and inches to meters C C234567 program bmi real heightM,weightKG,lbs,feet,inches write(*,*) "Enter the feet value of your height: " read(*,*) feet write(*,*) "Enter the inches value of your height: " read(*,*) inches write(*,*) "Enter your weight in lbs: " read(*,*) lbs heightM = (feet*12+inches)*0.0254 weightKG = lbs*0.45359237 write(*,*) "Your Body Mass Index is ", weightKG/heightM**2 end
iv. Try compiling and then running your code to see if you have entered it correctly for the FORTRAN compiler.
To edit:
>> pico bmi.f
To compile:
>> gfortran -o bmi bmi.f
To run:
>> bmi
Look online for lots of help with whichever editor you choose. O'Reilly publishers have lots of technical books to help programmers with a variety of programming tools (like editors).