ts-6 structure and dynamic memory allocation

Upload: pareen5

Post on 02-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 TS-6 Structure and Dynamic Memory Allocation

    1/5

    Programming and Data StructuresTutorial 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 100p = (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&/';st!ct ,tag *net;

    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 *net;

    main()st!ct node 314 34 3/;315c2al = 1; 35c2al = 10; 3/5c2al = 100;315net = 63; 35net = 63/; 3/5net = 631;

  • 8/10/2019 TS-6 Structure and Dynamic Memory Allocation

    2/5

    pintf(78d48d74 35net 9: c2al4 35net 9: net 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&

  • 8/10/2019 TS-6 Structure and Dynamic Memory Allocation

    3/5

    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), andBalance 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 *? atomp;

    F! aim is to comp!te t$e s!m and pod!ct of tGo s!c$ comple 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 a 4 int + 4 int *a 4 int *+ ) ?*(a1?+1)(a?+)*?

    *a = a1 * + a * +1; *+ = +1 * +; atHed!ce( ,,,,,,,,,,,4 ,,,,,,,,,,,,);

    2oid atM!l ( int a1 4 int +1 4 int a 4 int + 4 int *a 4 int *+ ) ?*(a1?+1)*(a?+) *?

    *a = a1 * a; *+ = +1 * +; atHed!ce( ,,,,,,,,,,,,,,,,,4,,,,,,,,,,,,,,, );

  • 8/10/2019 TS-6 Structure and Dynamic Memory Allocation

    4/5

    atomp compS!m ( atomp z1 4 atomp z ) ?* omp!te z1 z *?

    atomp z;?* Set t$e eal pat of z *?atS!m (,,,,,,,,,,,,,,,,,,,,, );

    ?* Set t$e imagina" pat of z *?atS!m (,,,,,,,,,,,,,,,,,,,,,,, );et!n z;

    atomp compM!l ( atomp z1 4 atomp z ) ?* omp!te z1 * z *?

    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 toEploe&'. To start with, room u is marked as visited and is also

    inserted in the array toEploe&'. 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 toEploe&'. A good way to handle elements of the

    array toEploe&'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 toEploe&'.

    Complete the following function to solve this connectivity problem.

    int connected ( int M&MAXIM'&MAXIM'4 int n4 int !4 int 2 )int *2isited4 *toEploe4 i4 4 stat4 end;?* Allocate memo" fo n integes to eac$ of 2isited and toEploe *?2isited = ,,,,,,,,,,,,,,,,,,,,,,,;toEploe = ,,,,,,,,,,,,,,,,,,,,,,,,,;fo (i=0; in; i) 2isited&i' = 0; ?* Initialize t$e aa" 2isited*?2isited&!' =,,,,,,,,,,,,,,,,,,,,,,, ; ?* Ma@ oom ! as 2isited *??* Inset oom ! in t$e aa" toEploe*?toEploe&0' = !; stat = end = 0;?* As long as t$ee ae moe ooms to eploe*?

    G$ile (,,,,,,,,,,,,,,,,,,, ) i = toEploe&stat'; stat;?* if i is t$e destination oom 24 et!n t!e*?if ( i == 2 ) et!n 1;?* $ec@ all ooms s$aing doos Git$ oom i*?fo (=0; n; ) ?* if t$ee is a doo +etGeen i and 4 and is not 2isited*?if (( ,,,,,,,,,,,,,,,,) 66 ( ,,,,,,,,,,,,,,,)) ?* Ma@ as 2isited *?,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    ?* Inset in toEploe&' and ad!st t$e insetion inde*?,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,?* 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

  • 8/10/2019 TS-6 Structure and Dynamic Memory Allocation

    5/5

    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)