Tutorial 6 Array Problem Solving NUS School of Computing CS1010E Programming Methodology CS1010E Tutorial Slides Prepared by Wu Chao
CS1010E Tutorial Slides Prepared by Wu Chao Quick Summary CS1010E Tutorial Slides Prepared by Wu Chao
Q1: Insertion Sort Algorithm CS1010E Tutorial Slides Prepared by Wu Chao
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
Q1: Insertion Sort Algorithm CS1010E Tutorial Slides Prepared by Wu Chao
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
Q1: Insertion Sort Algorithm CS1010E Tutorial Slides Prepared by Wu Chao
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
Q1: Insertion Sort Algorithm CS1010E Tutorial Slides Prepared by Wu Chao
Q2: Matrix Manipulation CS1010E Tutorial Slides Prepared by Wu Chao
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
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
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
Q2: Matrix Manipulation CS1010E Tutorial Slides Prepared by Wu Chao
CS1010E Tutorial Slides Prepared by Wu Chao Q3: Magic Square … CS1010E Tutorial Slides Prepared by Wu Chao
CS1010E Tutorial Slides Prepared by Wu Chao Q3: Magic Square [0][1] [2][2] CS1010E Tutorial Slides Prepared by Wu Chao
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
CS1010E Tutorial Slides Prepared by Wu Chao Q3: Magic Square CS1010E Tutorial Slides Prepared by Wu Chao