UNIPORT CSC 280: Answers to Introduction to Computer Programming (Modern FORTRAN) Practical Workbook Part II

Spread the love

Contents

fortran programs

CSC 280 Answers to Practical Workbook – Introduction to Computer Programming, Modern FORTRAN Language Programs – CSC 280 is a course from Department of Computer Science, Faculty of Science in the University of Port Harcourt as well as other Universities. It is offered as borrowed course by other departments to enable them have the knowledge of programming. Though, it was introduced to the University as GES 101 – Computer Appreciation and Application.

In this course, ForTran – Formula Translator language is obeyed. Students are expected to run the programmes using Plato FORTRAN Application.

The objective of the course is that, students will learn how to run programmes on their own before semester ends. Thus, the initiation of the course in the University.

To pass this course is very easy as you are very lucky to come across this article. You must fill your manual or workbook very well. You can use download the PDF of the ones we have filled to fill yours. Also, you are required to do well in your Test then your exams.

FOR FORTRAN WORKBOOK ANSWERS PART I =

FORTRAN (Formula Translation) Arithmetic Operators, Logical Operators or Relational Operators and Order of Precedence (Priority)

FORTRAN ARITHMETIC OPERATORS

Operators Actions
() Bracket (B)
** Exponentiation (E)
/ Division (D)
* Multiplication (M)
+ Addition (A)
Subtraction (S)

In FORTRAN order of precedence is BEDMAS

LOGICAL OPERATORS OR RELATIONAL IN FORTRAN

Operators Actions
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
= = Equal to
.or. Logical OR
.and. Logical AND
.not. Logical NOT

FORTRAN ORDER OF PRECEDENCE FOR ARITHMETIC OPERATORS

Operators Actions
() Bracket (B)
** Exponentiation (E)
/ Division (D)
* Multiplication (M)
+ Addition (A)
Subtraction (S)
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
.not. Logical NOT
.and. Logical AND
.or. Logical OR

Lab 13: Simple Maths

Aim: To write a Fortran program to add, subtract, multiply or divide two numbers based on a selection.

ALGORITHM

  1. Declare all variables
  2. Enter values to A and B
  3. Display values of A and B
  4. If selection equals 2, subtract B from A.

If Selection equals 3, multiply A by B

If selection equals 4, divide A by B

OBSERVATIONS/OUTPUTS

Observation: the program performs simple arithmetic operation of addition, subtraction, multiplication and division on two real values that will be given as run time.

Output: if A=10, B=5 and selection =3. Then, the output will be:

A*B=50

Lab 14: LINEAR EQUATION

Aim: write fortran program to solve a pair of linear equation.

ALGORITHM

  1. Declare all variables
  2. Supply values to variables
  3. Display the equations
  4. Calculate the value of X and Y
  5. Display the values of X and Y

OBSERVATION AND OUTPUT

Observation: the program calculates the values of X and Y for any given pair of linear equations of the form:

A1X + B2Y = C1

                A2X + B2Y = C2

Such that if A1*B2-A2B1=0 then, the equation has no solutions.

Output: if A1 =1, B1=1, C2=2

A2 = 1, B2 = -1, C2 = 2

The output will be:

X: 2.000000

Y: 0.000000

Lab 15: Sort integers in Ascending order

Aim: write a Fortran program to reads in three Integer numbers and displays them in ascending order.

ALGORITHM

  1. Declare all variables
  2. Supply values to variables
  3. If a is less than b and c and b is less than c then print a, b, c else print a, c, b.

If a is greater than c but less than b print b, a, c else print c, a, b

If b is greater than c and c is greater than a then print b, c, a else print c, b, a

Lab 16: CALCULATING INCOME TAX

Aim: to write a fortran program to calculate income tax of a person.

OBSERVATIONS/OUTPUT

Observation: the name of the program is income tax. ‘age’ is declare as INTEGER while ‘tax’ and ‘income’ are declare as REAL. The program takes the age and yearly income of the subject using READ statement compares the age and yearly income of the subject with a specified range of values for age and yearly income, and determines whether subject will pay income or not. If the subject will pay income tax, the program will determine how much income tax should be paid.

Output: Let age = 85, income = 450000

The output will be:

NO INCOME TAX TO BE PAID

Lab 17: Test grading

Aim: Enter the following program code into a Fortran compiler and print the result with different test scores and exam score to determine the grade assigned to the computation of the score. Explain the reason while test total is assigned the value of 1 in the program.

ALGORITHM

  1. Declare all variables
  2. Supply test 1, test 2, test 3 and exam.
  3. If test 1<=10, test 2<=10, test 3<=10.
    • Calculate test ave.
    • Calculate test total i.e. testtotal = test ave.
    • Display test ave., exams testtotal

OBSERVATION/OUTPUT

Observation: the program calculates the total score of a student in a particular course. Three test scores are supplied (test 1, test 2, and test 3), their average is calculated and added to the exam score of the student this is the total score of the student (i.e. testtotal) in a particular course.

Output: Let test 1=6, test 2=7, test 3=9 and exam =5

The output will be:

Average of the three test score is 22

Exam score is 56

Total score is percentage is 78

Exercises

Write a FORTRAN program to calculate the roots of a quadratic equation using the general formula. The program should be able to check if the result will be real roots or not

Source code:

! Program to calculate the root of a quadratic equation

Program Equation

Implicit None

Real:: a, b, c, d, root1, root2

Write (*,*) “Enter the value of a, b and c”

Read (*,*) a, b, c

d = b**2 – 4*a*c ! calculating discriminant

if (d≥0) then d = SQRT (d)

root1 = (-b + d)/2*a

root2 = (-b – d)/2*a

Write (*,*) “The roots are:” root1, “and”, root2

else

Write (*,*) “The roots are complex and cannot be calculated”

end if

End program Equation

 

Write a FORTRAN program that reads an item

Lab 18: Factorial

Aim: A fortran program that calculate the factorial of any given number

ALGORITHM

  1. Declare all variables
  2. Supply value to K
  3. Do n=1, K

multiply nfact by n,

print n and nfact,

end do

  1. If n is less than 20 then print nfact

OBSERVATIONS/OUTPUT

Observation: the name of the program is factorial and it calculates the factorial of any value supplied to k. it also checks if n is less than 20 am prints the value of nfact.

Output: if k=3

  • 2
  • 2
  • 6

6

Lab 19: Static Array

Aim: A program that demonstrate the use of static array

ALGORITHM

  1. Declare all variables including arrays
  2. Assigning values to array v and k
  3. Do i = 1, 4

Add arrays v and k, and store in

Print value of z (i) for each value of i

End do

OBSERVATIONS /OUTPUT

Observations: the program contains two one dimensional arrays (i.e. v and k) with values and another one dimensional array without initializing it. The values arrays v and k are added and stored in array z

Output:       16

32

14

28

Lab 20: Sum of Consecutive numbers

Aim: A program that computes the sum of any “k” consecutive numbers.

ALGORITHM

  1. Declare all variables
  2. Supply value to k
  3. Do i = 1, k

Sum all values from 1to k

end do

  1. Display the sum

OBSERVATIONS/OUTPUT

Observation: the program is testsum. This program sums from 1 to a given value of k and display the result.

Output: if k = 5

The sum is: 15

Lab 21: DO – Loop

Aim:  A FORTRAN program that demonstrate the of a DO – Loop

ALGORITHM

  1. Declare three integer variable
  2. Initialize j to zero
  3. Do i = 8, 1, -1

Increase the value of j by 1

Multiply j by 9 and store the rest

Display the value of j and k

end do loop.

OBSERVATIONS/OUTPUT

Observation: the name of the loop demo. The do loop has initial value of 8, final value of and an increment of -1. The program prints the value of j and k at each iteration.

Output:

  • 9
  • 18
  • 27
  • 36
  • 45
  • 54
  • 63

Lab 22: Nested Do Loop

Aim: A program that demonstration the of a nested do loop.

ALGORITHM

  1. Declare all variables
  2. Print “start”
  3. Do i = 1, 3

Do j = 1, 2

Print the product of i and j

end do

  1. Print “end”

 

OBSERVATIONS/OUTPUT

Observation: the name of the program is testloop ad it illustrates the use of nested do loop. If prints the product of i and j

Output:

i*j = 1

i*j = 2

i*j = 2

i*j = 4

i*j = 3

i*j = 6

 

Lab 23: Addition of Matrix

Aim: A Fortran program that can ADD the elements of a two (3×4) matrix array.

ALGORITHM

  1. Declare all variables and array v, k, and z.
  2. Supply values for array v
  3. Supply values for array k
  4. Calculate array z (i.e. z(i, j) = v(i, j) + k(i, j))

 

OBSERVATIONS/OUTPUT

Observation: The program contain three two-dimensional array namely: v, k and z. Values will be supplied to array v and k, and the values will be added and stored in third array named z. i.e. z(i, j) = v(i, j) + k(i, j). The arrays declared in the program are 2×3 two dimensional arrays and a nested do looped is used to supply values to array v and k, and to perform the addition operation.

Output:  the addition of the two arrays

Position 11 is 4

Position 12 is 7

Position 13 is 0

Position 21 is 8

Position 22 is 6

Position 23 is 10

Lab 24: Sum and Average of N Consecutive

Algorithm

  1. Declare all variables
  2. Supply value to count and initialize sums to zero
  3. Do number = 1, count

Supply value to data item

Sum all data item values

end do

  1. Calculate average
  2. Display average

Observations/Output

Observation: The program calculates the sum of the values stored in dataitem and calculates the average

Output:

If count = 3

When count = 1

dataitem = 4

when count = 2

dataitem = 5

when count = 3

dataitem = 6

average = 5.00000

To be updated on all Uniport Materials for Examination and Sure Questions

Subscribe to this site using your Email address for FREE

LIKE our Facebook page for updates

FOLLOW our Twitter Handle for udpates

UNIPORT CSC 280: Answers to Introduction to Computer Programming (Modern FORTRAN) Practical Workbook Part II

Leave a Reply

Your email address will not be published. Required fields are marked *

google-site-verification: googlea02b72f032db8970.html
error: Content is protected !!
Advertisement