CS1010E Programming Methodology Tutorial 7 Arrays and Matrices C14,A15,D11,C08,C11,A02.
-
Upload
willa-richardson -
Category
Documents
-
view
242 -
download
0
description
Transcript of CS1010E Programming Methodology Tutorial 7 Arrays and Matrices C14,A15,D11,C08,C11,A02.
CS1010E Programming MethodologyTutorial 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 end2. exchange list[start] and list[end]3. move start to the right and list to the left4. stop when start >= end
1 2 3 4 5 6 7 8List:
Reverse(list, 8, 2, 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 end2. exchange list[start] and list[end]3. move start to the right and list to the left4. stop when start >= end
1 2 3 4 5 6 7 8List:
Reverse(list, 8, 2, 4)Start =2 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 end2. exchange list[start] and list[end]3. move start to the right and list to the left4. stop when start >= end
1 2 6 4 5 3 7 8List:
Reverse(list, 8, 2, 4)start end
Question 1 (a)
Reverse a portion of a list void reverse(int list[],int size,int start,int n); Algorithm:
1. find start and end2. exchange list[start] and list[end]3. move start to the right and list to the left4. stop when start >= end
1 2 6 5 4 3 7 8List:
Reverse(list, 8, 2, 4)start end 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
1 2 3 4 5
6 7 8 9
insert(list,5,list2,4,2)
list
list21 2 3 4 5 6 7 8 9Merge
rightshift this range
Question 1 (b)
1 2 3 4 5
6 7 8 9
insert(list,5,list2,4,2)
list
list21 2 6 3 4 5 7 8 9Merge
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
Question 1 (b)
1 2 3 4 5
6 7 8 9
insert(list,5,list2,4,2)
list
list21 2 6 7 3 4 5 8 9Merge
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
Question 1 (b)
1 2 3 4 5
6 7 8 9
insert(list,5,list2,4,2)
list
list21 2 6 7 8 3 4 5 9Merge
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
Question 1 (b)
1 2 3 4 5
6 7 8 9
insert(list,5,list2,4,2)
list
list21 2 6 7 8 9 3 4 5Merge
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
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
1 2 3 4 5 0 0 0 0
6 7 8 9
insert(list,5,list2,4,2)
list
list2
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
1 2 3 4 5 0 0 0 0
6 7 8 9
insert(list,5,list2,4,2)
list
list2 1 2 6 7 8 9 3 4 5
listcopy(list2, list + 2, 4)
1 2 3 4 5 0 3 4 5
Move elements
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];
1 2 3 4 5 6 7 8 9list
delete(1,3)
1 5 3 4 5 6 7 8 9
to be deleted [4, 8] to be shift lefti=4i-3 =1
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];
1 2 3 4 5 6 7 8 9list
delete(1,3)
1 5 6 4 5 6 7 8 9
to be deleted [4, 8] to be shift lefti=5i-3 =2
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];
1 2 3 4 5 6 7 8 9list
delete(1,3)
1 5 6 7 5 6 7 8 9
to be deleted [4, 8] to be shift lefti=6i-3 =3
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];
1 2 3 4 5 6 7 8 9list
delete(1,3)
1 5 6 7 8 6 7 8 9
to be deleted [4, 8] to be shift lefti=7i-3 =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];
1 2 3 4 5 6 7 8 9list
delete(1,3)
1 5 6 7 8 9 7 8 9
to be deleted [4, 8] to be shift lefti=8i-3 =5 Done!
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 matrix2. Sorting matrix based on Mark
ID Mark Grade196 46 ?179 92 ?158 56 ?198 32 ?146 31 ?165 73 ?134 67 ?
118 54 ?159 55 ?136 59 ?171 34 ?137 98 ?154 76 ?175 57 ?157 76 ?188 57 ?106 37 ?135 70 ?151 47 ?164 33 ?112 59 ?186 80 ?173 73 ?189 9 ?
student[3]
student[9]
Question 2
Reading in matrixint 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;
}
Question 2
Sorting Matrix by Mark (student[i][1])
Similar to 1-D sorting
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; } }
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:CompareSWAP
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
198 32 ?
136 59 ?
student i
student j
temptmp[0][0]=student[j][0]; tmp[0][1]=student[j][1]
int tmp[2];
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
198 32 ?
136 59 ?
student i
student j
136 59temp
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
136 59 ?
198 32 ?
student i
student j
136 59tempstudent[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; }
} }
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 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!!