Professional Documents
Culture Documents
University of Toronto Faculty of Applied Science and Engineering APS106 MIDTERM - February 27, 2014
University of Toronto Faculty of Applied Science and Engineering APS106 MIDTERM - February 27, 2014
OF
TORONTO
FACULTY
OF
APPLIED
SCIENCE
AND
ENGINEERING
APS106
MIDTERM
–
February
27,
2014
INSTRUCTORS:
V.
Sinnathurai,
L.
Shu,
S.
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.
An
aid
sheet
is
attached
to
the
end
of
this
test.
Time
allotted:
60
min
Question
Maximum
Mark
Actual
Mark
1
9
2
3
3
8
Total
20
Page
1
of
6
Question
1.
(9
marks)
(a
-‐
1
mark)
Convert
the
following
decimal
number
to
binary
(base
2):
23
Answer:
10111
(b
-‐
1.5
marks)
For
the
following
bit
of
code:
int i;
for (i=1; i<5; i++)
???
write
the
one
line
of
code
that
will
generate
the
following
output:
1 8 2 7 3 6 4 5
Answer:
printf ("%d %d ", i, 9-i);
(c
-‐
1
mark)
What’s
the
output
of
the
following
bit
of
code?
double d = 1.8;
int i = d;
do {
i = (i%3)*4;
printf ("%d ", i++);
d *= 2.0;
} while (d < 6.0);
Answer:
4 8
Page
2
of
6
(d
-‐
2
marks)
Consider
the
following
bit
of
code:
char c;
int i = 4;
while (c != '\n') {
c -= i--;
putchar (c);
c = getchar ( );
}
What’s
the
output
if
the
user
enters:
H3pf
Answer:
D0ne
(e
-‐
1.5
marks)
For
the
following
bit
of
code:
int i;
if ( ??? )
printf ("%d ", i);
}
what
conditional
???
will
result
in
the
following
output:
3 9 15 21 27
Answer:
i%3 == 0 && i%2 == 1
Page
3
of
6
(f
-‐
2
marks)
What’s
the
output?
double fnc (int A);
int main () {
int i;
double d;
return 0;
}
double B;
B = (double) (A/2)*2;
return B;
}
Answer:
1 0.0 2 2.0
Page
4
of
6
Question
2.
(3
marks)
The
Fibonacci
sequence
is
a
famous
number
series,
where
each
number
is
the
sum
of
the
prior
two:
1,
1,
2,
3,
5,
8,
13,
21,
34,
55,
…
The
program
below
is
supposed
to
write
out
the
first
N
Fibonacci
numbers
as
specified
by
the
user,
separated
by
commas.
For
example,
if
the
user
enters
N
=
7,
the
program
should
output:
1,
1,
2,
3,
5,
8,
13
Unfortunately,
the
code
contains
3
errors.
Carefully
circle
the
errors,
and
indicate
corrections
beside
the
statements
as
comments
(//).
#include <stdio.h>
printf("\n");
return 0;
}
Page
5
of
6
Question
3.
(8
marks)
A
file
“numbers.txt”
contains
a
list
of
integers.
Write
a
program
that
reads
the
file
(you
may
assume
it
exists)
and
identifies
the
smallest
positive
(>
0)
and
largest
negative
(<
0)
values
in
the
list.
For
example,
if
numbers.txt
contains
the
following
data,
your
program
would
indicate:
The smallest positive value is 8.
12 The largest negative value is -3.
37
-15
If
the
file
does
not
contain
positive
values,
or
does
not
contain
negative
values,
your
-43
program
should
respond
appropriately.
8
-21
54
-3
0
14
-5
#include <stdio.h>
int main ( ) {
FILE * input;
input = fopen (numbers.txt", "r");
int num, pos = 0, neg = 0;
return 0;
}
Page 6 of 6