Professional Documents
Culture Documents
APS106H1 20141 621398289314mid 2014 B Answers
APS106H1 20141 621398289314mid 2014 B Answers
APS106H1 20141 621398289314mid 2014 B Answers
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
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);
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:
Page 5 of 5