Docsity

Midterm Exam with Answers - Programming and Problem Solving | ECS 030, Exams of Computer Science

Toggle sidebar
bg1
ECS 30 Midterm
WInter 2009
Name:
SID:
Rules:
•You must do your own work.
•No books or notes allowed except for the one page emailed to you.
•You may not use phones, calculators, laptops or any other computing device. Embedded computers
(defibrilators, automatic transmissions, watches, anti-tank missiles ) OK.
Suggestions:
•When in doubt, state any assumptions you make in solving a problem.
•Read the questions carefully. Be sure to answer all parts.
•Clearly identify your answers!
•“Brevity is the soul of wit” Long-haired British Dude, Hamlet II, ii
•Problems are not necessarily in the order of difficulty.
•Grab the low lying fruit: make sure you solve all the problems whose answers are easy or obvious to
you before tackling the harder problems.
Good Luck!
Problem Possible Score
1 10
2 10
3 10
4 4
5 10
6 15
Total 59
pf3
pf4
pf5

Partial preview of the text

Download Midterm Exam with Answers - Programming and Problem Solving | ECS 030 and more Exams Computer Science in PDF only on Docsity!

ECS 30 Midterm

WInter 2009

Name:

SID:

Rules:

  • You must do your own work.
  • No books or notes allowed except for the one page emailed to you.
  • You may not use phones, calculators, laptops or any other computing device. Embedded computers (defibrilators, automatic transmissions, watches, anti-tank missiles ) OK.

Suggestions:

  • When in doubt, state any assumptions you make in solving a problem.
  • Read the questions carefully. Be sure to answer all parts.
  • Clearly identify your answers!
  • “Brevity is the soul of wit” Long-haired British Dude, Hamlet II, ii
  • Problems are not necessarily in the order of difficulty.
  • Grab the low lying fruit: make sure you solve all the problems whose answers are easy or obvious to you before tackling the harder problems.

Good Luck!

Problem Possible Score 1 10 2 10 3 10 4 4 5 10 6 15 Total 59

  1. (10 points) Consider the following code. What is the output?

#include <stdio.h> void func (int *p, int *q) { int temp = *p; *p = *q; *q = temp; }

int main(){ int p, q; int arr[10]; for (p = 0; p < 10; p++) arr[p] = p * p; p =5; q = 6; func(&p, &q); func(&arr[p], &arr[q]); printf (" %d squared is %d, %d squared is %d\n", p, arr[p], q, arr[q]); return 0; }

Answer: Correct answer is ‘‘6 squared in 25, 5 squared is 36’’.

“5 squared in 25, 6 squared is 36” (or flipped) gets 3 points. All other answers, no points

  1. (10 points) All 1 point, except as noted. Check true or false, fill in blank, or otherwise answer as instructed:

A. In C, if you want to return more than one value from a function you have to use Answer: pointers

B. (T or F) If a is an int variable, then *(&a) is always the same as a; Answer: T

C. (T or F) If a is an int * variable, then &(*a) is always just the same as a; Answer: Should be F; Because of unexpected behaviour in some compilers we accept either answer D. (T or F) If iarr is an int array of size 5, the expression iarr[5] refers to the last element in the array. Answer: False

E. (3 points, fill in values) Assuming the matrix, declaration int imat[5][7] what are i, j for the corners of the matrix in the expression imat[i][j]? where upper left is given by i = 0, j = 0: Lower right? i = , j = , Lower left? i = , j = Upper right? i = , j = , Answer: (4,6), (0,6) and (4,0), but accepted also (6,4), (6,0) and (0,4)

F. (cross out what is not applicable) If the array iarr is as described above, then the statement iarr[10] = 0 will result in a) possible Run-time error b) compile-time error with gcc -Wall c) no warnings. Answer: Cross out part b

G. (fill in blanks, 2 points. ) When passing in a function parameter to a higher-order function, the parameter type declaration must declare type and the type of the to the function. Answer: return type and parameter types

myarr[j] = 0;

printf("First %s\n", myarr); printf("Second %s\n", &myarr[j-5]);

}

(a) (5 points) What output does peculiar(10) produce? Answer: First ACEGIKMOQS Second KMOQS

Partial Credit: First character correct in each string, worth one point. Length correct for each string worth one point. Interval, sequencing, should be A-C-E etc. worth 2 points for all of question 5. (b) (5 points) What output does pecular(6) produce? Answer: First ACEGIK Second CEGIK

  1. (15 points) Matrix normalization Consider the following program, which fills a 10x10 matrix with ran- dom numbers, and then normalizes this matrix, by dividing each element by the sum of all the elements, so that sum of all the elements of the normalized matrix is 1.0, (except for round-off errors) Fill in the missing details of this program.

#include <stdio.h> #include <math.h>

#define MATSIZE 10

float matsum( ......) { /* Part a, complete the formal parameter declaration */

int i, j; float tot = 0; /* Part b, complete the body of this function */

return tot;

int main () {

float mymatrix ..... /* part c, complete declaration of matrix */ float normval; int i,j; for (i=0; i< 10; i++) for (j=0; j< 10; j++) mymatrix[i][j] = random();

normval = matsum (mymatrix); /* Computes Sum of all the matrix elements */

printf("\nThe normalizeation constant %.3f\n", normval);

/* Part d, complete the loop (s) for the normalization step of normalizing each entry of mymatri

normval = 0;

for (i=0; i< 10; i++) for (j=0; j< 10; j++) normval += mymatrix[i][j] ; /* Check the sum of the normalized matrix */

printf("\nThe normalized sum (should be 1) %.3f\n", normval);

};

Fill in the parts here:

(a) (2 points) Answer: float mymatrix[][MATSIZE] or float mymatrix[MATSIZE][MATSIZE]

float **mymatrix, or float mymatrix[][] gets one point. Additional/wrong declarations penalized (^12) point (b) (5 points) Answer: for (i = 0; i < MATSIZE; i++) for (j = 0; j < MATSIZE; j++) tot +=mymatrix[i][j];

Two points if both nested loops; 1 point more if some attempt at proper (but incorrect) addition in loop body. (c) (3 points) Answer: float mymatrix[MATSIZE][MATSIZE]

two points if 2-dimensional array (or ptr to ptr) declaration, but with wrong dimensions in some way (d) (5 points) Answer: for (i = 0; i < MATSIZE; i++) for (j = 0; j < MATSIZE; j++) mymatrix[i][j] = mymatrix[i][j] /normval; Two points if both nested loops; 1 point more if some attempt at proper (but incorrect) division in loop body.