0% found this document useful (0 votes)
36 views5 pages

University of Toronto Faculty of Applied Science and Engineering Aps106 Midterm Ii - March 27, 2014

The document appears to be a midterm exam for a course at the University of Toronto. It contains instructions for students to provide their name and signature, and circle their instructor's name. The exam is divided into 4 questions worth a total of 20 marks. Question 1 has two parts worth 4 marks total, asking students to identify outputs of C code snippets. Question 2 has one part worth 4 marks, asking students to identify and correct 4 errors in a C code snippet. Question 3 is worth 6 marks and asks students to write a function to set adjacent cells to 1 for a specified element in a rectangular array, considering different cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views5 pages

University of Toronto Faculty of Applied Science and Engineering Aps106 Midterm Ii - March 27, 2014

The document appears to be a midterm exam for a course at the University of Toronto. It contains instructions for students to provide their name and signature, and circle their instructor's name. The exam is divided into 4 questions worth a total of 20 marks. Question 1 has two parts worth 4 marks total, asking students to identify outputs of C code snippets. Question 2 has one part worth 4 marks, asking students to identify and correct 4 errors in a C code snippet. Question 3 is worth 6 marks and asks students to write a function to set adjacent cells to 1 for a specified element in a rectangular array, considering different cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

UNIVERSITY

 OF  TORONTO  
FACULTY  OF  APPLIED  SCIENCE  AND  ENGINEERING  
APS106  MIDTERM  II  –  March  27,  2014  
 
 
INSTRUCTORS:      Vijay  Sinnathurai,    L.  Shu,    Siva  Srikukenthiran,    M.  Bussmann  

 
                                   circle  the  name  of  your  instructor  whose  lectures  you  attend  
 
 
Last  Name:                      
 
First  Name:                    
 
Signature:                  
 
Number:                        
 
 
DO  ALL  WORK  IN  THIS  BOOKLET.    DO  NOT  REMOVE  THE  STAPLE.  
 
 
Important:    Marks  will  be  awarded  for  correctness  of  your  algorithm,  C  syntax,  adherence  to  
recommended  C  coding  practice,  code  efficiency,  and  the  clarity  of  your  program.    No  calculator  
allowed.    No  textbook  allowed.    
 
 
Time  allotted:    60  min  
 
 
Question   Maximum  Mark   Actual  Mark  
1   4    
2   4    
3   6    
4   6    
Total   20    

Page  1  of  5  
Question  1.    (4  marks)    
 
(a  -­‐  2marks)    :  What’s  the  output  of  the  following  ?  
 
void  mat_op  (int  A[][4],  int  b[],  int  c[])  {  
  int  i,j;  
  for(i=0;  i<2;  i++)  {  
    c[i]  =  0;  
    for(j=0;  j<4;  j++)  {  
      c[i]  +=  A[i][j]*b[j];  
    }  
  }  
}  
 
int  main()  {  
  int  M[2][4]  =  {{1,2,3,4},  {5,6,7,8}};  
  int  v1[]  =  {2,1,2,0};    
  int  v2[2];  
  mat_op(M,  v1,  v2);  
  printf  ("%d  %d",  v2[0],  v2[1]);  
  return  0;    
}  

 
Answer:      10 30  
 
 

(b  -­‐  2marks)    :    What  is  the  output:  


 
void  Fun(int  x,  int  y,  int  *z,  int  *w)  {  
  int  temp;  
  temp  =  x;  
  x  =  y;  
  y  =  *z;  
  *z  =  *w;  
  *w  =  temp;  
}  
 
int  main()  {  
  int  a=4,  b=8,  c=12,  d=16;  
  Fun(a,  b,  &c,  &d);  
  printf("%d  %d  %d  %d",  a,  b,  c,  d);  
  return  0;  
}  
 
 
Answer:      4 8 16 4  
 
 

Page  2  of  5  
Question  2.    (4  marks)  
 
The  program  below  is  supposed  to  calculate  the  mean  of  the  non-­‐zero  data  values  using  the  
function:    calculate_mean.  
 
Unfortunately,  the  code  contains  4  errors.    Circle  the  errors  in  the  code  in  the  first  column,  and  
indicate  how  to  correct  the  errors  in  the  2nd  column  .    
 
Identify  error  in  code  by  circling   Correct  error  circled  to  left  
#include  <stdio.h>    
double  calculate_mean  (int  [],  int  *);    
int  main()  {    
int  Data[]={43,55,65,0,0,64,0,54,0,57,45,53}   missing  ;  
double  mean;    
int  num;    
num  =  sizeof(Data)/sizeof(int);    
mean  =  calculate_mean(Data,  num);   &num  
printf("Mean  of  %d  non  zero  data  is  %0.2f.\n",  num,  mean);    
       return  0;    
}    
 
double  calculate_mean  (int  A[],  int  *  N){    
int  sum=0;      
int  counter=0,  i;    
int  *ptr  =  &A;   A  
for(i=0;  i  <  *N  ;  i++)  {    
if(*ptr)  {    
sum  +=  *ptr;    
counter++;    
}    
ptr++;    
}    
*N  =  counter;    
       return  sum/counter;   (double)sum  
}    
 
   

Page  3  of  5  
Question  3.    (6  marks)  
 
Consider  a  rectangular  array  of  cells.  For  a  specified  element  of  the  array,  write  a  function  that  sets  all  
adjacent  cell  values  to  1.    Your  program  must  consider  all  of  the  following  cases:  

(0,0) (0,1) (0,2) (0,3) (0,4) (0,0) (0,1) (0,2) (0,3) (0,4) (0,0) (0,1) (0,2) (0,3) (0,4)

(1,0) (1,1) (1,2) (1,3) (1,4) (1,0) (1,1) (1,2) (1,3) (1,4) (1,0) (1,1) (1,2) (1,3) (1,4)

(2,0) (2,1) (2,2) (2,3) (2,4) (2,0) (2,1) (2,2) (2,3) (2,4) (2,0) (2,1) (2,2) (2,3) (2,4)

(3,0) (3,1) (3,2) (3,3) (3,4) (3,0) (3,1) (3,2) (3,3) (3,4) (3,0) (3,1) (3,2) (3,3) (3,4)
Case of interior cell Case of cell at an edge Case of cell at a corner

 
In  each  example  above,  the  black  cell  is  the  specified  cell  [x][y],  and  the  adjacent  cells  are  gray.  
 
Use  the  following  preprocessor  definitions  and  function  prototype:  
 
#define  ROWS  4  
#define  COLS  5  
void    set_cells  (int  Matrix[ROWS][COLS],  int  x,  int  y);  

Solution: One possibility

void    set_cells  (int  Matrix[ROWS][COLS],  int  x,  int  y)  {  


  int  i,  j;  
  for(i=x-­‐1;  i<=x+1;  i++){  
         for(j=y-­‐1;  j<=y+1;  j++)  
         {  
       if(!(i  ==  x  &&  j  ==  y)  &&  (i  >=  0)  &&  (i  <  ROWS)  &&  (j  >=  0)  &&  (j  <  COLS))  
       {  
          Matrix[i][j]  =  1;  
       }  
         }  
  }  
}  

Page  4  of  5  
Question  4.    (6  marks)  
 
Given  an  integer  array  A[  ],  write  a  function  that  returns  the  index  of  the  array  value  that  is  closest  to  the  
average  value  of  the  entire  array.  
 
For  example,  if  A[  ]  =  {12,  1,  7,  14,  2,  9,  5},  then  the  function  returns  2,  to  indicate  that  A[2]  =  7  is  closest  to  
the   average   value   of   50/7   =   7.14.     You   may   assume   that   the   array   contains   only   one   instance   of   a   value  
closest  to  the  average.    You  may  call  the  math.h  function  fabs  (  ),  that  calculates  the  absolute  value  of  a  
double.  
 
The  prototype  for  this  function  is:  

int  closest2Average  (int  A  [  ],  int  N);  

where  N  is  the  number  of  elements  in  A.  


 
One  solution:  
 

int    closest2Average  (int  A[  ],    int  N)  {  


 
       int  i,  index;  
       double  avg  =  0.,  diff;  
 
       for  (i=0;  i<N;  i++)  
               avg  +=  A[i];  
       avg  /=  N;  
 
       diff  =  fabs(A[0]-­‐avg);  
       index  =  0;  
       for  (i=1;  i<N;  i++)  {  
               if  (fabs(A[i]-­‐avg)  <  diff)  {  
                       diff  =  fabs(A[i]-­‐avg);  
                       index  =  i;  
               }  
       }  
 
       return  index;  
}  
 

Page  5  of  5  

You might also like