{"id":7047,"date":"2024-08-20T03:00:00","date_gmt":"2024-08-20T07:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=7047"},"modified":"2024-08-13T21:44:12","modified_gmt":"2024-08-14T01:44:12","slug":"guess-the-number-in-fortran-77","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=7047","title":{"rendered":"Guess the number in FORTRAN 77"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"7047\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>New computer science graduates are excited to work in Go or Rust or Python &#8211; the modern languages they learned during their program &#8211; only to be tasked to support a legacy programming language like FORTRAN. And there\u2019s a lot of FORTRAN 77 out there, especially in the engineering and science fields. <a href=\"https:\/\/www.both.org\/?p=6923\">Learning some FORTRAN 77<\/a> can take you a long way.<\/p>\n\n\n\n<p>But FORTRAN 77 doesn\u2019t have to be all dull work. You can have fun with it too! Let\u2019s exercise FORTRAN 77 by writing the classic \u201cguess the number\u201d game, where the computer generates a random, secret value from 1 to 100, and you must guess what it is.<\/p>\n\n\n\n<p>The \u201cguess the number\u201d game exercises several concepts in programming, including how to declare variables, how to store values, how to print text, how to read input, and how to compare numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-basics-of-fortran-77\">The basics of FORTRAN 77<\/h2>\n\n\n\n<p>FORTRAN is a very old language, probably the oldest <em>compiled<\/em> \u201chigh level\u201d language that\u2019s still around. The first FORTRAN (<strong>for<\/strong>mula <strong>tran<\/strong>slation) was released in the 1950s, FORTRAN-II and FORTRAN-III in the late 1950s, FORTRAN 66 in 1966, and FORTRAN 77 in 1977. Later versions changed the spelling to \u201cFortran,\u201d starting with Fortran 90, which was meant to be codified in 1990 but wasn\u2019t available until 1991. I learned FORTRAN 77 as an undergraduate physics student in the early 1990s, so that\u2019s the version that I remember.<\/p>\n\n\n\n<p>Old-style FORTRAN statements were originally written on punched cards, and the language retains some historical limitations because of that. For example, FORTRAN 77 statements could only be written in all uppercase. Also, FORTRAN 77 uses significant columns: the first five columns on a line were reserved for a statement label, which was just a number. Entering <code>C<\/code> or <code>*<\/code> in the first column indicated a <em>comment line<\/em>.<\/p>\n\n\n\n<p>A number or one of a few other recognized characters in column six indicated a <em>continuation<\/em>, and that \u201ccard\u201d or statement was tacked onto the previous one. Program instructions could only be written from column 7 to 72; columns 73 to 80 were reserved for <em>card sorting<\/em>, such as when you dropped a stack of punched cards on the floor and needed to use a card sorting machine to put them back into order.<\/p>\n\n\n\n<p>Due to the limited amount of space on each card to write program instructions, old-style FORTRAN could ignore spaces between keywords and names. But I\u2019ll use spaces in this example to keep things more clear.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"program-structure\">Program structure<\/h2>\n\n\n\n<p>You define FORTRAN 77 programs using the <code>PROGRAM<\/code> keyword, and terminate them with the <code>END<\/code> keyword. You must give every program a name, starting with a letter and followed by one or more letters or numbers. In the simplest example, you might define an empty program called <code>TEST<\/code> like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      PROGRAM TEST\n      END<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"declaring-variables-and-storing-values\">Declaring variables and storing values<\/h2>\n\n\n\n<p>FORTRAN 77 provided several variable types, including <code>INTEGER<\/code> for whole numbers and <code>REAL<\/code> for floating point values. Variable names started with a letter, and could be letters or numbers after that. For example, to declare a variable called <code>COUNT<\/code> as an <code>INTEGER<\/code>, you would write:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      INTEGER COUNT<\/code><\/pre>\n\n\n\n<p>If you wanted to define more than one variable at a time, but of the same type, you separated the names with commas:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      INTEGER COUNT,TOTAL<\/code><\/pre>\n\n\n\n<p>To store a value in a variable, use the equal sign. For example, type this to store the value 0 in the variable called <code>TOTAL<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      TOTAL = 0<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"comparing-values\">Comparing values<\/h2>\n\n\n\n<p>FORTRAN supports several ways to compare values. The most basic is number comparisons, such as these:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Comparison<\/th><th>What it means<\/th><\/tr><\/thead><tbody><tr><td><code>.LT.<\/code><\/td><td>Less than<\/td><\/tr><tr><td><code>.GT.<\/code><\/td><td>Greater than<\/td><\/tr><tr><td><code>.LE.<\/code><\/td><td>Less than or equal<\/td><\/tr><tr><td><code>.GE.<\/code><\/td><td>Greater than or equal<\/td><\/tr><tr><td><code>.EQ.<\/code><\/td><td>Equal<\/td><\/tr><tr><td><code>.NE.<\/code><\/td><td>Not equal<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Use the <code>IF<\/code> statement with these comparisons. You can write <code>IF<\/code> statements as a one-line instruction or as a block. The simplest and oldest syntax is the one-line statement, like this to jump to (<code>GO TO<\/code>) a line label if a value is greater than zero:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      IF (TOTAL .GT. 0) GOTO 10<\/code><\/pre>\n\n\n\n<p>With the one-line <code>IF<\/code> statement, if the comparison isn\u2019t true, then nothing happens. So the program will only jump to label 10 if the value in <code>TOTAL<\/code> is greater than zero. For values zero or less, FORTRAN 77 executes the next statement in the program.<\/p>\n\n\n\n<p>You can also use a block <code>IF<\/code> statement. These use the <code>THEN<\/code> keyword to start the block, and <code>END IF<\/code> to end the block. For example, to increment a counter if it\u2019s less than ten, you could write this <code>IF<\/code> block:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      IF (COUNT .LT. 10) THEN\n        COUNT = COUNT + 1\n      END IF<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"simple-arithmetic\">Simple arithmetic<\/h2>\n\n\n\n<p>This statement also shows how to use variables and values with simple arithmetic. FORTRAN supports these operations:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operation<\/th><th>What it does<\/th><\/tr><\/thead><tbody><tr><td><code>+<\/code><\/td><td>Add<\/td><\/tr><tr><td><code>-<\/code><\/td><td>Minus<\/td><\/tr><tr><td><code>*<\/code><\/td><td>Multiply<\/td><\/tr><tr><td><code>\/<\/code><\/td><td>Divide<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Arithmetic operations are always done in proper math order: parentheses are evaluated first, then multiplication and division, then addition and subtraction. For example, to calculate Newton\u2019s equation of motion (a staple in physics) using some predefined values, you could enter this FORTRAN 77 statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      PROGRAM NEWTON\n      REAL Y,Y0,V0,T,G\nC INITIALIZE VARIABLES\n      Y0 = 10.0\n      V0 = 0.0\n      T = 1.0\n      G = 9.8\nC CALCULATE RESULT\n      Y = Y0 + V0 * T + .5 * G * T * T\n      END<\/code><\/pre>\n\n\n\n<p>Because FORTRAN was created to make it easier to solve formulas, and because formulas often include exponents, FORTRAN 77 also supports \u201cpowers\u201d or exponents using <code>**<\/code>. These take the same level of precedence as parentheses. You could rewrite Newton\u2019s calculation with an exponent, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      Y = Y0 + V0 * T + .5 * G * T**2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"functions-and-subroutines\">Functions and subroutines<\/h2>\n\n\n\n<p>FORTRAN 77 provides a collection of standard library functions that help you do your work. Functions take one or more values and return a single value. You might guess that many functions are mathematical in nature, like <code>COS<\/code> for cosine and <code>SIN<\/code> for sine. But you can find other functions for all kinds of things, including random values.<\/p>\n\n\n\n<p>The <code>RAND<\/code> function generates a random number from 0 to 1 (although it never reaches 1, so the maximum value is like 0.999\u2026) For historical reasons, the <code>RAND<\/code> function takes an argument: 0 if it should return the next <a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gfortran\/RAND.html\">pseudo-random number<\/a> in the sequence, or 1 if it should restart the random number generator.<\/p>\n\n\n\n<p>Before you can use the random number generator in FORTRAN 77, you need to initialize it <a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc-4.9.4\/gfortran\/SRAND.html\">with a seed value<\/a>. You do this with a <em>subroutine<\/em> called <code>SRAND<\/code>, and give it a seed number. To use a subroutine, use the <code>CALL<\/code> keyword, then the name of the subroutine. For example, to seed the random number generator with a predetermined value, then print a few random numbers, you might write this sample program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      PROGRAM RAND000\n      CALL SRAND(23456)\n      PRINT *, RAND(0)\n      PRINT *, RAND(0)\n      PRINT *, RAND(0)\n      END<\/code><\/pre>\n\n\n\n<p>The <code>PRINT<\/code> instruction prints one or more values using a <em>format<\/em> statement. In simple programs, you can get away with using <code>*<\/code> to indicate a <em>default<\/em> format, where FORTRAN 77 will just print the value in a way that seems to make sense.<\/p>\n\n\n\n<p>In the above program, each <code>PRINT<\/code> statement prints a single number. To print all three random numbers on one line, you can separate the values with a comma:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      PRINT *, RAND(0), RAND(0), RAND(0)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a-program-to-guess-a-number\">A program to guess a number<\/h2>\n\n\n\n<p>We can put all this together to write a fun program to guess a random number. In this program, the computer picks a random number from 1 to 100, then the user must guess the number. After each guess, the program prints \u201ctoo low\u201d or \u201ctoo high\u201d to help the user with their next guess. If the user guesses the secret number, the program prints \u201cthat\u2019s right!\u2019<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      PROGRAM GUESS00\n      INTEGER SECRET,GUESS,SEED\nC F77 REQUIRES YOU TO PROVIDE YOUR OWN SEED TO THE RND NUM GENERATOR\n      PRINT *, 'ENTER A RANDOM SEED:'\n      READ *, SEED\n      CALL SRAND(SEED)\n      SECRET = INT(RAND(0)*100)+1\nC GUESS THE NUMBER\n      PRINT *, 'GUESS THE RANDOM NUMBER FROM 1 TO 100'\n10    PRINT *, 'YOUR GUESS?'\n      READ *, GUESS\n      IF (GUESS.LT.SECRET) PRINT *, 'TOO LOW'\n      IF (GUESS.GT.SECRET) PRINT *, 'TOO HIGH'\n      IF (GUESS.NE.SECRET) GOTO 10\n      PRINT *, 'THATS RIGHT!'\n      END<\/code><\/pre>\n\n\n\n<p>I\u2019ve added comment lines to help break up the program into more understandable parts. The first two lines define the program with the <code>PROGRAM<\/code> keyword, and define a list of variables for the secret number, the user\u2019s guess, and the random number seed.<\/p>\n\n\n\n<p>After that, the program prompts the user for a random number seed, and initializes the random number generator with the <code>SRAND<\/code> subroutine. Then it uses the <code>RAND<\/code> function to generate a random value; the program multiplies the random number by 100, giving a random value between 0 and 99.999\u2026 Taking the integer of that value gives a random number from 0 to 99. Adding one gives a final random number between 1 and 100.<\/p>\n\n\n\n<p>The rest of the statements do the main body of work. The program prints a prompt, then reads the user\u2019s guess with <code>READ<\/code>. Two <code>IF<\/code> statements tell the user if their guess was too low or too high. A third <code>IF<\/code> statement jumps back to the prompt statement if the user hasn\u2019t yet guessed the secret number<\/p>\n\n\n\n<p>The program continues to the final <code>PRINT<\/code> statement only when the user\u2019s guess matches the secret random number. And that\u2019s where the program ends.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"playing-the-game\">Playing the game<\/h2>\n\n\n\n<p>Let\u2019s compile the program using the GNU Fortran compiler, part of the GNU Compiler Collection. If you don\u2019t have this installed on your system, most Linux distributions should provide it as a package that you can install like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo dnf install gcc-gfortran<\/code><\/pre>\n\n\n\n<p>Save the program as <code>guess.f<\/code> and compile it with the <code>gfortran<\/code> command. Because my program is written in classic FORTRAN 77, I\u2019ve used the <code>-std=legacy<\/code> option so the compiler doesn\u2019t complain about any old-style syntax.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gfortran -std=legacy -o guess guess.f<\/code><\/pre>\n\n\n\n<p>When you run the program, you\u2019ll be prompted to enter a seed value for the random number generator; enter any integer, usually a large value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ .\/guess\n ENTER A RANDOM SEED:\n3456\n GUESS THE RANDOM NUMBER FROM 1 TO 100\n YOUR GUESS?\n50\n TOO HIGH\n YOUR GUESS?\n25\n TOO HIGH\n YOUR GUESS?\n15\n TOO HIGH\n YOUR GUESS?\n5\n TOO HIGH\n YOUR GUESS?\n1\n TOO LOW\n YOUR GUESS?\n3\n THATS RIGHT!<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"keep-exploring\">Keep exploring<\/h2>\n\n\n\n<p>The \u201cguess the number\u201d game is a great introduction to programming, and it\u2019s often one of the first programs I write when exploring a new programming language. Use this as a starting point to learn more about classic programming with FORTRAN 77.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explore classic programming in FORTRAN 77 with this sample program.<\/p>\n","protected":false},"author":33,"featured_media":2803,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[69,150],"tags":[147,152],"class_list":["post-7047","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fun","category-programming","tag-fun","tag-programming"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7047"}],"version-history":[{"count":1,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7047\/revisions"}],"predecessor-version":[{"id":7048,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/7047\/revisions\/7048"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2803"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}