Você está na página 1de 5

Programming and Data Structures

Tutorial sheet: 6
Topics: Structures and Dynamic Memory Allocation


Q1: Select the correct option :

(a) How many bytes are allocated to the pointer p after the following call?
#define MAXSIZE 100
p = (long int *)malloc(MAXSIZE * sizeof(long int));

(A) 4 (B) 100 (C) 400 (D) 1600

(b) Assume that my machine supports 32-bit addresses. The structure myStruct is declared as follows.
What value is returned by sizeof(struct myStruct)on my machine?

st!ct m"St!ct #
long A;
c$a %&10';
float (&10';
st!ct m"St!ct *);
*;
(A) 16 (B) 22 (C) 58 (D) 88

(c) Assume that on a certain machine an int variable is of size 32 bits and each memory address is also ofsize
32 bits. Assume further that the sizeof() function call returns the size of its operand in bytes. Considerthe
following structure:

st!ct m"St!ct #
int a;
int +&10';
int *c;
*;

What does sizeof(struct myStruct) return on this machine?
(A) 3 (B) 12 (C) 48 (D) 384

(d) In a machine with 32-bit integers and 32-bit addresses, what is sizeof(node), where node is defined as
follows:
t"pedef st!ct ,tag #
int a&-';
do!+le +&1.';
c$a c&/0';
st!ct ,tag *ne1t;
* node;
(A) 384 (B) 196 (C) 132 (D) 60

(e) Each of the following choices declares two variables A and B. Identify the pair in which A and B do not
have compatible organizations in memory.
(A) int *A, B[MAX]; (B) int *A[MAX], **B;
(C) int (*A)[MAX], **B; (D) int (*A)[MAX], B[MAX][MAX];

(f) What is the output of the following program?
st!ct node #
int c2al;
st!ct node *ne1t;
*
main()
#
st!ct node 314 304 3/;
315c2al = 1; 305c2al = 10; 3/5c2al = 100;
315ne1t = 630; 305ne1t = 63/; 3/5ne1t = 631;


pintf(78d48d74 305ne1t 9: c2al4 305ne1t 9: ne1t 9: c2al);
*;
(A) 1,10 (B) 1,100 (C) 10,100 (D) 100,1


Q2. What is printed by the following program?
#incl!de ;stdio5$:
st!ct a+c #
int a;
int *+;
int c&<';
*;
2oid foo (st!ct a+c 14 st!ct a+c "&') #
15a = 0<;
*(15+) = <0;
15c&0' = /0;
"&0' = 1;
*
int main () #
st!ct a+c 14 "&<' ;
int n = 00;
15a = <;
15+ = 6n;
15c&0' = 10;
"&0' = 1;
foo (14 ") ;
pintf (7 1= 8d4 8d >n74 15a4 15c&0') ;
pintf (7"&0'= 8d4 8d >n74 "&0'5a4 "&0'5c&0') ;
pintf (7n=8d>n74 n) ;
*

Q.3. (a)Consider a computer that has 4 byte memory addresses. What will be the output of the following
statement? Justify your answer.
printf(%d %d %d\n, sizeof(char *), sizeof(int *), sizeof(float *));

(b)Suppose we have the following statement inside a function foo( ), and int *p is a global variable:
p = (int *) malloc(50 * sizeof(int));
Suppose main( ) calls foo( ). From which of the following places can we access the memory allocated by
this call?
a. Only within the function foo( )
b. Within the function foo( ) and the function main( )
c. Within any function, that is, the memory is global

Q. 4. Fill in the blanks for performing operations as indicated in the comments. Write the output.
c$a *p;
p=,,,,,,,,,,,,,,,,,,; ?*)"namicall" allocate an aa" of 00 c$aactes
o *?
if (,,,,,,,,,,,,,) # ?* ($ec@ fo pope allocation *?
pintf(7Eo in memo" allocation5 >n7);
*
,,,,,,,,,,,,,,, ?* (op" a sting 7IIABCD7 to t$e allocated aa"
!sing stcp"() f!nction5 *?
pintf(78s >n74 pE/);

THE OUTPUT IS ______________

Q.5. Consider the following code:
t"pedef st!ct ,A+c#
int emp,id;
c$a name&00';
c$a add&0<';
do!+le sala";
* A+c;
A+c *a+c;


Assign a valid address to the pointer variable abc in the following two ways: (i) using an existing
address of an appropriate variable, and (ii) by dynamically allocating the appropriate memory. Write code
for printing all the data-fields which are pointed to by the pointer variable abc.

Q 6.1: Define a structure customer to specify data of customer in a bank. The data to be stored is: Account number
(integer), Name (character string having at most 50 characters), and Balance in account (integer).

6.2. Assume data for all the 100 customers of the bank are stored in the array :
struct customer bank[100];
The function, transaction, is used to perform a customer request for withdrawal or deposit to her account.
Every such request is represented by the following three quantities: Account number of the customer, request
type (0 for deposit and 1 for withdrawal) and amount.

The transaction function returns 0 if the transaction fails and 1 otherwise. The transaction fails only when the
account balance is less than the withdrawal amount requested.

The array bank (defined above) is another input to the transaction function and is suitably updated after every
request. In case of a failed transaction no change is made in the bank array.

The header for the function transaction is given below, complete the body of the function.

int transaction ( int account_number, int request_type, int amount,
struct customer bank [ 100 ] )
{
}

Q7. In this exercise we deal with complex numbers of the form (a/b) + i(c/d) with a; b; c; d integers and with b and
d positive. The following structure represents such a complex number.

t"pedef st!ct #
int a; ?* 3!meato of t$e eal pat *?
int +; ?* )enominato of t$e eal pat *?
int c; ?* 3!meato of t$e imagina" pat *?
int d; ?* )enominato of t$e imagina" pat *?
* at(omp;

F! aim is to comp!te t$e s!m and pod!ct of tGo s!c$ comple1 n!m+es Git$ eac$
faction ed!ced to t$e standad fom (i5e54 ed!ced to t$e fom m=n Git$ gcd(m;
n) = 14 n : 0)5 (10)

int gcd ( int a 4 int + ) ?* (omp!te t$e gcd of tGo integes *?
#
if (a ; 0) a = 9a; if (+ ; 0) + = 9+;
if (a == 0) et!n +; if (+ == 0) et!n a;
et!n gcd(+4 ,,,,,,,,,,,,,,,,,,);
*
2oid atHed!ce ( int *a 4 int *+ ) ?* Hed!ce a faction to loGest tems *?
#
int d;
d = gcd( ,,,,,,,,,,,,4,,,,,,,,,,, ); *a =,,,,,,,,,,, ; *+ =,,,,,,,,,,,;
*


2oid atS!m ( int a1 4 int +1 4 int a0 4 int +0 4 int *a 4 int *+ ) ?*(a1?+1)E(a0?+0)*?
#
*a = a1 * +0 E a0 * +1; *+ = +1 * +0; atHed!ce( ,,,,,,,,,,,4 ,,,,,,,,,,,,);
*


2oid atM!l ( int a1 4 int +1 4 int a0 4 int +0 4 int *a 4 int *+ ) ?*
(a1?+1)*(a0?+0) *?
#
*a = a1 * a0; *+ = +1 * +0; atHed!ce( ,,,,,,,,,,,,,,,,,4,,,,,,,,,,,,,,, );
*




at(omp compS!m ( at(omp z1 4 at(omp z0 ) ?* (omp!te z1 E z0 *?
#
at(omp z;
?* Set t$e eal pat of z *?
atS!m (,,,,,,,,,,,,,,,,,,,,, );
?* Set t$e imagina" pat of z *?
atS!m (,,,,,,,,,,,,,,,,,,,,,,, );
et!n z;
*
at(omp compM!l ( at(omp z1 4 at(omp z0 ) ?* (omp!te z1 * z0 *?
#
*

Q8. A house has n rooms numbered 0, 1, . . . , n 1. Consider the n n matrix M. The i, j-th entry of M is 1 if
there is a door between room i and room j; it is 0 otherwise. Given two rooms u, v, the following function finds
out whether there exists a way to go from room u to room v using the doors. The function works as follows.
It maintains two arrays 2isited&'and toE1ploe&'. To start with, room u is marked as visited and is also
inserted in the array toE1ploe&'. Then we enter a loop. We first check if there are some more rooms to
explore. Let i be one such room. We find out all unvisited rooms j sharing doors with room i. We mark all
these rooms j as visited and also insert them in the array toE1ploe&'. A good way to handle elements of the
array toE1ploe&'is to insert elements at the end (of the current list of rooms to explore) and consider the
rooms from beginning to end for further exploration. We maintain two indices start and end. The rooms yet to
be explored lie in the indices start, . . . , end in toE1ploe&'.
Complete the following function to solve this connectivity problem.

int connected ( int M&MAX)IM'&MAX)IM'4 int n4 int !4 int 2 )
#
int *2isited4 *toE1ploe4 i4 I4 stat4 end;
?* Allocate memo" fo n integes to eac$ of 2isited and toE1ploe *?
2isited = ,,,,,,,,,,,,,,,,,,,,,,,;
toE1ploe = ,,,,,,,,,,,,,,,,,,,,,,,,,;
fo (i=0; i;n; EEi) 2isited&i' = 0; ?* Initialize t$e aa" 2isited*?
2isited&!' =,,,,,,,,,,,,,,,,,,,,,,, ; ?* Ma@ oom ! as 2isited *?
?* Inset oom ! in t$e aa" toE1ploe*?
toE1ploe&0' = !; stat = end = 0;
?* As long as t$ee ae moe ooms to e1ploe*?
G$ile (,,,,,,,,,,,,,,,,,,, ) #
i = toE1ploe&stat'; EEstat;
?* if i is t$e destination oom 24 et!n t!e*?
if ( i == 2 ) et!n 1;
?* ($ec@ all ooms I s$aing doos Git$ oom i*?
fo (I=0; I;n; EEI) #
?* if t$ee is a doo +etGeen i and I4 and I is not 2isited*?
if (( ,,,,,,,,,,,,,,,,) 66 ( ,,,,,,,,,,,,,,,)) #
?* Ma@ I as 2isited *?
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

?* Inset I in toE1ploe&' and adI!st t$e insetion inde1*?
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
*
*
*
?* Joop ends5 Hoom 2 co!ld not +e eac$ed fom oom !5*?
?* Kee allocated memo" *?
fee( ,,,,,,,,,,,,,,,,,,); fee( ,,,,,,,,,,,,,,,,,,,);
?* Het!n fail!e *?
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
*

Q 9 For a pair of real numbers a, b with a < b, the interval [a, b] is defined as the set of all real numbers between
a and b, i.e., [a, b] = {x | x > a and x <= b}. epresent an interval b! the followin" stru#ture$
t!pedef stru#t { float a, b% } interval%
&e are "iven an arra! ' of n intervals [ai, bi] for i = (, ), . . . , n * ). &e loo+ at the union of all these
intervals, namel!, at the set , = -.from i=( to n/)0 [ai, bi]. 1he "iven intervals ma! be overlappin". 2ur tas+ is
to write , as the union of non/overlappin" intervals, i.e., as an arra! 3 of m non/overlappin" intervals [#4 , d4 ]
for 4 = (, ), . . . ,m * ) .for some m 5 n0. 's an example, #onsider the ten intervals$
[6, 7], [8, )9], [)(, ):], [)), )8], [):, 98], [96, 9:], [9;, 6)], [66, <9], [66, <(], [67, 68].


1he #orrespondin" arra! of non/overlappin" intervals is as follows. &e have m = 6 in this #ase.
[6, 7], [8, 6)], [66, <9].
=n order to solve this problem, we sort the input intervals with respe#t to their left end/points. 1hen we enter
a loop. &e store the next unpro#essed interval [ai, bi] in a temporar! stru#ture variable t. 1hen we loo+ at the
interval [ai>), bi>)]. =f ai>) <= t.b, the interval [ai>), bi>)] #an be mer"ed with t. 1his mer"in" step alters the
ri"ht end/point of t if and onl! if the ri"ht end/point of [ai>), bi>)] is stri#tl! bi""er than that of b. 1hen
#onsider the intervals [ai>9, bi>9], [ai>6, bi>6], . . . , [ai>l, bi>l] as lon" as mer"in" #an be done. &hen mer"in"
is no lon"er possible, we write t in the output arra! 3. &e then "o to the top of the loop, store[ai>l>), bi>l>)]
in t, and repeat the same pro#edure, until all of the n input intervals are ta+en #are of.

?omplete the followin" fun#tion that implements this al"orithm. 1he fun#tion should return the numberof
intervals in the output arra!. @ou do not have to write the sortin" fun#tion sotInte2als()
int megeInte2als ( inte2al A&' 4 int n 4 inte2al %&' )
?* A is t$e inp!t aa" of size n and % is t$e o!tp!t aa"*?
#
int i = 04 I = 0; ?* i is fo eading fom A4 I is fo Giting to %*?
inte2al t; ?* tempoa" 2aia+le *?
sotInte2als(A4n); ?* Sot A Git$ espect to t$e left end9points*?
G$ile (1) #
?* Stoe in t t$e ne1t !npocessed inp!t inte2al*?
t =,,,,,,,,,,,,,,, ; EEi;
?* As long as meging is possi+le*?
G$ile ((i ; n) 66 (,,,,,,,,,,,,,,,,,,,,,,,,,,, )) #
?* Joo@ at t$e possi+ilit" of e1tending t$e ig$t end9point of t*?
if (,,,,,,,,,,,,,,,,,, ) t5+ = ,,,,,,,,,,,,,,,;
EEi;
*
?* Lite t$e acc!m!lated inte2al t to t$e o!tp!t aa" %*?
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

?* ($ec@ G$et$e all inp!t inte2als ae pocessed*?
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

if (,,,,,,,,,,,,,,,,, ) et!n ,,,,,,,,,,,,,,,; ?* Het!n t$e size of % *?

*
*


Q 10 (a) Define a structure pt to represent a point in two dimensions. Use the pt structure to define a structure
rect representing a rectangle (in two dimensions).

(b) Write a function which takes as input an integer n, and an array of n rectangles, and returns the area of the
smallest rectangle enclosing all the n rectangles in the array. The function prototype is as follows:
int bounding_rectangle ( int n, struct rect rect_array[ ] );

Q 11. (a) Define a data type vector which consists of a dynamically allocated array to store the elements of a vector
and an integer to designate the size (dimension) of the vector. Assume that we deal with vectors of
floating-point numbers.

(b) Write a function that, for two input vectors v = (v0, v1, . . . , vn1) and w = (w0,w1, . . . ,wn1),
returns the dot product v w = v0w0 + v1w1 + + vn1wn1. If the input vectors are not of the same
dimension, your function should return some appropriate error value.
double dotProduct ( vector v, vector w )
{
}

Você também pode gostar