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" for these assignments (if you use the link above, the subject will be added automatically). Please attach the source code to any programs that you write for this assignment. If you write more than one program, attach each separately. 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" from page 16 in the text. 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 at a command prompt type:

          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. 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 a program in C++ or Fortran that takes an input temperature in Fahrenheit and converts it to Celsius, Kelvin, and Rankine. 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 Fahrenheit temperatures: -459, 0, 32, 212, 1000.00. As in the other problem copy and paste these results into your solution. Do you see any problem with these results?

    2. Now change the variable declarations in your code so that all numerical variables are declared with "float" in C++ and "real" 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 temperatures as above and copy the results into your solution.

    3. What has changed? Do you results now match what you expect?