Programming Assignment (1) Solution
Programming Assignment (1) Solution
Programming Assignment (1) Solution
SCHOOL OF
POST GRDUTAE STUDIES
ASSIGNMENT NO. IV
ID.NO: MSc/KP/H/014/03
Submitted by:-
Eshetie Tesfaye
October, 2012
1. State whether each of the following program names is valid or not. If not, state why the name
is invalid.
a) Junk
Answer: Valid Integer Variable
b) 3rd
Answer: Invalid → Program Name Must Begin With a Letter
c) Who_are_you?
Answer: Valid Character Constant
d) time_to_intercept
Answer: Valid Real Variable
2.Which of the following expressions are legal in FORTRAN? If an expression is legal, evaluate it.
a. 2.**3/3**2 Legal Expression
Evaluate exponents first: 8. /9
Evaluate divisions: 0.8889 (Real Result)
b. 2*6 + 6**2 / 2 Legal expression
Evaluate exponents first: 2*6 + 36 /2
Evaluate multiplications and divisions: 12 + 18
Evaluate addition: 30 (Integer Result)
c. 2* (-10)**-3 Illegal Expression ( two adjacent operators )
d. 2 / (-10.)**3 Legal Expression
Evaluate exponents first: 2 / (-1000.)
Evaluate divisions: -0.002 (Real Result)
e. 23 / (4 / 8) Illegal Expression ( division by zero )
3.Write the Fortran statements required to calculate y (t) from the equation
SOLUTION
The function y (t) is evaluated differently, depending on the sign of the variable t. To determine the proper
equation to apply, it will be necessary to check for the sign of the t value supplied by the user.
State the problem
This problem statement is very simple: Evaluate the function y (t) for any user supplied value of t.
The inputs required by this program are the values of the variable t. The output from the program will be
the value of the function y (t).
This task can be broken down into three major sections, whose functions are input, processing, and
output:
Calculate y (t)
We will now break each of the above major sections into smaller, more detailed pieces.
There are four possible ways to calculate the function y (t), depending on the values of t, so it is logical to
implement this algorithm with a two-branched IF statement. The resulting pseudo code is:
Read t
IF t ˃ 0 THEN
fun ← (-3)*t**2 +5
ELSE IF t ˂ 0 THEN
fun ← 3*t**2 +5
END IF
PROGRAM funt
!
! Purpose:
! This Program solves the function y(t) for a user specified t,
! Where y(t) is defined as:
! _
! |
! Y(t) = | (-3)*t**2 + 5 t >= 0
! | 3*t**2 + 5 t < 0
! |_
!
! Record of revisions:
! Date Programmer Description of change
! ========== ============= =======================
! 14/10/2012 Eshetie Tesfaye Original code
!
IMPLICIT NONE
Start
READ t
WRITE t
.FALSE.
t>0
.TRUE.
WRITE fun
Stop
4.Write a Fortran program to evaluate the equation y�x� � x 2 � 3x � 2 for all values of x between ↸1 and
3, in steps of 0.1.
SOLUTION
This problem statement is very simple: Evaluate the function y (x) for any user supplied value of x
between -1 and 3 in steps of 0.1.
Define the Inputs and Outputs
The inputs required by this program are the values of the variable x. The output from the program will be
the value of the function y (x).
This task can be broken down into three major sections, whose functions are input, processing, and
output:
Calculate y (x)
There is one possible way to calculate the function y (x), depending on the values of x, so it is logical to
implement this algorithm with one IF statement. The resulting pseudo code is:
Read x
IF -1 < x ˂ 3 THEN
𝑓𝑢𝑛 ← x 2 � 3x � 2
END IF
PROGRAM funx
!
! Purpose:
! This Program solves the function y(x) for a user specified x,
! where y(x) is defined as:
!
! Y(x) = x**2 - 3*x + 2 -1 < x < 3
!
! Record of revisions:
! Date Programmer Description of change
! ========== ============= =======================
! 14/10/2012 Eshetie Tesfay Original code
!
IMPLICIT NONE
Start
READ x
WRITE x
.FALSE.
-1 < x < 3
.TRUE.
x ← x + 0.1
𝑓𝑢𝑛 ← 𝑥 ∗∗ 2 � 3 ∗ 𝑥 � 2
WRITE fun
Stop
5.Write a Fortran program to implement the quadratic Lagrange polynomial given by the following
equation
The program must define the data set, and print the data set and the solution.
Solution
Each of the three terms in the given equation passes through one of the data points and is zero at the other
two. The summation of the three terms must, therefore, be the unique second order polynomial f2(x) that
passes exactly through one of the data points.