CS1010E Programming Methodology Tutorial 7 Arrays and Matrices Question 1 (a) Reverse a portion of a list void reverse(int list[],int size,int start,int n); Algorithm: 1. find start and end 2. exchange list[start] and list[end] 3. move start to the right and list to the left 4. stop when start >= end List: 1 2 Reverse(list, 8, 2, 4) 3 4 5 6 7 8 Question 1 (a) Reverse a portion of a list void reverse(int list[],int size,int start,int n); Algorithm: 1. find start and end 2. exchange list[start] and list[end] 3. move start to the right and list to the left 4. stop when start >= end List: 1 2 3 4 Start =2 Reverse(list, 8, 2, 4) 5 6 7 8 end = start + n -1 = 5 Question 1 (a) Reverse a portion of a list void reverse(int list[],int size,int start,int n); Algorithm: 1. find start and end 2. exchange list[start] and list[end] 3. move start to the right and list to the left 4. stop when start >= end List: 1 2 Reverse(list, 8, 2, 4) 6 4 start 5 3 end 7 8 Question 1 (a) Reverse a portion of a list void reverse(int list[],int size,int start,int n); Algorithm: 1. find start and end 2. exchange list[start] and list[end] 3. move start to the right and list to the left 4. stop when start >= end List: 1 2 Reverse(list, 8, 2, 4) 6 5 start 4 3 end 7 8 Start >= end, stop! Question 1 (a) void reverse(int list[], int size, int start, int n) { int end = start + n - 1; int tmp; while (start <= end) { tmp = list[start]; list[start] = list[end]; list[end] = tmp; start++; end--; } } Code for question 1 (a) Question 1 (b) int insert(int list[],int size,int list2[],int size2,int index); Algorithm I: for each element list2[i] insert it into list at position index + i insertion can be done by rightshift algorithm from last tutorial list list2 1 2 3 4 5 Merge 6 7 8 1 2 3 4 5 6 7 9 rightshift this range insert(list,5,list2,4,2) 8 9 Question 1 (b) int insert(int list[],int size,int list2[],int size2,int index); Algorithm I: for each element list2[i] insert it into list at position index + i insertion can be done by rightshift algorithm from last tutorial list list2 1 2 3 4 5 Merge 6 7 8 1 2 6 3 4 5 7 8 9 rightshift this range insert(list,5,list2,4,2) 9 Question 1 (b) int insert(int list[],int size,int list2[],int size2,int index); Algorithm I: for each element list2[i] insert it into list at position index + i insertion can be done by rightshift algorithm from last tutorial list list2 1 2 3 4 5 Merge 6 7 8 1 2 6 7 3 4 5 8 9 9 rightshift this range insert(list,5,list2,4,2) Question 1 (b) int insert(int list[],int size,int list2[],int size2,int index); Algorithm I: for each element list2[i] insert it into list at position index + i insertion can be done by rightshift algorithm from last tutorial list list2 1 2 3 4 5 Merge 6 7 8 1 2 6 7 8 3 4 5 9 9 rightshift this range insert(list,5,list2,4,2) Question 1 (b) int insert(int list[],int size,int list2[],int size2,int index); Algorithm I: for each element list2[i] insert it into list at position index + i insertion can be done by rightshift algorithm from last tutorial list list2 1 2 3 4 5 Merge 6 7 8 1 2 6 7 8 9 9 done insert(list,5,list2,4,2) 3 4 5 Question 1 (b) int insert(int list[],int size,int list2[],int size2,int index); Algorithm II: move every element in list[index, size] list[i+size2] = list[i]; insert it into list at position index + I Copy list2 into position use listcopy from last tutorial list 1 2 3 4 5 0 0 0 0 list2 6 7 8 9 insert(list,5,list2,4,2) 1 2 3 4 5 0 3 4 5 Move elements Question 1 (b) int insert(int list[],int size,int list2[],int size2,int index); Algorithm II: move every element in list[index, size] list[i+size2] = list[i]; insert it into list at position index + I Copy list2 into position use listcopy from last tutorial list 1 2 3 4 5 0 0 0 0 1 2 3 4 5 0 3 4 5 Move elements list2 6 7 8 9 insert(list,5,list2,4,2) 1 2 6 7 8 9 3 4 5 listcopy(list2, list + 2, 4) Question 1 (c) Delete a rang of element within a list int delete(int list[],int size,int index,int n); Algorithm: shift element from list[index+n, size-1] to left list[i-n] = list[i]; list 1 2 3 4 to be deleted 5 6 7 8 9 1 5 3 [4, 8] to be shift left i-3 =1 i=4 delete(1,3) 4 5 6 7 8 9 Question 1 (c) Delete a rang of element within a list int delete(int list[],int size,int index,int n); Algorithm: shift element from list[index+n, size-1] to left list[i-n] = list[i]; list 1 2 3 4 to be deleted 5 6 7 8 9 1 5 6 [4, 8] to be shift left i-3 =2 i=5 delete(1,3) 4 5 6 7 8 9 Question 1 (c) Delete a rang of element within a list int delete(int list[],int size,int index,int n); Algorithm: shift element from list[index+n, size-1] to left list[i-n] = list[i]; list 1 2 3 4 to be deleted 5 6 7 8 9 1 5 6 [4, 8] to be shift left i-3 =3 i=6 delete(1,3) 7 5 6 7 8 9 Question 1 (c) Delete a rang of element within a list int delete(int list[],int size,int index,int n); Algorithm: shift element from list[index+n, size-1] to left list[i-n] = list[i]; list 1 2 3 4 to be deleted 5 6 7 8 9 1 5 6 [4, 8] to be shift left i-3 =4 i=7 delete(1,3) 7 8 6 7 8 9 Question 1 (c) Delete a rang of element within a list int delete(int list[],int size,int index,int n); Algorithm: shift element from list[index+n, size-1] to left list[i-n] = list[i]; list 1 2 3 4 to be deleted 5 6 7 8 9 1 5 6 8 9 7 [4, 8] to be shift left i-3 =5 i=8 delete(1,3) 7 Done! 8 9 Question 2 ID Score is represented by a 2-D array (Matrix) let’s call the matrix as student[][3] student[3] student[i] is a 1-D array of length 3 Two problems: 1. Reading in matrix 2. Sorting matrix based on Mark student[9] 196 179 158 198 146 165 134 118 159 136 171 137 154 175 157 188 106 135 151 164 112 186 173 189 Mark 46 92 56 32 31 73 67 54 55 59 34 98 76 57 76 57 37 70 47 33 59 80 73 9 Grade ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Question 2 Reading in matrix int students[NUM_STUDENTS][3]; int matric, mark; for(i = 0; i < NUM_STUDENTS; i++){ //Scans for data, saves the into 2 columns. scanf("%d %d", &matric, &mark); students[i][0] = matric; students[i][1] = mark; } for ( c = 0 ; c < ( n - 1 ) ; c++ ) { position = c; for ( d = c + 1 ; d < n ; d++ ) { if ( array[position] > array[d] ) position = d; } if ( position != c ) { swap = array[c]; array[c] = Question 2 Sorting Matrix by Mark (student[i][1]) Similar to 1-D sorting for (i = 0 ; i < ( n - 1 ); i++) { for (j = 0 ; j < n - i - 1; j++) { if (array[j] > array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } Bubble sort for 1-D The difference between 1-D and 2-D is in: Compare SWAP Question 2 Sorting Matrix by Mark (student[i][1]) Compare two students’ mark student[i][1] < student[j][1] Swap the records of two students student i 198 32 ? student j 136 59 ? temp int tmp[2]; tmp[0][0]=student[j][0]; tmp[0][1]=student[j][1] Question 2 Sorting Matrix by Mark (student[i][1]) Compare two students’ mark student[i][1] < student[j][1] Swap the records of two students student i 198 32 ? student j 136 59 ? 136 59 temp student[j][0] = student[i][0]; student[j][1] = student[i][1]; Question 2 Sorting Matrix by Mark (student[i][1]) Compare two students’ mark student[i][1] < student[j][1] Swap the records of two students student i 136 59 ? student j 198 32 ? temp 136 59 student[i][0] = tmp[0]; student[i][1] = tmp[1]; Question 2 Sorting the matrix: for (i=0;i<(n-1); i++) { for (j=0; j<n-i-1;j++) { if (array[j]>array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } 1-D Bubble Sort for (i=0;i<(n-1); i++) { for (j=0; j<n-i-1;j++) { if (student[j][1]>student[j+1][1]) { temp[0] = array[j][0]; temp[1] = array[j][1]; student[j][0] = student[j+1][0]; student[j][1] = student[j+1][1]; array[j+1][0] = temp[0]; array[j+1][1] = temp[1]; } } } 2-D Bubble Sort Question 3 Summing up surroundings: Problem Solving: Step 1: What is input and what is output? Step 2: How to store the input and output? Store row and column into two variables: width, height Store M*N digits into one 2D array - board[][] Store M*N digits into another 2D array - result[][] Step 3: How to derive the output based on the input? Each cell in result[][] equals the sum of the eight adjacent cells from board[][] Question 3 Algorithm: For each mine[i][j], summing up its surroundings: mine[i-1][j] + mine[i+1][j] + mine[i][j-1] + mine[i][j+1] Write output to a new matrix result[i][j] = mine[i-1][j] + mine[i+1][j] + mine[i][j-1] + mine[i][j+1];