Computers in Engineering 308-208A 1995

McGill University

Faculty of Engineering

COMPUTERS in ENGINEERING

308-208A

Final Examination

Examiners:

Prof. G. Ratzer Date: Monday 18 December 1995.

S. Spiller Time: 9.00 - 12.00 noon

This examination consists of 19 questions and is worth 50% (or 70% - depending on your mid-term) of the final course grade.

Questions 1 - 15 multiple choice 30 marks

Questions 16 0 (Yes, zero!)

Questions 17 6

Question 18 7

Question 19 7

Total 50 marks

Answer questions 1 to 16 on the IBM sheet provided using an HB pencil.

Select the BEST answer from the five given.

REMEMBER to put your student number on the IBM sheets both as numeric digits and by filling in the circles below each digit.

Answer questions 17, 18 and 19 in the space provided on this paper.

Do rough work on the backs of pages.

No CALCULATORS allowed.

Use space below on this page to bring any apparent ambiguities to the notice of the examiners.

MAKE SURE YOU WRITE YOUR NAME AND STUDENT NUMBER ON BOTH THE

COMPUTER SCORE SHEET AND THIS EXAMINATION PAPER.

QUESTION 1:

What is the output of this FORTRAN 90 program:

program question1

implicit none

!

integer :: I,J,K

real :: A,B,C

!

A = 2.1E1 + 4

B = 37/7

C = 37.0/7.0

I = 2.2 + MOD(78,5)

J = 89/4*4+2-1*2

K = C

PRINT *,I,J,K,A,B,C

!

stop

end program question1

1) 6 5 5.0000 25.0000 5 5

2) 5 88 5 25.0000 5.00000 5.28571

3) 6 5 5.0000 25.0000 5 5.28571

4) 5 5 5 6.10000 5.00000 5.28571

5) 6 88 5 25.0000 5.00000 5.28571

QUESTION 2:

What is the output of this FORTRAN 90 program:

program question2

implicit none

!

integer :: i,j,k

real :: a,b,c,d

!

a = 1384E+03

b = 1384E03

c = 1384.

d = 1384E-03

i = 1384.

j = 1384+3

k = 138.4E01

PRINT *,i,j,k,a,b,c,d

stop

end program question2

1) 1384 1387 138 1.38400E+06 1.38400E+06 1384.00 1.38400

2) 1384.00 1387.00 13840.00 1.38400E+06 1.38400E+03 1384.00 1.38400

3) 1384 1387 1384 1.38400E+06 1.38400E+06 1384.00 1.38400

4) 1384 1387 138 1.38400E+06 1.38400E+03 1384.00 1.38400

5) 1384.00 1387.00 138.00 1.38400E+06 1.38400E+06 1384.00 1.38400

QUESTION 3.

program question3

implicit none

!

integer :: i, j(1000), k(1000), n

integer :: x, y

!

do i=1, n ! Input data is:-

end do ! 5

! 3

do i=1, n ! 2

end do ! 4

! 11

do i=1, n-2 ! 12

x=k(j(i)) ! 13

y=k(j(i+1)) ! 8

print *, (n-i), (y-x) ! 9

end do

stop

end program question3

The output of the above program is:

1) 2) 3) 4) 5)

4 4 4 -4 2 -4 2 -4 4 -1

3 -1 3 1 3 1 3 -1 3 -1

2 -1 2 1 4 1 4 -1 2 -4

QUESTION 4.

PROGRAM QUESTION4

IMPLICIT NONE ! Input data is:-

! ! 12.0 11.0 0.0 7.5

INTEGER :: I, J, K ! 10.0 1.0 2.50 0.0

REAL :: M1(3,4), M2(4,1), M3(3,2) ! 1.0 0.0 25.0 0.0

! ! 2.0

DO I=1,3 ! 5.0

END DO ! 0.0

10 FORMAT(8(F5.2))

!

DO I=1,4

END DO

M3=0.0

DO K=1,1

DO I=1,4

DO J=1,3

M3(J,K) = M3(J,K) + M1(J,I) * M2(I,K)

END DO

END DO

END DO

DO I=3,1,-1

PRINT *, M3(I,1)

END DO

STOP

END PROGRAM QUESTION4

The output of the above program is:

1) 2) 3) 4) 5)

7.00000 7 79.0000 7.50000 7.10000

27.0000 27 27.5000 27.1000 27.5000

1. 79 7.10000 78.0000 79.0000 78.1

QUESTION 5.

program question5

implicit none

!

integer :: I, J

!

do I=29, 18, -7

do J=5, 1, -2

if (mod(I*3,J*2) < (I+1)/4) then

if (mod(I,2) >= mod(J*7,2)) then

print *, 'NHL'

else

print *, 'SUPERBALL'

end if

else

print *, 'NBA'

end if

end do

end do

!

stop

end program question5

What will the above program produce as output?

1) 2) 3) 4) 5)

NBA NHL NBA NBA NBA

NHL NHL NHL NBA NHL

NHL NHL NBA NBA NHL

NBA NHL SUPERBALL NHL SUPERBALL

SUPERBALL SUPERBALL NHL NBA NHL

SUPERBALL NHL NBA NBA NHL

QUESTION 6.

#include <stdio.h>

main()

{

int c;

c=3;

if (c=4)

c++;

else

c--;

printf("%d\\n",c);

}

What will the above program ouput?

1) 2

2) 4

3) 3

4) 6

1. 5

QUESTION 7

#include <stdio.h>

#include <math.h>

main()

{

float a , b ;

int c ,d ;

a = 3.0 ;

b = 2.0 ;

c = 8 ;

d = 6 ;

if ( a > b) d/=a ; else d/=b ;

c -= c - d * d;

if ( c > d*d ) a-=c ; else a+=d ;

b += a*a*1/5 - b ;

printf (" %1f , %1f , %d , %d\\n",a,b,c,d);

}

What will this program print ?

1) 5 , 5 , 4 , 2

2) 6 , 7.2, 9 , 3

3) -1, 0.2, 4, 2

4) 5, 25, 4, 8

1. None of the above

QUESTION 8

#include <stdio.h>

int t = 0;

void funny ( int x, int y , int *z );

main()

{

int i;

int j;

int *k ;

i = 4.0 ;

j = ++i ;

*k = i++ ;

funny(i,j,k);

funny(*k,j,&i);

printf("%d , %d , %d , %d\\n",i,j,*k,t);

}

void funny ( int x, int y, int *z )

{

t += x ;

{ int t ;

t = x ;

x = y - *z ;

*z = t - y ;

}

}

What will this program print ?

1) -4 , 3 , -1, 0

2) 5 , 6 , 0 , 0

3) 3 , -3 , 5 , 7

4) -4 , 5 , 1 , 7

1. None of the above

QUESTION 9

#include <stdio.h>

int main(void)

{

int i=0,test=1;

while(i++ <= 4.9)

printf("%6d",test *= 2);

printf("\\n");

return 0;

}

What will the above program output?

1) 2 4 6 8

2) 2 4 6 10 16

3) 2 4 6 8 10

4) 2 4 8 16

1. 2 4 8 16 32

QUESTION 10

#include <stdio.h>

#define SIZE 10

void DeathHand ( char *, char *); /* prototype for DeathHand */

main()

{

char String1[SIZE+1] = "HI THERE!!" ;

char String2[SIZE+1] = "BONJOUR!!!" ;

printf("%s %s",String1,String2);

DeathHand(String1,String2);

printf("%s %s\\n",String1,String2);

}

void DeathHand ( char *first, char *second)

{

char Temp[SIZE+1] ;

int i ;

for (i=0 ; i <= SIZE ; i++)

Temp[i] = first[i] ;

for (i=0 ; i < SIZE ; i++)

first[i] = second[SIZE-i-1];

first[SIZE]=0 ;

for (i=0 ; i < SIZE ; i++)

second[i] = Temp[SIZE-i-1];

second[SIZE]= 0;

}

What will this program Print ?

1) HI THERE!!BONJOUR!!!!!EREHT IH!!!RUOJNOB

2) HI THERE!!BONJOUR!!!!!!RUOJNOB!!EREHT IH

3) HI THERE!!BONJOUR!!!BONJOUR!!!HI THERE!!

4) HI THERE!!BONJOUR!!!HI THERE!!BONJOUR!!!

1. None of the above

QUESTION 11.

Using the Newton-Raphson method to find the approximation to the root of a function F(x), if the initial value of x is -4 and F(x) = 3(x*x*x)+10(x*x)-96, what is the value of x after 1 iteration?

1) x = -10

2) x = -128

3) x = -2

4) x = -3

1. None of the above.

QUESTION 12.

Using Selection Sort to sort the following array in ascending order:

25 57 48 37 12 92 86 33

After two passes the array would be:

1) 25 57 33 37 12 92 86 48

2) 25 12 33 92 37 48 86 57

3) 12 25 48 37 57 92 86 33

4) 12 25 33 37 48 57 86 92

5) None of the above

.

QUESTION 13.

Which of the following triangularized matrices represent a

solution of this system of linear equations?

4 x + 2 y + 5 z = 13

3 x - 5 y + 2 z = -5

9 x + y - 4 z = 7

1) 1 0 0 2

0 1 0 0

0 0 1 1

2) 1 0 1 1

0 1 0 2

0 0 1 1

3) 1 0 0 3

0 1 0 4

0 0 1 3

4) 1 0 0 1

0 1 0 2

0 0 1 1

5) 1 0 0 1

0 1 1 2

0 0 1 1

QUESTION 14.

Which of the following Statements are true.

a) The trapezoidal rule uses n+1 evaluations of f(x) where n is the

number of panels used in the approximation

b) The pseudocode evaluates the integral from XMin to XMax using the

midpoint rule

PanelWidth = (XMax - XMin) / NumberPanels

Sum = (F(XMin) + F(XMax))/2.0

X = X_Min + PanelWidth

For k = 1 , NumberPanels-1

Sum = Sum + F(x)

x = x + h

end of for loop

Estimate = PanelWidth*Sum

c) Simpson's rule is usually a much better approximation that the Trapezoidal and Midpoint rule.

d) For a convex f(x) (i.e where f''(x) < 0) the midpoint rule overestimates the integral and the trapezoidal rule underestimates it.

1) a, c and d

2) a, b and c

3) b, c and d

4) a, b and d

1. None of the above

QUESTION 15.

If we approximate the integral of the function f(x) = x*x - x + 1 from 0 to 2 with h=0.5 to be equal to 2.75, which numerical integration approximation was used?

1) Midpoint rule

2) Left endpoint rule

3) Right endpoint rule

4) Trapezoidal rule

1. Simpson's rule

QUESTION 16.

On your IBM computer marked sheet fill in circle ONE = 1

(This is to prove that you are still awake and got this far in the exam!!)

QUESTION 17.

Consider the following method which finds the maximum and minimum of a group of N numbers, located in N slots (like an N element array).

Partition the N numbers into N/2 adjacent pairs.

Compare the two numbers in each pair (which will be in an odd/even slot) with each other.

Perform a swap, if needed, so that the smaller one is in the odd slot.

Find the maximum of the N/2 numbers (in the even slots) which turned out to be larger.

Find the minimum of the N/2 numbers (in the odd slots) that turned out to be smaller.

An example with N=6 would be starting with --> 9 7 8 3 4 6

Step 1. 7 9 3 8 4 6 Swap adjacent pairs if needed.

Step 2. Minimum of ODD slots is 3

Step 3. Maximum of EVEN slots is 9

Assume that n is an even number for simplicity.

a) What is the estimated running time of this algorithm, in general ?

b) Outline a sorting algorithm that uses the above idea to sort a list of n numbers by continuously finding the maximum and minimum. You may use C or Fortran or PseudoCode.

c) Find the running time of your algorithm. Compare it to another sorting mechanism (use Insertion sort or Selection sort). Does your algorithm outperform these algorithms ?

1. Express the running time of your algorithm in terms of Big-O notation. Is your algorithm considered an advanced sorting method or an elementary sorting method ? Explain your answer.

QUESTION 18.

The Gaussian error function, Erf(y), is defined by the integral from x = 0 to x = y of the expression Exp(-x**2)*2/Sqrt(Pi).

STEP A: State the formula for the Trapezoidal Rule for numerical integration of a function f(x) in terms of lower and upper limits a and b, number of panels n, and step size h.

STEP B: Write a C function to evaluate the integrand for Erf. That is, a function F(x) which encodes the above expression for a value of x accepted as an input parameter.

STEP C: Write a general purpose C function that uses the Trapezoidal Rule to estimate the integral of f(x) between a and b. Your function should accept as parameters values for a, b, and n, and return the area.

STEP D: Write a C main program which reads input data for Y and tolerance Delta.

The program must then compute Erf(Y) by calling the Trapezoidal Rule function with n = 2, 4, 8, 16,... until two successive estimates of the have an absolute difference less than Delta.

Finally, the program must print out values for Y, Erf(Y), and a count of the number of times the Trapezoidal Rule function was called.

QUESTION 19.

Write a FORTRAN 90 program to find a root of a given polynomial using the Newton-Raphson Method.

The data to be read in will be the coefficients of a polynomial of degree of at most 5, i.e. 6 real coefficients will always be given. For example the polynomial f(x) = 3 + 2x -3x**2 + x**5 would be read as 3.0 2.0 -3.0 0.0 0.0 1.0.

You also have to read the limits a,b of the interval in which you try to find a root together with the accuracy of the computation.

Given the polynomial function, you have to evaluate one of its root with 5 different initial guesses of x for the root. You should cover the whole interval [a,b] with the initial guesses.

Your output should look like this:

Initial guess Root f(Root)

------------------------------------------