LAST Name ________________________

First Name _______________________

MUSIC Code HC______

McGill University

Computers in Engineering

308-208A

Second Mid-term

Tuesday November 4th, 1997

2.30 - 3.50 p.m.

Faculty CALCULATORS are allowed. All the C programs below have been tested with Turbo C. They all compile and execute without errors. This is a multiple-choice exam to be answered using the red and white sheets for questions 1 to 5. Select what you consider to be the BEST answer of the five answers provided for each question.

If you have time at the end of this test please feel free to comment on the course content, the text, Website, diskette, facilities, the instructor, TAs and how things could be improved.

Question 1. (12 minutes, 15 points)

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

#include <stdio.h>

#define LIMIT 33

int main(void)

{

long f0=0, f1=1, temp;

printf("%ld %ld ",f0,f1);

for (;(f1 <= LIMIT) && (temp = f1) && (f1 += f0) &&
(f0 = temp);)

printf("%ld ",f1);

return 0;

}

Type of numbers below - not printed

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

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

3) 0 1 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 2: (12 minutes, 15 points)

What will the following program print?

#include <stdio.h>

int main(void)

{

long i= 0, test= 1;

while(i++ <= 3)

printf("%7ld",test *= 2 + test);

return 0; }

1) 3 15 255 65535

2) 3 9 27

3) 3 9 27 81

4) 3 15 255

5) None of the above

Question 3: (12 minutes, 15 points)

#include <stdio.h>

main()

{

int c;

c=3;

if (c=4) c++;

else c--;

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

}

What will the above program output?

1) 2

2) 3

3) 4

4) 5

5) None of the above

 

Question 4: (11 minutes, 15 points)

#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 5 (12 minutes 15 points)

What is the FIRST line of output from the program that follows?

#include<stdio.h>

void exchange(int *int1, int *int2);

void main (void){

int m[] = {48, -98, 42, 2, 57, -9, 0, -1, 21, 3};

int i,j,k,element;

for(i=0;i<9;i++){

k = i;

element = m[i];

for(j=i+1;j<10;j++){

if(m[j] < element){

element = m[j];

k = j;

}

}

exchange(&m[i],&m[k]);

printf("\n%d %d %d %d %d %d %d %d %d %d",

m[0],m[1],m[2],m[3],m[4],m[5],m[6],m[7],m[8],m[9]);

}

}

void exchange(int *int1, int *int2){

int int3;

int3 = *int1;

*int1 = *int2;

*int2 = int3;

}

1) -98 42 2 48 -9 0 -1 21 3 57

2) -98 48 42 2 57 -9 0 -1 21 3

3) -98 -9 42 2 57 48 0 -1 21 3

4) -98 48 42 2 57 -9 0 -1 21 3

5) None of the above

Question 6. (23 minutes, 25 points)

Write a simple "C" program to accept data in the following format:

Columns Description Example

1-7 Student ID 9712345

11-35 Student Name Roy, Jean

41-45 Total Course Assignments (100) 78

46-50 Mid Terms also out of 100 67

51-55 Final exam out of 100 73

Your program should read a group of records with the above data, terminated by 9999999. For each input record, print the input data, under appropriate headings, and calculate and print the final course mark for each student, all on the same line. The course mark is calculated by counting the assignments for 20%, the midterms for 30%, and the final exam for 50%. The course mark should be printed to one decimal place for each student.

At the end of the class list, print the Student ID and Name of the TOP THREE students in the class. (5 BONUS marks for this part!!)

Your program does NOT need to use 2-D arrays nor function subprograms.

Use the back of pages to write your program.