Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Fortran 90 Programming Questions for Structural Engineering, Exams of Mathematical Methods for Numerical Analysis and Optimization

A set of programming questions in fortran 90 as part of the computing & numerical methods module for the bachelor of engineering (honours) in structural engineering program at cork institute of technology. The questions cover various concepts including conditional statements, loops, input/output, and computations with integers and characters.

Typology: Exams

2012/2013

Uploaded on 03/28/2013

shona
shona 🇮🇳

4

(1)

18 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Cork Institute of Technology
Bachelor of Engineering (Honours) in Structural Engineering – Stage 1
(NFQ – Level 6)
Summer 2006
COMPUTING & NUMERICAL METHODS
Time : 3 Hours
Answer Question 1 and 2 other questions. Examiners :
Prof. P.E.O’Donoghue
Mr. T.Corcoran
Dr M.G. Murphy
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Fortran 90 Programming Questions for Structural Engineering and more Exams Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

Cork Institute of Technology

Bachelor of Engineering (Honours) in Structural Engineering – Stage 1

(NFQ – Level 6)

Summer 2006

COMPUTING & NUMERICAL METHODS

Time : 3 Hours

Answer Question 1 and 2 other questions. Examiners :

Prof. P.E.O’Donoghue

Mr. T.Corcoran

Dr M.G. Murphy

Q1 continues

Q1[ 40 marks] Please answer each of the following questions.

Q1a[ 8 marks] What is the output from the following Fortran90 program? NOTE: Trace your working so that partial credit may be given for incomplete or incorrect work.

program s2006_q1a //----------------------------------------------- implicit none

integer::i = 4, j = 3, k = 2 character(1):: c = 'a', b = 'c', a = 'b'

write(,'(a)') "-Part 1-"; if ((i < j) .and. (i < k))then write(,'(a)',advance='no') 'A' else if ((c > b) .or. (a > 'a'))then write(,'(a)',advance='no') 'B' else if ((b > c) .and. (k == j-1))then write(,'(a)',advance='no') 'C' else write(,'(a)',advance='no') 'D'; end if write(,'(a)')'X'

write(,'(a)')"--Part 2--" if (k < i)then if (a == b)then write(,'(a)',advance='no') 'E' else write(,'(a)',advance='no')'F' end if end if write(,'(a)') 'Y' write(,'(a)') "---Part 3---" select case (i + j) case (0) write(,'(a)',advance='no') 'G' case (1) write(,'(a)',advance='no') 'H' case default write(,'(a)',advance='no') 'I' end select write(*,'(a)') 'Z' print *

stop's2006_q1a.f90 ends' end program s2006_q1a

Q1 continues

Q1 continues

Q1c[ 8 marks]

What is the output of the following Fortran90 program for the successive inputs 1, 2, 3 , 4, - Variable names have been chosen to hide their real purpose. Deduce the purpose for the code. NOTE: Trace your working so that partial credit may be given for incomplete or incorrect work.

program s2006_q1c //--------------------------------------------------------------------- implicit none

integer::cork, mayo, limerick, ennis, galway

do write(, '(a)', advance='no')'cork = ?, <0 to quit:--->' read, cork if(cork < 0)exit

write(,)'pytagorean triples are --->' write(, 20) 'limerick', 'ennis', 'galway' write(, 30)

do mayo = 1, cork - 1, 1 limerick = cork2 - mayo ennis = 2 * cork * mayo galway = cork2 + mayo write(, 40) limerick, ennis, galway end do write(, 30)

end do 10 format(1x,a) 20 format(1x, t5, a12, t20, a12, t35, a12) 30 format(1x, 45('-')) 40 format(1x, t5, i12, t20, i12, t35, i12)

stop 's2006_q1c.f90 is ending --->' end program s2006_q1c

Q1 continues

Q1 continues

Q1d[ 8 marks]

What is the output of the following Fortran90 program for the inputs 1, 2, 3, 4, 5, 6, 0 Deduce the purpose for the code and suggest what the texts AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH might more usefully be. NOTE: Trace your working so that partial credit may be given for incomplete or incorrect work.

PROGRAM s2006_q1d !------------------------------------------------------------------------ implicit none integer::k integer::r = 0, s = 0 integer::t = 0, u = 0

do write(,'(a)', advance='no')'gimmee an integer(+ve or -ve, 0 to END)' read, k if( k == 0)then exit else if( mod(k,2) == 0)then !even r = r + k t = t + 1 else if( mod(k,2) == 1 )then !odd s = s + k u = u + 1 end if end do

if(t > 0)then print 20, t, r, REAL(r)/t else print*, 'xxxxxx' end if

if(u > 0)then print 30, u, s, REAL(s)/u

else print*, 'zzzzzz' end if

20 format(//' ', t10, i5, " AAA BBB ",& / t20, " with CCC -->> ",i8, & / t20, " and DDD -->> ", f8.3 )

30 format(//' ', t10, i5, " EEE FFF ", & / t20, " with GGG -->> ", i8, & / t20, " and HHH -->> ", f8.3 ) !------------------------------------------------------------------------ stop's200_q1d.f90 ends ...' END PROGRAM s2006_q1d

Q2 ends

Q2[ 30 marks]

A data file F:\costs.d contains information about the costs of equipment purchased for a building project.

Each line in the file contains the product code, the actual cost, the estimated cost for a particular piece. A sample file might be

BX123 122.36 120.

BY234 135.25 140.

BZ345 93.00 100.

BQ456 56.21 50.

BN678 17.20 17.

The quality control engineer would like to analyze this information by calculating the error of each estimate as a

percentage of the estimate and needs a report, called F:\costs.analysis, consisting of four columns

with appropriate headings, the first three being those in the original file and the fourth column containing the percentage error in the estimate. The engineer would also like to get a screen report to spot data that is especially anomalous i.e. where the percentage error is larger than some specified percentage.

Write a program that opens the files, requests the limit for spotting the anomalous errors, reads the data from

F:\costs.data, computes the percentage required, reports to the screen and to the file as specifies e.g. a

program run might be as follows for the data given above

Enter name of data file: d:\costs.data

Enter name of new data file: d:\costs.analysis

enter your spot limit: 5

BZ345 93.00 100.00 -7.

BQ456 56.21 50.00 12.

s2006_q2.f

Press any key to continue

Q3 ends

Q3[ 30 marks]

A hailstone sequence, (n 0 , n 1 , n 2 , n 3 ,.... ) , is defined as follows:

  • The seed, n 0 , is a positive integer.
  • Each successive member, n^ i,^ i^ = 1,^ 2,^ 3,.^.^ .,^ is generated from its predecessor,

n i-1 , according to the following formula:

n i = n i-1 / 2, if n i-1 is even,

however,

n i = 3* n i-1 + 1 if n i-1 is odd.

  • The sequence stops when n (^) i has the value 1.

Write a Fortran90 program which will display the hailstone sequence generated by the positive integer seed called n 0

  • prompts for and reads the seed n 0 and ensures that it is positive
  • prompts for and reads the width i.e. the number of hailstones per row and ensures that it is positive
  • then generates each the hailstone numbers required. and prints them to screen
  • wraps your code in a loop to allow the user to continue processing by entering a '#'. This will allow for easier testing.
  • ensures also that the seed n 0 and the width are positive.

Thus your program might run like this:

a hailstone sequence can be yours

a positive integer as seed: start (> 0 )--->

how many per row?> width (> 0)--->

HAILSTONES

write # to finish --->m

a hailstone sequence can be yours ...

a positive integer as seed: start (> 0 )--->

how many per row?> width (> 0)--->

HAILSTONES

write # to finish --->#

Press any key to continue

Q5 ends

Q5[ 30 marks]Please answer each of the following questions.

Q5a(15 marks)

GCD(a, b) , the Greatest Common Divisor of two positive integers a and b is the largest positive integer that divides both a and b. Euclid’s Algorithm for finding it is as follows:

  • Divide a by b to obtain the integer quotient q and remainder r.[ a = bq + r]. Euclid figured that GCD(a, b) = GCD(b, r). You don't have to check it out here of course.]
  • Now replace a with b, b with r and repeat this procedure, the remainders will decrease and will eventually become 0;
  • The last non-zero remainder is the GCD(a, b). The following is the design, in pseudo-code, of a program to calculate the GCD of pairs of integers using Euclid’s Algorithm: and next to it is a sample output from the program

loop loop prompt for, read, store theA if theA > 0 exit loop end loop

loop prompt for, read, store theB if theB > 0 exit loop end loop

if(0 == theA) theEuclidGCD Í theB else if(0 == theB) theEuclidGCD Í theA else gotTheGCD Í false loop theQuotient Í theA / theB theRemainder Í theA – theB * theQuotient gotTheGCD Í (o == theRemainder) if(gotTheGCD)exit the loop theA Í theB theB Í theRemainder end loop theEuclidGCD Í theB end if display theEuclidGCD prompt for, read, store theDecision if(theDecision = ‘n’ or ‘N’)escape the loop end loop say goodbye

  • Please code the design in Fortran90 to produce the output shown.
  • Please trace the workings of the program to produce the results shown in this output i.e. just show the changes in the

program variables as the code proceeds.

Q5b(15 marks)

Using pseudo-code, design a program that reads the coordinates of three points, and then determines whether they form a right-angle triangle and continues this work until the user decides to quit

the first --> 9 the second --> 27

their GCD is 9

thank you Euclid

try again? YyNn --> y

the first --> 1260 the second --> 198

their GCD is 18

thank you Euclid

try again? YyNn --> n s2006_q5a.f90 is ending Press any key to continue

algorithm

sample output

Simple Quadratures

Simple Quadratures

QUADRATURE: approximating Definite Integrals

Let the interval (a, b) be divided into n internals of width h by the points

n −

x x x x

Let

x a x b

n

Let

m 1 , m 2 , m 3 ,..., m n

be the mid-points of the intervals ending at

x 1 , x 2 , x 3 ,..., x n

Three simple quadratures that approximate

b

x a

f ( x ) dx

are

Mid-Point Rule:

n

k

k

b

x a

f x dx h f m

Trapezoidal Rule

=

=

1

1

n

k

k

b

x a

f x

f a f b

f x dx h

Simpson’s Rule

=

=

=

1

1 ( 2 )

2

2 ( 2 )

n

k

k

n

k

k

b

x a

f a f b f x f x

h

f x dx

None of these can hold a candle to Gaussian Quadrature but they are useful to play

with

a= x 0 x 1 x 2 x 3 x 4 b= xn

m 1 m 2 m 3 m 4 m 5 mn

xn-