Homework 2, due September 23, 2010.
Posted: Thu Sep 09, 2010 9:29 am
Be sure to upload all your programs in by replying to the uploads link to Homework 2. Also be sure to print all your programs and submit them with the written part of your homework.
Collaborative Problems:
This homework will ask you to intentionally make a lot of programming mistakes, which will hopefully help you avoid the most common pitfalls. You can combine as many of the questions as possible into single programs, but make sure that whatever you submit addresses each problem. You may exercise your own judgment in this regard, but I would observe that it will usually make it easier for a TA to grade the homeworks if the programs are organized in a manner (with comments) that indicate where exercise is being addressed.
For each question and/or problem, you must both
Collaborative Problems:
This homework will ask you to intentionally make a lot of programming mistakes, which will hopefully help you avoid the most common pitfalls. You can combine as many of the questions as possible into single programs, but make sure that whatever you submit addresses each problem. You may exercise your own judgment in this regard, but I would observe that it will usually make it easier for a TA to grade the homeworks if the programs are organized in a manner (with comments) that indicate where exercise is being addressed.
For each question and/or problem, you must both
- write the general answer; and,
- illustrate it in a program.
- What happens in an algebraic computation when the quotient of two integers is computed?
- Answer: the quotient is computed and then truncated and the value returned is an integer.
Part of a complete program that you upload should contain:Code: Select all
c Problem 1 print *, 'The quotient 10/3 is ', 10/3
- Answer: the quotient is computed and then truncated and the value returned is an integer.
- What happens when a real number is divided by an integer?
- What happens when an integer is divided by a real number?
- Do the same results hold for the previous three questions if the computations are done with variables instead of numbers? For example
Instead of using the code written right above this, write a program that demonstrates all three and correctly uses implicit data typing.
Code: Select all
integer x,y print *, 'x/y =', x/y
- Repeat the previous problem, but put IMPLICIT NONE as the first statement and properly declare all the variables.
- As pointed out in class, programs with variables with names longer than six characters will compile and run. Can a program distinguish between variables that have more than six characters, e.g., will
do what you expect?
Code: Select all
variable1=3.0 variable2=2.0
- What happens when you try to compile a program with variables that are not properly named, e.g., if a variable starts with a number?
- Can you compile a program with a variable that starts with a symbol such as "@"?
- Print out or copy the following arithmetic expressions.
Construct a diagram for each expression like the following figure that indicates the order in which the operations will be performed. Unlike the diagram below, write next to each number that indicates the order in which the computations are performed, what the value is that results from the computation. For example, if A=2, then next to the 2 for the second computation, put a 4.1. Verify the final answer you compute by copying the expressions into a program and printing the answer.
Code: Select all
x = 16.0**4.0*2.0 x = (2**4/12+3)*(18/(2+4))**5/4 x = 2/3/4/5*4*3*2+4+3+9/(3+5)**2 x = 2+3+4**(5/2+3)
- Write a FORTRAN program that allows a user to input the x and y values of two points in the plane and the x value for a third point. Have the program compute and then print out to the screen the y value for the third point if it is on the line that connects the first two points.