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 program FtoC real f,c write(*,*) "What is the temp in F? " read(*,*) f c = (f-32)*5/9 write(*,*) "The temp in Celsius is ", c end
Lines 1-3: 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: First line of code. 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!
Line 5: Next, any variables that need to be used in the program must be declared. The line real f,c declares 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 6: Next, some text is displayed to the user of the program. The line write(*,*) "What is the temp in F? " displays just what you would expect to the user of the program.
Line 7: Next, our program waits for some input from the user and stores this value in the variable c.
Line 8: Next, we calculate our result and save in the variable c.
Line 9: Next, we display the results of our calculation to the user.
Line 10: The end of our first FORTRAN program.
- Try creating the same source file in a UNIX editor.
i. Login to UNIX.
ii. Type vi tempconv.f
iii. Type i to enter the insert mode.
iv. Type the following in the document.
v. Type Esc key and then Shift-ZZ to save and exit the file.
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.