Download presentation
Presentation is loading. Please wait.
Published byWilla Richardson Modified over 9 years ago
1
CS1010E Programming Methodology Tutorial 7 Arrays and Matrices C14,A15,D11,C08,C11,A02
2
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 12345678 List: Reverse(list, 8, 2, 4)
3
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 12345678 List: Reverse(list, 8, 2, 4) Start =2 end = start + n -1 = 5
4
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 12645378 List: Reverse(list, 8, 2, 4) startend
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 12654378 List: Reverse(list, 8, 2, 4) startendStart >= end, stop!
6
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)
7
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 12345 6789 insert(list,5,list2,4,2) list list2 123456789 Merge rightshift this range
8
Question 1 (b) 12345 6789 insert(list,5,list2,4,2) list list2 126345789 Merge rightshift this range 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
9
Question 1 (b) 12345 6789 insert(list,5,list2,4,2) list list2 126734589 Merge rightshift this range 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
10
Question 1 (b) 12345 6789 insert(list,5,list2,4,2) list list2 126783459 Merge rightshift this range 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
11
Question 1 (b) 12345 6789 insert(list,5,list2,4,2) list list2 126789345 Merge done 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
12
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 123450000 6789 insert(list,5,list2,4,2) list list2 123450345 Move elements
13
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 123450000 6789 insert(list,5,list2,4,2) list list2 126789345 listcopy(list2, list + 2, 4) 123450345 Move elements
14
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]; 123456789 list delete(1,3) 153456789 to be deleted[4, 8] to be shift left i=4i-3 =1
15
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]; 123456789 list delete(1,3) 156456789 to be deleted[4, 8] to be shift left i=5i-3 =2
16
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]; 123456789 list delete(1,3) 156756789 to be deleted[4, 8] to be shift left i=6i-3 =3
17
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]; 123456789 list delete(1,3) 156786789 to be deleted[4, 8] to be shift left i=7i-3 =4
18
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]; 123456789 list delete(1,3) 156789789 to be deleted[4, 8] to be shift left i=8i-3 =5 Done!
19
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: 1. Reading in matrix 2. Sorting matrix based on Mark IDMarkGrade 19646 ? 17992 ? 15856 ? 19832 ? 14631 ? 16573 ? 13467 ? 11854? 15955? 13659? 17134? 13798? 15476? 17557? 15776? 18857? 10637? 13570? 15147? 16433? 11259? 18680? 17373? 1899? student[3] student[9]
20
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; }
21
Question 2 Sorting Matrix by Mark (student[i][1]) Similar to 1-D sorting for ( c = 0 ; c array[d] ) position = d; } if ( position != c ) { swap = array[c]; array[c] = array[position]; array[position] = swap; } } 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
22
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 19832 ? 13659? student i student j temp tmp[0][0]=student[j][0]; tmp[0][1]=student[j][1] int tmp[2];
23
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 19832 ? 13659? student i student j 13659 temp student[j][0] = student[i][0]; student[j][1] = student[i][1];
24
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 13659 ? 19832? student i student j 13659 temp student[i][0] = tmp[0]; student[i][1] = tmp[1];
25
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; } 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]; } 1-D Bubble Sort2-D Bubble Sort
26
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[][]
27
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];
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.