Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 6 Array Problem Solving

Similar presentations


Presentation on theme: "Tutorial 6 Array Problem Solving"— Presentation transcript:

1 Tutorial 6 Array Problem Solving
NUS School of Computing CS1010E Programming Methodology CS1010E Tutorial Slides Prepared by Wu Chao

2 CS1010E Tutorial Slides Prepared by Wu Chao
Quick Summary CS1010E Tutorial Slides Prepared by Wu Chao

3 Q1: Insertion Sort Algorithm
CS1010E Tutorial Slides Prepared by Wu Chao

4 Q1: Insertion Sort Algorithm
7 13 20 38 44 52 88 89 90 92 Starting with index 0 10 If current value is greater or equal, we return this index Otherwise, return last CS1010E Tutorial Slides Prepared by Wu Chao

5 Q1: Insertion Sort Algorithm
CS1010E Tutorial Slides Prepared by Wu Chao

6 Q1: Insertion Sort Algorithm
7 13 20 38 44 52 88 89 90 92 52 13 20 38 44 52 Store the last value 52 Start copying from back Put last into the first CS1010E Tutorial Slides Prepared by Wu Chao

7 Q1: Insertion Sort Algorithm
CS1010E Tutorial Slides Prepared by Wu Chao

8 Q1: Insertion Sort Algorithm
findIndex(a[],1,a[1]); index = 0; shiftRight(a[],0,2); findIndex(a[],2,a[2]); index = 0; shiftRight(a[],0,3); findIndex(a[],3,a[3]); index = 0; shiftRight(a[],0,4); findIndex(a[],4,a[4]); index = 2; shiftRight(a[],2,3); CS1010E Tutorial Slides Prepared by Wu Chao

9 Q1: Insertion Sort Algorithm
CS1010E Tutorial Slides Prepared by Wu Chao

10 Q2: Matrix Manipulation
CS1010E Tutorial Slides Prepared by Wu Chao

11 Q2: Matrix Manipulation
To check if a matrix is Identity Matrix: (1) Left Diagonal must be 1 (2) Others must be 0 Diagonal indices: i == j CS1010E Tutorial Slides Prepared by Wu Chao

12 Q2: Matrix Manipulation
j = 0 1x9 + 2x6 + 3x3 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 1 30 i = 0 = X i = 0 A B C CS1010E Tutorial Slides Prepared by Wu Chao

13 Q2: Matrix Manipulation
You have to iterate through all the cells of the resulting matrix and calculate its value! CS1010E Tutorial Slides Prepared by Wu Chao

14 Q2: Matrix Manipulation
CS1010E Tutorial Slides Prepared by Wu Chao

15 CS1010E Tutorial Slides Prepared by Wu Chao
Q3: Magic Square CS1010E Tutorial Slides Prepared by Wu Chao

16 CS1010E Tutorial Slides Prepared by Wu Chao
Q3: Magic Square [0][1] [2][2] CS1010E Tutorial Slides Prepared by Wu Chao

17 CS1010E Tutorial Slides Prepared by Wu Chao
Q3: Magic Square [0][1] [1][0] [2][0] [2][2] CS1010E Tutorial Slides Prepared by Wu Chao

18 CS1010E Tutorial Slides Prepared by Wu Chao
Q3: Magic Square CS1010E Tutorial Slides Prepared by Wu Chao


Download ppt "Tutorial 6 Array Problem Solving"

Similar presentations


Ads by Google