The following program (SQUARES) uses the Fortran Console project type.
The SQUARES program reads formatted data from the file datafile.dat
and displays
the calculated results. With the source code shown below, it does not generate the
expected results:
PROGRAM SQUARES
INTEGER INARR(10), OUTARR(10), I, K
! Read the input array from the data file.
OPEN(UNIT=8, FILE='datafile.dat', STATUS='OLD')
READ(8,*,END=5) N, (INARR(I), I=1,N)
5 CLOSE (UNIT=8)
! Square all nonzero elements and store in OUTARR.
K = 0
DO I = 1, N
IF (INARR(I) .NE. 0) THEN
! Is the error in this DO loop?
OUTARR(K) = INARR(I)**2
ENDIF
END DO
! Print the squared output values. Then stop.
PRINT 20, N
20 FORMAT (' Total number of elements read is',I4)
PRINT 30, K
30 FORMAT (' Number of nonzero elements is',I4)
DO, I=1,K
PRINT 40, I, OUTARR(K)
40 FORMAT(' Element', I4, ' has value',I6)
END DO
END PROGRAM SQUARES
The formatted file datafile.dat
currently contains one record that includes the
following:
To view the values of this formatted data file in the Microsoft visual development environment, use the Open item in the File menu.
When executed without array bounds checking (set by the /check:nobounds option), the output appears as follows:
When the program was executed with array bounds checking on, the output appears as follows:
You can either build this program from the command line or within the visual development environment (see Preparing Your Program for Debugging). This example assumes a project workspace already exists.
To debug this program:
squares.f90
: double-click its file name in the FileView pane. The
screen appears as follows:
The following toolbars are shown:
To change the displayed toolbars, select Customize in the Tools menu and click the Toolbars tab. You can move toolbars by dragging the anchor (double vertical line on the left of the toolbar).
OPEN(UNIT=8, FILE='datafile.dat', STATUS='OLD')
The red circle in the left margin of the text editor/debugger window shows where a breakpoint is set.
In the Build menu, click the Start Debug, Go item:
The Debug menu appears on the visual development environment title bar in place of the Build menu. If not displayed previously, the Debug toolbar appears.
If needed, you can set another breakpoint, position the cursor at the line where you want to add or remove a breakpoint and do either of the following:
Step through the lines of source code. You can do this with the Debug menu item Step Over (as shown) or the Step Over button on the Debug toolbar:
The error seems related to the value of variable K!
! Square all nonzero elements and store in OUTARR.
K = 0
DO I = 1, N
IF (INARR(I) .NE. 0) THEN
K = K + 1 ! add this line
OUTARR(K) = INARR(I)**2
ENDIF
END DO
The output screen appears as follows:
In the Build menu, click the Start Debug, Go item.
You can view the values of the local variables by using the Locals tab, including the arrays (click the plus sign).
The Variables window displays a Context menu (after the word Context:
). The Context
menu can help you debug exceptions.
The Locals tab does not let you display module variables or other non-local variables. To display non-local variables, display the Watch window:
In the text editor window, select the variable name INARR (without its subscript syntax), drag it, and drop it into the Watch window:
When the program completes execution, the screen appears as follows:
If a Disassembly window (shows disassembled code with source-code symbols) unintentionally appears, click the Step Out button on the debugger toolbar (or select the Step Out item in the Debug menu) as needed to dismiss the Disassembly window.
If you have the Visual Fortran Professional or Enterprise Edition, you can use the Array Viewer to display and graph multidimensional array element values.
For more information: