Algorithm Analysis and recurren algorithm Question 1...

11
Exercise 1 – EKT224 Lecturer : Salina Mohd Asi Date release : 27 Sept 2016 Algorithm Analysis and recurren algorithm Question 1 Consider the C code segment below. The program is to display addresses of elements from two (2) matrices. Analyze the code and obtain the frequency count of the program segment. Determine the bigO notation of the program complexity. [Pertimbangkan keratan kod C berikut. Program ini adalah memaparkan alamat bagi elemen – elemen dari dua (2) tatasusunan. Analisa program kod tersebut dan dapatkan kiraan kekerapannya dan tentukan kerumitannya dengan menggunakan notasi bigO] No. Code Segment Frequency 1. I=0; 2. While (I<2N) 3. array1[I] = 0; 4. I++; 5. End while 6. I=0; 7. While (I < 2N) 8. array2[I]=0; 9. For J =1 to N 10. array2[I][J] = 0; 11. End for 12. I++; 13. End While 14. For I = 1 to to 2N 15. printf(“Element [%d]: %d\n”, I, &array2[I]; 16. End For 17. For I = 1 to 2N 18. For J=1 to N 19. printf(“Element [%d]: %d\n”, I, &array2[I][J]; 20. End For 21. End For Total frequency is : _______________________________________________ BigO is _________________________________________________________ [10 Marks/Markah]

Transcript of Algorithm Analysis and recurren algorithm Question 1...

Exercise 1 – EKT224Lecturer : Salina Mohd AsiDate release : 27 Sept 2016

Algorithm Analysis and recurren algorithm

Question 1Consider the C code segment below. The program is to display addresses of elements from two (2)matrices. Analyze the code and obtain the frequency count of the program segment. Determine thebigO notation of the program complexity.[Pertimbangkan keratan kod C berikut. Program ini adalah memaparkan alamat bagi elemen – elemen dari dua (2)tatasusunan. Analisa program kod tersebut dan dapatkan kiraan kekerapannya dan tentukan kerumitannya denganmenggunakan notasi bigO]

No. Code Segment Frequency

1. I=0;

2. While (I<2N)

3. array1[I] = 0;

4. I++;

5. End while

6. I=0;

7. While (I < 2N)

8. array2[I]=0;

9. For J =1 to N

10. array2[I][J] = 0;

11. End for

12. I++;

13. End While

14. For I = 1 to to 2N

15. printf(“Element [%d]: %d\n”, I, &array2[I];

16. End For

17. For I = 1 to 2N

18. For J=1 to N

19. printf(“Element [%d]: %d\n”, I, &array2[I][J];

20. End For

21. End For

Total frequency is : _______________________________________________

BigO is _________________________________________________________

[10 Marks/Markah]

Exercise 1 – EKT224Lecturer : Salina Mohd AsiDate release : 27 Sept 2016

Question 2Consider the C code segment below in Figure 1, the program is to display addresses of elementsfrom two (2) matrices. Analyze the code and obtain the frequency count of the program segment.Determine the bigO notation of the program complexity.

[Pertimbangkan keratan kod C berikut. Program ini adalah memaparkan alamat bagi elemen – elemen dari dua (2)tatasusunan. Analisa program kod tersebut dan dapatkan kiraan kekerapannya dan tentukan kerumitannyamenggunakan notasi bigO]

[10 Marks/Markah]

Question 3

Consider the following recurrence relation where T(n) is the time complexity of some statements.

T(n) = 2 * T(n/2) + c * n if n ≥ 2

= d if n < 2

1. Demonstrate the generic solution for time T(n) at kth steps using substitution method.

2. Solve the kth step for T(n) when n=2k.

//initialized array1for(i=0;i<n;i++)

array1[i]=0;

//initialized array2for(i=0;i<n;i++)

for(j=0;j<3n;j++)array2[i][j]=0;

printf("Print address for array1\n");for(i=0;i<n;i++){

printf("Element[%d]: %d\n",i,&array1[i]);}printf("===================================\n");

printf("\nPrint address for array2\n");for(i=0;i<n;i++)

for(j=0;j<3n;j++)printf("Element[%d][%d]: %d\n",i,j,&array2[i][j]);

printf("==================================\n");

Figure. 1.Rajah 1

Exercise 1 – EKT224Lecturer : Salina Mohd AsiDate release : 27 Sept 2016

Let's discuss this

Exercise 1 – EKT224Lecturer : Salina Mohd AsiDate release : 27 Sept 2016

Chapter Array and Stack

Question 1Consider 3 types of array A[-2:10], B[2:7, 3:10] and C[1:3, 1:10, 1:10]. Array A consist only integerelements and 2-bytes in each, array B consists only real elements and 4-byte in each, and array C consists only character elements and 1-bytes in each. Note that, base address of A is 250 and the 3 arrays A, B, C represent in memory in sequence as follows. Answer the following questions:

A[-2:10], B[2:7, 3:10], C[1:3, 1:10, 1:10]

(a) Calculate the address of A[10] element. (b) Calculate the address of B[3,5] element. (c) Calculate the address of C[3,3,3] element. (d) If an array D[1:5] need to attach at the end of array C, calculate the base address of array D.

Question 2There are two arrays have been declared in a program: int arrayA[1:10, 0:10] and int arrayB[1:3,1:5, 1:6]. Let assume that the two array were create next to each other arrayA and followed byarrayB. Calculate the address of arrayA[3,3] and arrayB[3,3,3]. The base address is 200.

Question 3

There are two algorithms y and z have time complexites of n2 and 2n. If these two algorithm will

be executed on a machine which take 10-6 second to execute an instruction. Calculate the time taken for these algorithm to complete their tasks if the input size of 100.

Question 4The following are details of and array MARKS [Row, Column].[Di bawah adalah maklumat berkenaan dengan tatasusuna MARKS.]

Array : MARKSColumn index : 1:20Row index : 1:4Base Address : 20000Size of memory location : 4 bytes

i) State the dimension of array MARKS.[Nyatakan dimensi bagi tatasusunan MARKS.]

Answer : ____________________________________________

[1 Marks/Markah]

ii) Calculate the number of elements array MARKS. [Kirakan bilangan elemen tatasusunan MARKS.]

[2 Marks/Markah]

Exercise 1 – EKT224Lecturer : Salina Mohd AsiDate release : 27 Sept 2016

iii) Calculate the address of MARKS[2,3]. [Kirakan alamat bagi element MARKS[2,3] bagi tatasusunan ARRAY2] [4 Marks/Markah]

iv) If there is another array immediately after array MARKS, calculate the base address for the array.[Jika terdapat satu lagi tatasusunan selepas tatasusuna MARKS, kirakan alamat asas bagi tatasusunan tersebut.]

[3 Marks/Markah]

Exercise 1 – EKT224Lecturer : Salina Mohd AsiDate release : 27 Sept 2016

Question 5

Figure 2 shows the output of a program to print addresses of elements from two arrays on acomputer using GNU C compiler. The first array is a one-dimensional array, ARRAY1[0:9] and thesecond array is a two-dimensional array, ARRAY2[0:3, 0:4]. Both arrays are declared as INTEGER.[Rajah 2 menunjukkan hasil keluaran bagi satu program C bagi memaparkan alamat bagi elemen-elemen daripadadua (2) tatasusunan dengan menggunakan kompiler GNU C. Tatasusunan yang pertama adalah tatasusunan satu-dimensi, ARRAY1[0:9] dan tatasusunan yang kedua adalah tatasusunan 2-diMensi, ARRAY2[0:3, 0:4]. Kedua-duatatasusunan diistiharkan sebagai jenis data INTEGER. ]

i) What is the base address for ARRAY1 and ARRAY2.[Apakah alamat dasar bagi tatasusunan ARRAY1 dan ARRAY2]

[2 Marks/Markah]ii) What is the size of the data type INTEGER for this situation.

[Apakah saiz bagi jenis data INTEGER bagi situasi ini] [2 Marks/Markah]

iii) What is the address of ARRAY2[2,3]. Show your calculation.[Apakah alamat bagi element ARRAY2[2,3] bagi tatasusunan ARRAY2] [4 Marks/Markah]

iv) If another array, ARRAY3 to be declared next, predict it's base address.[Jika satu lagi tatasusunan, ARRAY3 diistiharkan, ramalkan alamat dasar bagi tatasusunan ARRAY3]

[2 Marks/Markah]

Print address for array1Element[0]: 915063680Element[1]: 915063684Element[2]: 915063688Element[3]: 915063692Element[4]: 915063696Element[5]: 915063700Element[6]: 915063704Element[7]: 915063708Element[8]: 915063712Element[9]: 915063716===================================Print address for array2Element[0][0]: 915063728Element[0][1]: 915063732Element[0][2]: 915063736Element[0][3]: 915063740Element[0][4]: 915063744Element[1][0]: 915063748Element[1][1]: 915063752Element[1][2]: 915063756Element[1][3]: 915063760Element[1][4]: 915063764Element[2][0]: 915063768Element[2][1]: 915063772Element[2][2]: 915063776Element[2][3]: 915063780Element[2][4]: 915063784Element[3][0]: 915063788Element[3][1]: 915063792Element[3][2]: 915063796Element[3][3]: 915063800Element[3][4]: 915063804==================================

Figure. 2.[Rajah 2]

Exercise 1 – EKT224Lecturer : Salina Mohd AsiDate release : 27 Sept 2016

Let's discuss this,

Exercise 1 – EKT224Lecturer : Salina Mohd AsiDate release : 27 Sept 2016

Exercise 1 – EKT224Lecturer : Salina Mohd AsiDate release : 27 Sept 2016

Question 6

Study the code segment below. What is the output produced by the code segment for a stack S.PUSH(S,X) is a procedure to insert an item into a stack, POP(S,X) is a procedure to delete from astack, PRINT(X) is procedure to print output of item X, EMPTY_STACK(S) is a procedure tocheck whether the stack is empty or not and CLEAR(S) is procedure to empty the content of the astack. Update table 2 after each execution of the code segment.

[Kaji keratan aturcara di bawah. Apakah output yang dihasilkan oleh keratan aturcara tersebut untuk timbunan.PUSH(S,X) adalah fungsi untuk memasukkan item kedalam timbunan, POP(S,X) adalah fungsi membuang itemdaripada timbunan, PRINT(X) adalah fungsi untuk mencetak output X, EMPTY_STACK(S) adalah fungsi untukmenyemak bahawa timbunan kosong atau tidak dan CLEAR(S) adalah mengosongkan timbunan. Kemaskinikanmaklumat pada jadual 2 selepas setiap arahan pada keratan aturcara dilaksanakan.]

1. Status = 32. CLEAR(S)3. repeat4. if Status <= 125. PUSH(S,Status)6. Status = 2 * Status7. else8. POP(S, Status)9. PRINT(Status)10. Status = 3* Status + 211. until (EMPTY_STACK(S) and Status > 15)

Table 2Jadual 2

Step Content of Stack Status Output Display

Exercise 1 – EKT224Lecturer : Salina Mohd AsiDate release : 27 Sept 2016

[10 Marks/Markah]

Exercise 1 – EKT224Lecturer : Salina Mohd AsiDate release : 27 Sept 2016

Question 7

i) Transform the infix below into the postfix expression. [Tukarkan penyataan infix di bawah kepada penyataan postfix].

A + B^C + D/B

[3 Marks/Markah]

ii) Evaluate the postfix expression using linked stack data structure where A=1, B=2, C=3 dan D=4. Show the evaluation process using Table 2.

[Nilaikan penyataan postfix yang terhasil dengan menggunakan struktur data linked stack di mana A=1, B=2, C=3dan D=4. Tunjukkan prosess penilaian tersebut dengan menggunakan Jadual 2].

Table 2Jadual 2

Ch Stack Action

[7 Marks/Markah]