CIVE 1331 COMPUTING FOR ENGINEEERS LAB 12 -DOS Fundamentals and FORTRAN Programming Part 1: DOS Commands 1. Using Windows Explorer, connect the network drive R:\bird\fortran by Tools, Map Network Drive. 2. Start a MSDOS shell by selecting MSDOS Command Prompt from under programs. 3. Perform the following within the MSDOS shell: a) change directories to R:\bin b) list the files in the write down on a piece of paper how files there are and the total disk space these files occupy. c) change directories back to your directory ( look in Windows Explorer, it should be something such as h:\) d) Make a directory called lab12. e) Change to the directory lab12. f) Using the MS text editor, make a file called lab12a.txt In the file put Lab #12a, your name, your email address, the number of files and size that you found in R:\bin in step b above. Save the file and exit the text editor. g) Make a subdirectory (within the directory lab12) called temp1. h) Copy the file lab12.txt to the subdirectory temp1. i) Change directories to the subdirectory temp1. j) Edit the file lab12.txt. Play with the file, make changes and save the file. k) Exit the file. Delete the copy of the file lab12.txt in the directory temp1. l) Change directories back to lab12. m) Remove the subdirectory temp1. Deliverables: Briefly describe the DOS commands that you used in this laboratory. What does each command do? Include a copy of the file lab12.txt that you made. Part 2: FORTRAN Commands 1. Introduction. The objective of this lab is to familiar with the basic structure of Fortran programming by writing a simple program, compiling the program, and executing the program. 2. Instructions. Create a Fortran program which will prompt the user to input the vertical component (VCOMP) of a cable and the horizontal component (HCOMP). From this data, compute the cable length (LENGTH) and return this output to the screen. Be careful with the names of the variables and the intrinsic type of data associated with each. Check the results of your program by running the following example: VCOMP=225.3 HCOMP=130.4 3. Deliverables Write up a short report for this lab. The report should include a summary of the lab and a hard copy of the source code for your program. Include a hand calculation checking the accuracy of your program with the example. Submit your report to the TA at the next lab meeting. Extra Practice Once you get your program working, save the source code under a different name. Play with the variable names such as by not declaring it a REAL value. You may either use the SQRT(number) function or use ( number)**0.5. Change the and vertical components to integer type names and don’t declare them REAL If you instead use (number)**(1/2) does this affect the answer. Note that each time you change your source code you must recompile your program. FORTRAN II Program Structure: Last lecture the format of a line of Fortran source code was discussed. The layout of each line was discussed in terms of the position (column) on the line. The form of a program also has some very basic requirements: The first line of a program usually consists of a PROGRAM statement (Page 35 of Etter). The program statement assigns a name to the program and takes the following form: PROGRAM program name The program name can be one to six characters (alphabetic or numeric) and must begin with a letter. PROGRAM TEST PROGRAM LOAD1 It is usually good programming format to have some comment lines follow the program statement to describe what the program does. STOP Statement: The STOP statement is used to signal the computer to terminate execution of the program. In some instances, you may wish to exit the program if certain conditions exist. This is accomplished with a STOP statement. There are no arguments associated with the STOP statement, it is the only command on the particular line. The STOP statement is optional at the end of the program. If one is not placed in the program, the compiler will automatically place a STOP command in the executable to stop the program. END Statement: The END statement identifies the end of the FORTRAN PROGRAM. There are no arguments associated with the END statement. The basic form of the program is therefore as follows: PROGRAM TODD . . Specification Statements Executable Statements . . END Statement Specification Statements are not translated into machine language but instead assign memory locations or types of values which will be stored in these locations. Data Types We have discussed some of the different types of data which we may work with in our programs. We should already have a good understanding of the results of operations on real values, since the intermediate result of arithmetic operations on real values are real values. Understanding the action of integer statements is extremely important. One of the main operations which must be clearly understood is the process of "truncation"(page 16 of Etter). For example if we have integers: I=5 J=3 An operation such as: (I+J)/3 will yield another integer 2. Even though the actual real number calculation is 2.6666, the result will be "truncated". Truncation is not the same as rounding for which the above result would be round up to 3 since this is the closest whole number. In truncation, the decimal portion is dropped and the integer portion of the number is kept. Even if we used a "real" constant variable such as A = (I+J)/3, the intermediate result of operations involving only integers is another integer. The truncation will therefore take place during the calculation. The difference in using the variable A is that the value stored in A would be a real number 2.0 which may be valuable for future calculations depending on what we require in our program. Say I1=4, J1=7 what will be the result of A = (I1 + J1)/2? In many cases we may have a "Mixed-Mode Operation" which involves arithmetic operations on combinations between integers and real values. It is important in these case to consider when the truncation takes place: any operation between two real values will result in and intermediate result which is also a real number. The truncation will not take place until the values are attempted to be stored in a memory location which has been assigned an integer value: For example: MASS = 2.519*WEIGHT will first result in a real value from the multiplication and the truncation will take place when the value of 2.5 is stored in the integer memory location for MASS. If WEIGHT = 100 the result from the multiplication is 251.9 and the value that would be stored in MASS would be 251 ( the decimal portion will be truncated). Care needs to be taken in equations if you want a number to act as a real value then put in a decimal point. The example from Etter exhibits this: ROOT = NUM**(1/2) will always result in a value for ROOT of 1.0 regardless of the value of NUM. This is because the integer constants ˝ will always be truncated to 0. A more useful form for ROOT would be: ROOT = NUM**(0.5) which will result in a real value for ROOT ( the intermediate product of an integer and real value is a real value). Intermediate results on operations between integers and real values are real values. The final results depend on the variable names which are used to store the results. In some situations we may wish to use integer exponents if we can due to increased accuracy. Etter gives the example: AREA = SIDE**2 AREA = SIDE**2.0 which will essentially give identical results, however the actual operations will be different. With the integer exponent, AREA will be calculated by simply taking SIDE*SIDE, however the second case will usually be performed by taking the antilog of (2.0xlog(SIDE)). The latter may result introduce small errors into calculations. INTRINSIC FUNCTIONS In most of the calculations that we may wish to perform there are a number of standard operations that we may wish to perform. Many of these operations will involve standard algebraic or trigonometric operators. FORTRAN 77 has a number of built-in operators that we can use for these operations. Many of these intrinsic functions are listed in Table 2.4 on page 21 of Etter. Some of these functions include: SQRT( number or expression) COS (angle in radians) [ Note ACOS, ASIN, ATAN are all "ARC" functions or the Trigonometric functions] ABS (number or expression) etc. SIMPLE INPUT and OUTPUT (Page 22 - Etter) We will first learn how to receive input directly from the keyboard and print output directly to the screen. This is sometimes referred to as list-directed input and output. List-directed input: We can receive input directly from the keyboard with the simple READ statement: READ*, variable list The program will wait for the user to input the required values. The data values from the user may be either separated by commas or blanks. After the data has been typed into the command line, the user will press the return key and the program will continue. In general, the READ statement will have to preceded by an instruction that asks the user to put the data in. This can be simply conducted with the List-Directed Output which takes the similar form as the List-Directed Input statements: PRINT*, expression list values in the expression list should be separated by commas. Statements can be include in the Print statement by enclosing the desired expressions in single quotation marks or apostrophes: PRINT*, ‘Please input the area and length separated by commas’ this may be followed by: READ*, AREA, ALGTH Or if we have a variable named AREA PRINT*, ‘The Area of the rod is ’, AREA This will output the character string "The Area of the rod is" followed by the value of the variable AREA. In many situations we may wish to have formatted output which may be accomplished by the following statement: PRINT k, expression list where k is the address of a format statement for the output(recall that this is an address place in columns 1 through 5). For Example: Print 5, AREA, ALGTH 5 FORMAT (5X, ‘The area of the rod is ’, F6.2,/, + 5X,‘The length of the rod is ’, F6.3) The "5X" in the above expression is just specifying to leave 5 blanks before our character string, "The area of the rod is". The basic form of this is #X, where # is the number of blanks that we wish to leave. The F6.3 in the format statement is telling the compiler the desired format for the values of AREA - at most 6 digits with 2 points past the decimal. The "/" in the above expression specifies to start a new line. Format for Real values: The F6.2 and F6.2 are expressions for cases in which a real value is being printed. The basic form of the F command is Fw.d where w represents the total width(number of digits including the decimal point and any minus sign. Format for Integer values: If instead we have an integer we would use I5, or I3, or Iw where w is the number of positions or digits (including the minus sign for negative values). Format for Exponential values: If a very large or small value is expected, it will be more efficient to use exponential form. This is done with the Ew.d format, where E specifies that this will be scientific notation, w is the total width or number of positions to be used in printing the value. The d represents the number of positions to the right of the decimal point. E9.3, E7.2. If the actual output is larger than the widths specified in the formatting statement, the output will be asterisks. It is very important to remember to include room for the decimal and a potential minus sign for negative values. All of the values will be right justified so that if we specify too large of a space this will result in blank spaces preceding our output.