280 likes | 416 Views
CS1010E Programming Methodology Tutorial 7 Arrays and Matrices. C14,A15,D11,C08,C11,A02. Question 1 (a). Reverse a portion of a list void reverse( int list[], int size,int start,int n); Algorithm: find start and end exchange list[start] and list[end]
E N D
CS1010E Programming MethodologyTutorial 7Arrays and Matrices C14,A15,D11,C08,C11,A02
Question 1 (a) • Reverse a portion of a list • void reverse(int list[],intsize,intstart,int n); • Algorithm: • find start and end • exchange list[start] and list[end] • move start to the right and list to the left • stop when start >= end List: Reverse(list, 8, 2, 4)
Question 1 (a) • Reverse a portion of a list • void reverse(int list[],intsize,intstart,int n); • Algorithm: • find start and end • exchange list[start] and list[end] • move start to the right and list to the left • stop when start >= end List: Start =2 end = start + n -1 = 5 Reverse(list, 8, 2, 4)
Question 1 (a) • Reverse a portion of a list • void reverse(int list[],intsize,intstart,int n); • Algorithm: • find start and end • exchange list[start] and list[end] • move start to the right and list to the left • stop when start >= end List: start end Reverse(list, 8, 2, 4)
Question 1 (a) • Reverse a portion of a list • void reverse(int list[],intsize,intstart,int n); • Algorithm: • find start and end • exchange list[start] and list[end] • move start to the right and list to the left • stop when start >= end List: start end Start >= end, stop! Reverse(list, 8, 2, 4)
Question 1 (a) void reverse(int list[],int size,int start,int n){ int end = start + n -1; inttmp; 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[],intsize,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 rightshiftalgorithm from last tutorial list Merge list2 rightshift this range insert(list,5,list2,4,2)
Question 1 (b) • int insert(int list[],intsize,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 rightshiftalgorithm from last tutorial list Merge list2 rightshift this range insert(list,5,list2,4,2)
Question 1 (b) • int insert(int list[],intsize,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 rightshiftalgorithm from last tutorial list Merge list2 rightshift this range insert(list,5,list2,4,2)
Question 1 (b) • int insert(int list[],intsize,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 rightshiftalgorithm from last tutorial list Merge list2 rightshift this range insert(list,5,list2,4,2)
Question 1 (b) • int insert(int list[],intsize,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 rightshiftalgorithm from last tutorial list Merge list2 done insert(list,5,list2,4,2)
Question 1 (b) • int insert(int list[],intsize,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 listcopyfrom last tutorial list Move elements list2 insert(list,5,list2,4,2)
Question 1 (b) • int insert(int list[],intsize,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 listcopyfrom last tutorial list Move elements list2 insert(list,5,list2,4,2) listcopy(list2, list + 2, 4)
Question 1 (c) • Delete a rang of element within a list • int delete(int list[],intsize,intindex,int n); • Algorithm: • shift element from list[index+n, size-1] to left • list[i-n] = list[i]; list to be deleted [4, 8] to be shift left i-3 =1 i=4 delete(1,3)
Question 1 (c) • Delete a rang of element within a list • int delete(int list[],intsize,intindex,int n); • Algorithm: • shift element from list[index+n, size-1] to left • list[i-n] = list[i]; list to be deleted [4, 8] to be shift left i-3 =2 i=5 delete(1,3)
Question 1 (c) • Delete a rang of element within a list • int delete(int list[],intsize,intindex,int n); • Algorithm: • shift element from list[index+n, size-1] to left • list[i-n] = list[i]; list to be deleted [4, 8] to be shift left i-3 =3 i=6 delete(1,3)
Question 1 (c) • Delete a rang of element within a list • int delete(int list[],intsize,intindex,int n); • Algorithm: • shift element from list[index+n, size-1] to left • list[i-n] = list[i]; list to be deleted [4, 8] to be shift left i-3 =4 i=7 delete(1,3)
Question 1 (c) • Delete a rang of element within a list • int delete(int list[],intsize,intindex,int n); • Algorithm: • shift element from list[index+n, size-1] to left • list[i-n] = list[i]; list to be deleted [4, 8] to be shift left i-3 =5 i=8 Done! delete(1,3)
Question 2 • Score is represented by a 2-D array (Matrix) • let’s call the matrix as student[][3] • student[i] • is a 1-D array of length 3 • Two problems: • Reading in matrix • Sorting matrix based on Mark student[3] student[9]
Question 2 • Reading in matrix int students[NUM_STUDENTS][3]; intmatric, 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]= array[position]; array[position]= swap;}} 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; } } } The difference between 1-D and 2-D is in: Compare SWAP Bubble sort for 1-D
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 inttmp[2]; student j tmp[0][0]=student[j][0]; tmp[0][1]=student[j][1] temp
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 student[j][0] = student[i][0]; student[j][1] = student[i][1]; student j temp
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 student j student[i][0] = tmp[0]; student[i][1] = tmp[1]; temp
Question 2 • Sorting the matrix: • 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]; • } • } • } 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 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];
Thank you See you next week!!