11100827

Embed Size (px)

Citation preview

  • 8/3/2019 11100827

    1/5

    11100827 CHANDRA BHUSHAN SAH RD1106A02

    SOLUTION OF ASSIGNMENT OF DATASTRUCTURE (CAP511)

    1. Consider the linear arrays AAA(5:50, -2:12), BBB (-5:10,2:5) and CCC(1:8,3:12,-1:6).

    (a) Find the number of elements in each array

    (b) Suppose Base(AAA) = 300, Base(BBB) = 1000, Base(CCC) = 500 and w=4 words per

    memory cell for AAA,BBB and CCC. Find the address of AAA[15,2] in row major order,

    BBB[3,3] in column major order and CCC[5,5,2] in column major order.

    Solution:A)

    AAA(5:50,-2:12)M = UB LB + 1 N = UB LB + 1

    = 50 5 + 1 = 12 - ( - 2) + 1

    = 46 = 15

    So, No. Of elements in AAA(5:50, -2:12) = M N= 46 15

    = 690

    BBB(-5:10,2:5)M = UB LB + 1 N = UB LB + 1

    = 10 ( - 5) + 1 = 5 - 2 + 1

    = 16 = 4

    So, No. Of elements in BBB(-5:10,2:5) = M N

    = 16 4

    = 64

    CCC(1:8,3:12,-1:6) L1 = UB LB + 1 L2 = UB LB + 1 L3 = UB LB + 1

    = 8 1 + 1 = 12 - 3 + 1 = 6 - ( - 1) + 1

    = 8 = 10 = 8

    So, No. Of elements in BBB(-5:10,2:5) = L1 L2 L3

    = 8 10 8

    = 640

    B)

    AAA(5:50,-2:12)M = 46 & N = 15

    AAA[15,2] J = 15 & K = 2

    Given: Base(AAA) = 300,

    w = 4

    Thus, according to Row-major order

    LOC(AAA[15,2]) = Base(AAA) + w [ N (J 1) + ( K -1) ]

    = 300 + 4 [ 15 (15 -1) + ( 2 1) ]

    = 300 + 4 ( 15 14 + 1 )

    = 300 + 4 211

    = 300 + 844

    = 1144

    BBB(-5:10,2:5)M = 16 & N = 4

  • 8/3/2019 11100827

    2/5

    BBB[3,3] J = 3 & K = 3

    Given: Base(BBB) = 1000,

    w = 4

    Thus, according to Column-major order

    LOC(BBB[3,3]) = Base(BBB) + w [ M (K 1) + ( J -1) ]

    = 1000 + 4 [ 16 (3 -1) + ( 3 1) ]= 1000 + 4 ( 16 2 + 2 )

    = 1000 + 4 34

    = 1000 + 136

    = 1136

    CCC(1:8,3:12,-1:6) L1 = 8, L2 = 10 & L3 = 8

    Given: Base(CCC) = 500,

    w = 4

    CCC[5,5,2] E1 = 5 1 E2 = 5 3 E3 = 2 - ( - 1)

    = 4 = 2 = 3

    Thus, according to Column-major orderLOC(CCC[5,5,2]) = Base(CCC) + w [ (E1.L2 + E2) L3 +E3 ]

    = 500 + 4 [ ( 4 10 + 2 ) 8 + 3 ]

    = 500 + 4 ( 42 8 + 3 )

    = 500 + 4 339

    = 500 + 1356

    = 1856

    2. Sort 20,35,40,100,3,10,15 using insertion sort.

    Solution:

    Suppose given elements 20,35,40,100,3,10,15 are the element of an array A. And these can besorted by using insertion sort as follows:

    Pass K=1 K=2 K=3 K=4 K=5 K=6 K=7 Sorted

    A[0] - - - - - - - -

    A[1] 20 20 20 20 20 3 3 3

    A[2] 35 35 35 35 35 20 10 10

    A[3] 40 40 40 40 40 35 20 15

    A[4] 100 100 100 100 100 40 35 20

    A[5] 3 3 3 3 3 100 40 35

    A[6] 10 10 10 10 10 10 100 40

    A[7] 15 15 15 15 15 15 15 100

    Pass 1: A[1] element is supposed to be sorted in Pass 1.

    Pass 2: Since A[2] > A[1]. Thus A[1] and A[2] are sorted.

    Pass 3: In this pass A[3] > A[2] and A[1] and A[2] is already sorted. Thus A[1], A[2] and A[3] are

  • 8/3/2019 11100827

    3/5

    sorted.

    Pass 4: Here, A[4] i.e.; 100 is greater than A[3]=40 and A[1], A[2], and A[3] is already sorted.

    Thus A[1], A[2], A[3] and A[4] are sorted.

    Pass 5: Since A[5] is less than A[4], A[3], A[2] and A[1]; so A[4], A[3], A[2] and A[1] is shifted

    to A[5], A[4], A[3] and A[2] respectively and the element of A[5] i.e.; 3 is inserted to the place

    A[1]. So that A[1], A[2], A[3], A[4] and A[5] become sorted.

    Pass 6: Here again, A[6] i.e.; 10 is less than A[5], A[4], A[3] and A[2]; thus it is required to insert

    the element of A[6] to A[2] and shift A[5], A[4], A[3] and A[2] to A[6], A[5], A[4] and A[3]

    respectively and now the elements A[1], A[2], A[3], A[4], A[5] and A[6] become sorted.

    Pass 7: Here A[7] is required to be inserted at A[3] because it is less than A[6], A[5], A[4] and

    A[3] and also A[6], A[5], A[4] and A[3] is required to be shifted to A[7], A[6], A[5] and A[4]

    respectively.

    Here, we see that the elements 20,35,40,100,3,10,15 become sorted to

    3,10,15,20,35,40,100 by using Insertion Sort in 7 passes. The above table shows the same in

    which each pass, arrow shows where the circled element i.e.; Kth element is to be inserted.

    3. Write an Algorithm to Find the Smallest and Largest Element in the Array {23, 12, 89,

    56, 26, 11, 31}

    Solution:

    Suppose A is the array of given elements 23, 12, 89, 56, 26, 11, 31. The algorithm finds thelocation LOC_MIN & LOC_MAX and the value MIN & MAX of the largest and smallest

    element of array A. The variable K is used as counter.

    Step 1> [Initialize.] Set K : = 1, LOC_MIN : = 1, LOC_MAX : = 1, MIN : = A[K]

    and MAX : = A[K].

    Step 2> [Increment Counter.] Set K : = K + 1.

    Step 3> [Test Counter.] If K > 7 then:

    Write: LOC_MIN, MIN, LOC_MAX, MAX and Exit.

    Step 4> [Compare and update.] If MAX < A[K] then:

    Set LOC_MAX : = K, MAX : = A[K] and End If.

    If MIN > A[K] then:

    Set LOC_MIN : = K, MIN : = A[K] and End If.

    Step 5> [Repeat Loop.] Go to Step 2.

    Step 6> Exit.

    Finding Smallest and Largest Element by using above algorithm.Pass 1> K = 1, LOC_MIN = 1, LOC_MAX = 1,

    MIN = 23 and MAX = 23 . ( A[K = 1] = 23 )

    K = K + 1 = 1+ 1 = 2

    LOC_MIN = K = 2, MIN = 12 ( A[K = 2] = 12 )

    Pass 2> K = 3, LOC_MIN = 2, LOC_MAX = 3, MIN = 12 and MAX = 89 ( A[K = 3] = 89 )

  • 8/3/2019 11100827

    4/5

    Pass 3> K = 4, LOC_MIN = 2, LOC_MAX = 3, MIN = 12 and MAX = 89

    Pass 4> K = 5, LOC_MIN = 2, LOC_MAX = 3, MIN = 12 and MAX = 89

    Pass 5> K = 6, LOC_MIN = 6, LOC_MAX = 3, MIN = 11 and MAX = 89 ( A[K = 6] = 11 )

    Pass 6> K = 7, LOC_MIN = 6, LOC_MAX = 3, MIN = 11 and MAX = 89

    Pass 7> K = 8. Out of loop.

    Smallest Value = MIN = 11 at location = LOC_MIN = 2, and

    Largest Value = MAX = 89 at location = LOC_MAX = 3.

    4. Consider a class of 30 students takes 5 tests in which scores ranges from 0 to 100.

    Suppose the test Scores are stored in 30*6 array test. Write algorithm to find the

    students whose average grade is highest and lowest.

    Solution:Suppose A is an array of 30*6 that stores the test scores of 30 students of 5 tests as below:Studen

    t

    Test 1 Test 2 Test 3 Test 4 Test 5

    1

    2

    3

    4

    .

    .

    .

    .

    30

    56

    68

    71

    82

    .

    .

    .

    .

    66

    56

    86

    69

    88

    .

    .

    .

    .

    56

    63

    68

    78

    75

    .

    .

    .

    .

    71

    46

    78

    64

    81

    .

    .

    .

    .

    80

    79

    68

    70

    85

    .

    .

    .

    .

    86

    In this algorithm, the variables STUD_HIGH, STUD_LOW, HIGH_AVG and LOW_AVG the

    serial no. of student having highest and lowest average score with their respective values. The

    variable K is used as a counter and AVG is used to store average.

    Step 1> [Initialize.] Set K : = 1, STUD_HIGH : = 1, STUD_LOW : = 1,

    AVG : = (A[K,2] + A[K,3] + A[K,4] + A[K,5] + A[K,6] ) / 5,

    HIGH : = AVG and MAX : = AVG.

    Step 2> [Increment Counter.] Set K : = K + 1.Step 3> [Test Counter.] If K > 30 then:

    Write: STUD_HIGH, HIGH, STUD_LOW, LOW and Exit.

    Step 4> [Compare and update.] AVG : = (A[K,2] + A[K,3] + A[K,4] + A[K,5] + A[K,6] ) / 5

    If AVG < LOW then:

    Set STUD_LOW : = K, LOW : = AVG and End If.

    If AVG > HIGH then:

    Set STUD_HIGH : = K, HIGH : = AVG and End If.

    Step 5> [Repeat Loop.] Go to Step 2.

    Step 6> Exit.

    5. Write algorithm to find the students whose score is highest for each test. And

  • 8/3/2019 11100827

    5/5

    lowest for each test for Q4.

    Solution: HIGH

    LOW

    In this algorithm, I have take two arrays of size 5 named HIGH and LOW to store the serial no.

    of student who got highest and lowest average score in each test. The variable J and K is used as

    a counter and AVG is used to store average.

    Step 1> Repeat Step 2 to 5 for K = 1 to 5.

    Step 2> Set J : = 1, HIGH[K] : = 1, LOW[K] : = 1, HIGHEST : = A[1, K]

    and LOWEST : = A[1, K]Step 3> Repeat Step 4 and 5 Until J < = 30

    Step 4> [Compare and update.]

    If HIGHEST < A[J,K] then:

    Set HIGH[K] : = J and End If.

    If LOWEST > A[J,K] then:

    Set LOW[K] : = J and End If.

    Step 5> Set J : = J +1

    Step 6> Set FLAG1 : = 1, FLAG2 : = 1 and K = 1

    Step 7> Repeat Step 8 and 9 until K > =5

    Step 8> If HIGH[K] ! = HIGH[1] then:

    Set FLAG1 : = 0 and End If.If LOW[K] ! = HIGH[1] then:

    Set FLAG2 : = 0 and End If.

    Step 9> Set K : = K + 1

    Step 10> If FLAG1 = 1 then:

    Write: Student with Serial No. HIGH[1] scored highest in each test.

    Otherwise:

    Write: No such Student found who scored highest in each test.

    End If.

    Step 11> If FLAG2 = 1 then:

    Write: Student with Serial No. LOW[1] scored lowest in each test.

    Otherwise:Write: No such Student found who scored lowest in each test.

    End If.

    Step 12> Exit.

    Test 1 Test 2 Test 3 Test 4 Test 5

    Studen

    t Test 1 Test 2 Test 3 Test 4 Test 5

    Studen

    t