Computers in Engineering 308-208B 1996

YOUR LAST NAME _____________________________

YOUR FIRST NAME ____________________________

YOUR MUSICB CODE HQ_________________________

YOUR STUDENT NUMBER ________________________

YOUR ENGINEERING DEPARTMENT_________________

McGill University

Faculty of Engineering

COMPUTERS in ENGINEERING

308-208B

Final Examination


Examiners:

Prof. G. Ratzer Date: Thursday 18 April 1996.

S. Spiller Time: 9.00 - 12.00 noon

This examination consists of 18 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 10

Question 18 10

Total 50 marks

Answer questions 1 to 16 on the mark sense sheet provided using an HB pencil. Select the BEST answer from the five given.

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

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

Do rough work on the backs of pages.

Faculty CALCULATORS are allowed - none other.

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:

program question1

implicit none

!

integer :: I,J,K

real :: X,Y,Z, P, Q, R

!

I = 2

J = 4

X = 22.5

Y = -10.0

Z = 0.25

P = Z + X * Y

Q = P + 2.0 * Y ** I

R = -Y * 8.0 / (J*1.0)

X = Q + R

Y = X * 2.0 - Z

Z = Z * R

J = 3.0 * Z / (I*1.0)

I = I + 6

PRINT *,I,J,X,Y,Z,P,Q,R

stop

end program question1

What is the output of the above FORTRAN 90 program:

1) 8 7 -4.75 -9.75 5.0 -224.75 -24.75 20.0

2) 6 7.5 -4.75 -9.75 5 -224.75 -24.75 20.0

3) 8 7 -4.75 -9.75 5.0 -224.75 -24 20

4) 8 7 -4.75 -9.75 5.0 -224.75 -24 20.0

5) 8 7 -4.75 -9.75 5 -224.75 -24.75 20.0



QUESTION 2:

What is the output of this FORTRAN 90 program if :

x(1) = 2. x(2) = 3. x(3) = 4. x(4) = 5. x(5) = 6.

y(1) = 1 y(2) = 3 y(3) = 5 y(4) = 3 y(5) = 2

program question2

implicit none

integer :: i, y(5)

real :: a(5),x(5)

do i=1, 5

a(i) = x(y(i))

end do

print *, (a(i),i=1,5)

do i=1, 5

a(y(i)) = x(i)

end do

print *, (a(i),i=1,5)

stop

end program question2


1) 2. 4. 6. 4. 3.

2. 6. 5. 4. 4.

2) 2. 4. 6. 4. 3.

2. 6. 3. 4. 4.

3) 2. 4. 6. 4. 3.

2. 6. 5. 3. 4.

4) 5. 5. 5. 5. 5.

2. 6. 5. 4. 4.

5) 2. 4. 6. 4. 3.

3. 3. 3. 3. 3.


QUESTION 3.


program question3

implicit none

integer :: i, nterm

real :: n,term, s

!

read *, n

read *, nterm

s = 1

term = 1

do i=1, nterm

term = term * n / (i*1.0)

s = s + term

end do

print *, s

stop

end program question3

The program calculates (where ! is the symbol for FACTORIAL):

1) 1 + n/1! + n**2/2! + n**3/3! + n**4/4! + .....

2) 1 + n/1 + n**2/2 + n**3/3 + n**4/4 + ....

3) 1 + n/1! + n/2! + n/3! + n/4! + ....

4) n**n/n!

5) None of the above

QUESTION 4.

PROGRAM QUESTION4

IMPLICIT NONE

INTEGER :: I, J, K

INTEGER :: M1(10), M2(10), M3(10)

READ *,(M1(I),I=1,5)

READ *,(M2(I),I=1,5)

READ *,(M3(I),I=1,5)

DO I=1,5

IF( (I < 2) .AND. (M1(I) < M2(I))) THEN

PRINT * , M1(I)

ELSE IF ( (M1(I) == M2(I)) .OR. (M1(I) == M3(I))) THEN

PRINT * , M2(I)

ELSE IF (I == 5) THEN

PRINT *, M3(I)

END IF

PRINT *, 's'

END DO

STOP

END PROGRAM QUESTION4

If the input data is:-

2 4 0 7 7

3 1 0 0 7

2 4 1 1 0

The output of the above program (shown on one line) with the input data given is:

1) 2 s 1 s 0 s s 7 s

2) 2 s 1 s 0 7 s 0 s

3) 2 s 1 s 1 s s 0 s

4) s s 1 s 1 s s 0

5) s s 1 s 0 s s 0 s



QUESTION 5.

program question5

implicit none

integer :: i, j, tmp

real :: a(7)

read * ,(a(i),i=1,7)

do i = 2,6

do j = i, 2, -1

if (a(j - 1) > a(j)) then

tmp = a(j - 1)

a(j - 1) = a(j)

a(j) = tmp

end if

end do

end do

print * ,(a(i),i=1,7)

stop

end program question5

If the input data is:

2. 3. 6. 5. 4. 9. 1.

What will the above program produce as output?

1) 1. 2. 3. 4. 5. 6. 9.

2) 2. 3. 4. 5. 6. 9. 1.

3) 9. 6. 5. 4. 3. 2. 1.

4) 6. 5. 4. 3. 2. 1. 9.

5) 1. 9. 2. 6. 3. 5. 4.


QUESTION 6.

What does the following program print and what are these numbers called?

#include <stdio.h>

#define LIMIT 7

int main(void)

{

long f0=0, f1=1, n, temp;

printf("%ld %ld ",0,1);

for (n= 2; n <= LIMIT; ++n) {

temp = f1;

f1 += f0;

f0 = temp;

printf("%ld ",f1);

}

return 0;

}


1) 1 2 3 5 8 13 21 34 (Perfect Numbers)

2) 0 1 1 2 3 5 8 13 (Fibonnaci Numbers)

3) 2 3 5 7 11 13 17 19 (Prime Numbers)

4) 1 3 5 7 9 11 13 15 (Odd Numbers)

5) None of the above.

QUESTION 7

What will the following program print?

#include <stdio.h>

int main(void)

{

int i = 0, test=1;

while(i++ <= 3.9)

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

return 0;

}

1) 3 9 27 81 243

2) 3 6 12 24

3) 3 9 27 81

4) 3 9 81 243

5) None of the above


QUESTION 8

In the following routine sort, a particular sorting method is done to sort the elements. The following routine sort calls the routine temp in order to complete the sorting.


void sort(int a[], int n)

{

int i,j;

void temp(int *, int*);

for (i = 0;i < n-1; ++i)

for (j = n-1;j > i; --j);

if (a[j-1] > a[j])

temp(&a[j-1], &a[j]);

}

void temp(int *p, int *q)

{

int tmp;

tmp = *p;

*p = *q;

*q = tmp;

}

If the array int a[] = {7, 3, 66, 3, -5, 22, -77, 2}, the elements after the first pass and the sorting method employed here are:

1) Bubble sort ( -77, 7, 66, 3, 3, -5, 22, 2)

2) Bubble sort ( -77, 7, 3, 66, 3, -5, 22, 2)

3) Shell sort ( -77, -5, 7, 3, 66, 3, 22, 2)

4) Selection sort ( -77, -5, 2, ,7, 3, 66, 3, 22)

5) None of the above

.

QUESTION 9

#include<stdio.h>

#define MAX 5

void operate(s)

int s[MAX];

{ int i,j,k;

for (i=0, j = MAX-1; i < j; i++ , j--){

k = s[i]; s[i] = s[j]; s[j] = k; } }

main(){

int i;

int s[MAX];

s[0] = 5;

s[1] = 3;

s[2] = s[0] - s[1];

s[3] = 4;

s[4] = s[1] - s[2];

for (i=0;i<MAX-2;i++)

if (s[i] > s[i+1]) continue;

else s[i] = s[i-1]+ 2;

operate(s);

printf("%d %d %d %d %d",

s[0],s[1],s[2],s[3],s[4]); }

The output of above program would be

1) 1 2 3 4 5

2) 1 4 5 3 5

3) 5 3 5 4 1

4) 5 4 3 2 1

5) None of the above.

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) HITHERE!!BONJOUR!!!!!EREHTIH!!!RUOJNOB

2) HITHERE!!BONJOUR!!!!!!RUOJNOB!!EREHTIH

3) HITHERE!!BONJOUR!!!BONJOUR!!!HITHERE!!

4) HITHERE!!BONJOUR!!!HITHERE!!BONJOUR!!!

5) 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

5) 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.

Select the "best" answer.

1) a, c and d

2) a, b and c

3) b, c and d

4) a, b and d

5) None of the above

QUESTION 15.

Consider the following statements:

a) The Runge-Kutta method to estimate a differential equation is less accurate than Euler's method.

b) This pseudo-code implements the Euler algorithm:

x <-- 0

y <-- y0

while ( x <= xf )

y <-- h*y + f(x)

x <-- x + h

c) Given the differential equation y' = (0.25)*y + x + 7 on the interval [0, 0.4], and 2.478 is the value of y at x=0.2 using Euler's method with h=0.1 and initial value of y=1 (i.e. y(0) = 1 ).

Which of the above statement(s) is(are) false?

1) b only

2) a and b

3) a and c

4) a,b and c

5) a only

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.

You are given a

matrix(n,n).

Write a FORTRAN program that will fill the complete matrix with n-squared stars ("*"s) and display the following shapes according to user's entries

<Comments like this do NOT have to be printed>:

a) Draw a triangle in increasing order (base in the bottom - lower triangular section).

b) Draw a triangle in decreasing order (base on top - upper triangular section).

c) Draw an horizontal line - top row of elements

d) Draw a vertical line - column 1 elements

e) Draw a diagonal line - Take elements from the diagonal

e.g. For n=7 and the user input of a b c d e

* * * * * * * * < Here the user typed in a and then b >

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * <horizontal line - from row 1>

* <vertical line - elements from column 1>

*

*

*

*

*

*

* <elements from the diagonal - printed on the diagonal>

*

*

*

*

*

*

Write your FORTRAN program below here and on the back of the page. Make your program as generic as possible.

QUESTION 18.

Write a "C" procedure

Quad(a,b,n,pointm,trap,simp)

which uses a single loop to find the Midpoint (pointm), the trapezoidal (trap), and Simpson's (simp) rules approximations to the integral of a function f(X) (supplied) from a to b using intervals.

Note you only have to write the procedure, not the main or function f. Try to minimize the number of function evaluations.

Write your program neatly below here: