ecs30_w2012_final_keyx

jaspersugarlandΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 10 μήνες)

80 εμφανίσεις

ecs30


Final Exam, Winter 2012 on March 21
th
, 2012

-
1
/
9
-

ECS
-
30, Winter 2012,
Final

Exam


Name:

________
Key
__________________


Student ID:

__________________________


E
-
mail:

__________________________


Important Instructions:




Close

book/notes



We
have totally 6

question
s

(
4
~5
% each, totally 2
5%),

9
pages.



You are allowed to ask questions to the instructor

(but NOT to each other)
during
the exam

for clarification of the exam

itself
.
Signal/raise your hand and h
e will
walk to your place and handle any clarification
-
related question. And, all suc
h
questions (plus the answer) will be
re
-
announced to the whole class if appropriate.



Please write precise and clean answers. If you write down the steps regarding how
you obtain the final answer, I might give you partial credits even the final answer
itse
lf is wrong.



Please write clearly to let the instructor recognize/understand your
hand writing
.

Please write clearly and with
reasonable size
.



Every page of this exam book needs to be returned. Also, while turning in your
exam book, please make sure to
SIGN the special sign
-
up sheet. If you do NOT
sign up and in case we lose your exam book, it will be hard to convince us that
you actually turn in the exam book back to us.



If we suspect any cheating behavior, we will pass the case to the academic
committe
e immediately.



All the code in this

exam should contain no C syntax

errors. If you find one, that
might be a typo so please let me know immediately and I will clarify that. Some
of the code might contain some warnings, especially if you run on different
ma
chine architectures

(and turn on different warning options)
.

ecs30


Final Exam, Winter 2012 on March 21
th
, 2012

-
2
/
9
-

Q
-
0
1

Question/Answer: (4
%)

(
Binary/Decimal/Hexadecimal, Number System Transformation, 1%
each)


80

(decimal) =


1010000



(binary) =



0x50


(hexad
ecimal)





0x80

(hexadecimal) =

10000000



(binary) =

128


(decimal)





Q
-
02

Question/A
nswer:

(4%)

(Value/Content/Address/Pointer)

Questions 2

is related to the following
program:

#include <stdio.h>

#include <
stdlib.h>


#define STRSIZ 10

#define PL_COUNT 8


typedef struct planet {


char name[STRSIZ];


double diameter;


int moons;


double orbit_time,


rotation_time;

} planet_t;


int

main(void)

{


int x

= 222
;


int *xp

= &x
;



printf("size [int, char
, double
] = [%d, %d
, %d
]
\
n",


size
of(int),
sizeof(char)
,

sizeof(double)
);



printf("&x = 0x%x
\
n", (int) &x);



printf("[
q2.1
] %x
\
n", xp);


printf(
"[q2.2
] %x
\
n", xp[0]
);


printf("[q2.3
] %x
\
n", (in
t)
(((char *)(xp
)) + 1));


p
rintf("[q2.4
] %x
\
n", (int) (((planet_t *)(
x
p
)) + 1));


return 0;

ecs30


Final Exam, Winter 2012 on March 21
th
, 2012

-
3
/
9
-

}

(continuing on Q2)
Assuming we are running the above program
on a 32
-
bits address
machine
. The following is the execution of the program until the
second
print
f

statement:


PC
-
15
:/Users/wu%

PC
-
15
:/Users/wu%

gcc
-
Wall
-
Wstrict
-
prototypes Q2
.c

PC
-
15
:/Users/wu% ./a.out

size [int, char
, double
] = [4, 1
, 8
]

&x = 0xbffffa08


[q2.1]__
0xbffffa08
________________


[q2.2]___
0xDE or
222
_______________


[
q2.3]
_
0xbffffa08 + sizeof(char)
_


[q2.4]_

0xbffffa08 + sizeof(planet_t)
_


ecs30


Final Exam, Winter 2012 on March 21
th
, 2012

-
4
/
9
-

Q
-
0
3

Question/Answer: (4
%)


(
string duplication)

Please implement the string library function
str
dup
()
.

Here is some information regarding
strdup
()

from the man page:


STRDUP(3)

BSD Library Functions Manual STRDUP(3)

NAME strdup
--

save a copy of a string

LIBRARY Standard C Library (libc,
-
lc)

SYNOPSIS


#include <string.h>


char * strdup(const char *s);

DESCRIPTION

The strdup() function allocates suff
icient memory for a copy of the string s1, does the
copy, and returns a pointer to it. The pointer may subsequently be used as an argument to
the function free(3). (The following part you do NOT need to worry about: If insufficient
memory is available, NU
LL is returned and errno is set to ENOMEM).



char *

str
dup

(
const char *s)

{


return (strcpy(malloc(
sizeof(char) * (strlen(s) + 1)), s)
;




4% if correct or almost correct (1~2 syntax errors are OK)

3% if it has one critical logical mistake like “forgot
to
+1”.

2% 2 major logical mistake.

1% had tried.

0% nothing or nothing make sense.











}





ecs30


Final Exam, Winter 2012 on March 21
th
, 2012

-
5
/
9
-

(Tower of Hanoi with 4 Pegs

for Questions 4 and 5
)
Given the following sub
-
functions for Tower of Hanoi with 4 pegs:


void

OneLeft(char from_peg, char
to_peg, int big)

{


printf("[%16d] ==> move disk %3d from %c to %c
\
n",


movecount++, big, from_peg, to_peg);

}


void

TwoLeft(char from_peg, char to_peg, char aux_peg, int big)

{


printf("[%16d] ==> move disk %3d from %c to %c
\
n",


movecount++, big
-
1, from_peg, aux_peg);


printf("[%16d] ==> move disk %3d from %c to %c
\
n",


movecount++, big, from_peg, to_peg);


printf("[%16d] ==> move disk %3d from %c to %c
\
n",


movecount++, big
-
1, aux_peg, to_peg);

}


void

ThreeLe
ft

(char from_peg, char to_peg, char aux1, char aux2, int big)

{


printf("[%16d] ==> move disk %3d from %c to %c
\
n",


movecount++, big
-
2, from_peg, aux1);


printf("[%16d] ==> move disk %3d from %c to %c
\
n",


movecount++, big
-
1, from_peg,
aux2);


printf("[%16d] ==> move disk %3d from %c to %c
\
n",


movecount++, big, from_peg, to_peg);


printf("[%16d] ==> move disk %3d from %c to %c
\
n",


movecount++, big
-
1, aux2, to_peg);


printf("[%16d] ==> move disk %3d from %c to %c
\
n"
,


movecount++, big
-
2, aux1, to_peg);

}


ecs30


Final Exam, Winter 2012 on March 21
th
, 2012

-
6
/
9
-

And, the function of
Tower of Hanoi with three pegs:

void

tower3p(char from_peg, char to_peg, char aux_peg,


int big, int small)

{


if (big == small)


{



OneLeft(from_peg, to_peg, big);



r
eturn;



}


tower3p(from_peg, aux_peg, to_peg, big
-
1, small);


OneLeft(from_peg, to_peg, big);


tower3p(aux_peg, to_peg, from_peg, big
-
1, small);

}


Here
is a
Tower of Hanoi with 4 pegs programs:

void

tower4p(char from_peg, char to_peg, char aux1,



char aux2, int big, int small)

{


if (big == small)


{



OneLeft(from_peg, to_peg, big);



return;



}



if (big == (small+1))


{



TwoLeft(from_peg, to_peg, aux1, big);



return;



}



if (big == (small+2))


{



ThreeLeft(from_peg,
to_peg, aux1, aux2, big);


return;



}



tower4p
(from_peg, aux1, aux2, to_peg, big
-
3, small);


tower3p(from_peg, to_peg, aux2, big, big
-
2);


tower4p(aux1, to_peg, aux2, from_peg, big
-
3, small);

}


ecs30


Final Exam, Winter 2012 on March 21
th
, 2012

-
7
/
9
-

Q
-
04

Question/A
nswer:

(5
%)

Please calculate the n
umber of moves starting with 4,
5, 6,

7 and 8

disks (i.e., big =
4,
5,6,7
,8

respectively
, 1% each
) for
the program and
fill up the following table:








Number of moves




----------------------------------






T
ower4p


=======

--------------
-------
-------------

1 disk 1

=
======
--------------------
--------------

2

disks

3

=
======
--------------------
--------------

3

disks

5

=
======
--------------------
--------------

4

disks

1+7+1 = 9

=======
----------------------------------

5 disks

3+7+3 = 13

=======
----------------------------------

6 disks

5+7+5 = 17

=======
----------------------------------

7 disks

9+7+9 = 25

=======
----------------------------------

8 disks

13+7+13 = 33

=======
----------------------------------


Q
-
05

Question/A
nswer:

(4%)

Here is another version
of tower3p (Tower of Hanoi with 3 pegs).

void

tower3p
_another
(char from_peg, char to_peg, char aux_peg,


int big, int small)

{


printf(“%s
\
n”,

strdup(“Tower of Hanoi
with
3

pegs
”);


/*


* The above line might not make a lot of sense


* but

let us just assume this way.


*/



if (big == small)


{


OneLeft(from_peg, to_peg, big);


r
eturn;


}


tower3p
_another
(from_peg, aux_peg, to_peg, big
, small);


OneLeft(from_peg, to_peg, big);


tower3p
_another
(aux_peg, to_peg, from_peg, big,

small);

}


ecs30


Final Exam, Winter 2012 on March 21
th
, 2012

-
8
/
9
-

What’s wrong with this program

(
meaning that which principle/critical step had not
been followed
)
? And, if you run this program what would happen

(what would be
the
potential expected
incorrect
behavior at run time
)
?

Please answer the question with
the consideration of both “correctness of recursion” and “correct dynamic memory
allocation”.


<Recursion>

(1%: what’s wrong? 1%: what would happen?)

Please note


for the “what’s wrong” part, it’s
insufficient

to simply sa
y

something like
“Oh, you forgot “big


1”. You must state that, due to this small difference (i.e., “big”
versus “big


1”), which critical principle of recursion in programming is being violated
and why?



(1)

the exit condition will never be reached.

(2)

The pr
ogram will run infinitely (infinite loop) (and eventually stop due to the
stack limit


this part is optional)













<Memory Allocation>

(1%: what’s wrong? 1%: what would happen?)

Please note


simil
arly,
for the “what’s wrong” part, it’s important
to state the critical
principle of dynamic memory allocation in C has been violated and why?


(1)

the program doesn’t have a free() statement (to release the internal malloc()
from strdup()).

(2)

Eventually, the program will use up all the memory
resources (from t
he heap).

ecs30


Final Exam, Winter 2012 on March 21
th
, 2012

-
9
/
9
-

Q
-
0
6

Question/A
nswer:

(4%)

(Two
-
Dimensional Array
)

In C, arrays can be represented using the pointer (or
address) format. As an example, for any two dimensional array, the following is the
general format for translation:


int GameOfLife[12][12];

GameOfLife
[x][y]
is the same as

*((
int *) GameOfLife

+ x*
12

+ y)


Here
GameOfLife
[x][y]
, as the regular array format,
means the content the (y+1)
th

element of the (x+1)
th

row. We assume
size
of
[int, char] =
=

[4, 1]
.




Q6.1 (1%) H
ow do we represent

GameOfLife
[4][7]

in the pointer/address
format?


*(((int *) GameOfLife) + 4 *12 + 7)



Q6.2 (1%)
Furthermore, what is the 2D regular array format for

*((int *)

GameOfLife
+

4
2)
? Please fill in the subscript of
X

and
Y

between the following
pairs of brackets



X = ? Y = ?


GameOfLife
[
3

][
6

]


Q6.3 (1%) Then,
what is the value of
&(GameOfLife[1
][1])

if
GameOfLife

is
equal to
0xbffffa00
?


0xbffffa00 + sizeof(
int) *
(1
*12 + 1)




Q6.4 (1%)
And, finally, what is the value of
((char

*)
GameOfLife + 4
2)
,

again given that

GameOfLife
== 0xbffffa00
?


0xbffffa00 + sizeof(char) * 42