Physics 222 Assignment 1

Due January 23

Directions

Please turn in this assignment (and all future assignments for this course) by emailing them to me . Please use the subject of "Physics 222 HW1" (note that the spacing matters) for these assignments (if you use the link above, the subject will be added automatically). Please attach separately the source code to all the programs that you write for this assignment. Each program should be attached separately to make it easier for me to compile them. When answering the questions themselves feel free to answer directly in the body of your message or attach your answers as a separate file. You may want to copy and paste the questions into your message, but please do not send me html email - I prefer plain text.

  1. Compilation and Running

    This problem involves compiling and running the example program "convert". First, compile the Fortran 90 version of this program. To do this first save a copy of the Fortran file to somewhere in your home directory in your Unix account. By the way, it probably would be a good idea to make a directory (or folder, if you prefer that term) in your Unix account for this class. You can make a directory with the command "mkdir" or using a filemanager like "konqueror".

    To compile the program, open up a terminal window (using a program like "Terminal", or "Konsole", or "xterm", or ...). Then at a command prompt go to the directory where you have the convert.f90 file. Use "cd" to move around and "ls" to check to see what files are in the directory that the terminal window is in.

    Then to compile the program at a command prompt type (or copy and paste):

          gfortran -o convert_f convert.f90
        

    The name of the compiler is "gfortran". The program gfortran is part of the free gcc compiler suite. The "-o" is what is known as a command-line option, which are common with command-line programs. In this case "-o" tells the compiler what to name the output executable file that it creates, in this case "convert_f". You can call your executable program just about anything that you'd like, though it is most common for it to have the same prefix as the source code. Here I have added the _f (for Fortran) to keep it distinct from the C++ version below. If you leave out the "-o" option the executable name defaults to "a.out". Finally, "convert.f90" is the name of the source code to compile.

    When you type in this command, if everything works correctly an executable file name "convert_f" will be created. If not, then you will get some error messages. To check to see if the executable was created type:

          ls -l
        

    Now that you have compiled the Fortran version of "convert", do the same for the C++ version. The process will be identical except for slight changes in the command-line:

     
          g++ -o convert_c convert.C
        

    Here the main changes are that the compiler is named "g++" and the file is called "convert.C".

    Now run each version of the program with the following inputs for the number of seconds: 0, 100, 100.111, 9347, 555554, -11, 12345678901, and 1234567890123456789012345678901234567890 . To run the Fortran version of the program type:

         ./convert_f
       

    and similarly for the C++ version. Copy and paste all of your results into your solution for this assignment. (To copy and paste with Unix select what you want to copy with the left mouse button, move the pointer to where you want to paste, and click the middle mouse button to paste).

    1. Are there any noticeable differences between the results from the two versions of the program?
    2. Discuss the results for 100.111 seconds.
    3. Comment on the results for 12345678901 and 1234567890123456789012345678901234567890 seconds. Do you have any idea why the results turned out this way - both for the Fortran and the C++ version of the code? Explain.
  2. Alter the programs above

  3. Being able to take someone else's code and make alterations to it (even when you don't know the language in question) is an important skill to have. Now you will alter the two "convert" programs that you used above to calculate the number of days, hours, minutes, and seconds instead of just the number of hours, minutes, and seconds. Test both the C++ and Fortran versions of your improved convert programs with the input listed above. Copy and paste your results into your homework solution.

    1. Are there any noticeable differences between the results from the Fortran and C++ versions of this program? How do they compare to the differences that you listed above?
  4. Writing your own programs

    Now you will write your own programs similar to "convert". Write programs in C++ and Fortran that takes an input area in square meters, then it converts and prints the equivalent area in square feet, acres, hectares, and barns. To begin with declare the numerical variables as "integer"s in Fortran or "int"s in C++.

    1. Run your program with the following input areas: 5, -50, 50000, 5555.5, and 6.59374E-27 . As in the other problem copy and paste these results into your solution. Do you see any problem with these results? What is happening?

  5. Alter your own program

    Now change the variable declarations in your code so that all numerical variables are declared with "float" in instead of "int" C++ and "real" instead of "integer" in Fortran. Also, make sure that any numerical constants that you use include a decimal point (i.e. 99 => 99. or 99.0). Re-run the same input areas as above and copy the results into your solution.

    1. What has changed? Do you results now match what you expect?
  6. Answer the following Chapter Review questions from chapter 2 in your testbook:

    3, 5, and 8.