fundamental of programming...
TRANSCRIPT
Lecturer: Mahdi Soltani
Sharif University of TechnologyDepartment of Computer Engineering
Fundamental of Programming (C)
Lecture 7
Array typical problems, Search, Sorting
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 2/73
Outline• Array typical problems
• Search
• Sorting
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 3/73
Find Maximum• Find maximum value in data array
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 4/73
Find Average
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 5/73
Number of elements greater than average
• After finding the average as shown in previous slide, use the following code
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 6/73
Find pair sum Find sum of every pair in data and write into pair array
data[0]=5
data[1]=7
data[2]=15
data[3]=5
…
…
data[98]=3
data[99]=12
pair[0]=12
pair[1]=20
…
pair[49]=15
}}
}
.
.
.
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 7/73
solution
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 8/73
Randomly re-shuffle numbers 30 timesdata[0]=5
data[1]=7
data[2]=15
data[3]=5
…
…
data[98]=3
data[99]=12
.
.
.
data[0]=12
data[1]=7
data[2]=5
data[3]=15
…
…
data[98]=3
data[99]=5
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 9/73
solution
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 10/73
Reverse an array
6 3 1 9 7 2
0 21 3 4 5
2 7 9 1 3 6
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 11/73
Reverse an Array
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 12/73
Print sum of top-bottom pairsA[0]=3
A[1]=6
…
A[49]=5
A[50]=3
A[98]=4
A[99]=5
+ + +….
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 13/73
Group avg• Suppose we have a sorted array of hundred
grades.
• We want to find the average oftop ten, second top ten students etc.
}Grade[0]
Grade[1]
….
Grade[9]
Grade[10]
…
Grade[19]
Grade[20]
…
Grade[90]
….
Grade[99]
}
}
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 15/73
#include <stdio.h>
#define SIZE 20
void main(void){
int number[SIZE];
double average;
int sum, large_size, small_size, i;
}
sum = large_size = small_size = 0;
for(i = 0; i < SIZE; i++){
int tmp;
scanf("%d", &tmp);
number[i] = tmp;
sum += number[i];}
average
for(i =
= (1.0 * sum) / SIZE;
0; i < SIZE; i++)
= %d\n", small_size, large_size);
برنامهایبنویسیدکهبیستعددازورودیترازدریافتکندوتعداداعدادکوچکتروبزرگ
.میانگینراچاپکند
if(number[i] >= average)
large_size++;
else
small_size++;
printf("average = %f\n", average);
printf("Small Size = %d, Larg Size
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 17/73
Array Elements in Functions
int number[20];
number[i] is an integer
variable
Array element can be used for
call by value input
Array element can be use for
output
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 18/73
Arrays in Functions
Array cannot be used as output type of
function
int [] f(int x, int y); //compile error
Arrays can be used in input list of functions
Arrays are not passed by Call By Value
Arrays are passed by Call By Reference
If we change array elements in a function
The element is changed in the caller function
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 19/73
Arrays in FunctionsSize of array could be different from the real oneBut dangerous things would happen
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 20/73
Exercise
a[0]=3
a[1]=5
c=? 8
b=
n=2
i=0 1 2
sum=0 3 8
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 21/73
Exercise
a[0]=3 20
a[1]=5
c=? 8
b=
n=2
i=0 1 2
sum=0 3 8
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 22/73
وگیردمیرا10بهطولرایهآتابعیکهیکمقداردهی9تا0اعضایآنرابااعداد
.میکند
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 23/73
Array Size in Functions If array is an input parameter of
a function
It cannot find out the size of the
array
Array size should be passed
from caller function to called
function
Using definitions
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 24/73
Array Size in Functions (cont’d) If array is declared in a function It knows the size of the array
It can find out the size of the array using sizeof
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 25/73
توتابعیبنویسیدکهیکآرایهرادریاف.محلبزرگترینعنصرآنرابازگرداند
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 26/73
Search function• Liner search
• Binary search
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 27/73
Unordered list – linear search
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 28/73
Ordered list – linear search
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 29/73
Binary Search
Key = 76
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 30/73
Binary Search
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 31/73
Sort function• Selection Sort
• Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 32/73
Selection Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 33/73
Implementation
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 34/73
Bubble sort • Bubble sort Several passes through the array
– Successive pairs of elements are compared • If increasing order (or identical ), no change
• If decreasing order, elements exchanged
– Repeat
34
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 35/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 36/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 37/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 38/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 39/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 40/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 41/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 42/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 43/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 44/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 45/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 46/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 47/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 48/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 49/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 50/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 51/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 52/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 53/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 54/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 55/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 56/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 57/73
Bubble Sort
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 58/73
Implementation
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 59/73
Merge two sorted array• Assume we have A and B arrays containing
sorted numbers
• For example– A = { 3, 5, 7, 9, 12}
– B = {4, 5, 10}
• Merge these two arrays as a single sorted array C, for example– C = {3, 4, 5, 5, 7, 9, 10, 12}
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 60/73
Solution
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 61/73
Intersection Set • Suppose we have two sets (groups) represented by A
and B
• E.g., A is the set of students taking Math,
B is the set of students taking Science.
• Find set C, the intersection of A and B, i.e., students taking both Math and Science
For each element ID in A
Search that ID in B
if found, put ID into C 3 6 9 17 2
458
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 62/73
Solution
6 3 1 9 7 2
4 2 5 6 1 8
6 1 2
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 63/73
Multidimensional Arrays in Functions
Can be used as input of functionsAll dimensions except the first one must be given
void func(int a[10][20][5]);
Input is a 10x20x5 integer matrix
void func(int a[][20][30], int size); void func(int
size1, int size2, int
a[size1][size2]);
Input is a matrix of integers that both rows and columns are
variable
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 64/73
Multidimensional Arrays in Functions
The first subscript therefore only indicates the amount of storage that is needed when the array is declared
Others are required because the computer needs to know how far along to increment the pointer for each "row"
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 65/73
#define
}
SIZE 5
void swap(int a[SIZE][SIZE], int i, int j){
int tmp;
tmp = a[i][j];
a[i][j]
a[j][i]
= a[j][i];
= tmp;
}
void transpose(int a[][SIZE]){
for(i =
int i, j;
0; i < SIZE; i++)
for(j = i; j < SIZE; j++)
swap(a, i, j);
ماتريس ترانهاده محاسبه
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 67/73
2-Dim Arrays as Arguments to Functions
void print_m(int m[][4], int r, int c)
{
int i,j;
for (i=0; i < r; i++) {
for (j=0; j < c; j++)
printf("%.5d ",m[i][j]);
printf("\n");
}
printf("\n");
return;
}
int i, j, matrix[3][4];
for (i=0; i<3; i++)
for (j=0; j<4; j++)
matrix[i][j] = i;
print_m(matrix, 3, 4);
void print_m(int m[3][4], int r, int c)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 68/73
Matrix sum• Compute the addition of two matrices
3 0 33
0 6 6 3
2 0 4 4
0 1 2 3
0
1
2
0 1 20
-1 2 4 3
0 -1 3 1
0
1
2
0 1 2 3
+
3 -1 13
1 4 2 0
2 1 1 3
0
1
2
0 1 2 3
=
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 69/73
solution
int matrix1[3][4],
matrix2[3][4],
sum[3][4];
// initialize matrix1 and matrix2
for (i=0; i<3; i++)
for (j=0; j<4; j++)
sum[i][j]=
matrix1[i][j]+matrix2[i][j];
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 70/73
Exchange Two Rows
4 6 2
0 5 3
0 8 1
2 1 4
4 6 2
2 1 4
0 8 1
0 5 3
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 71/73
Matrix multiplicationdouble a[3][2], b[2][4], c[3][4];
• Find c = a * b;
x =
3*2 + 4*4=22 3*3 + 4*5=29
2 3 7 1
4 5 6 8
3 4
5 2
1 6
22 29 45 35
18 40 47 21
26 33 43 49
5*2 + 2*4=18
3*7 + 4*6=45 3*1 + 4*8=35
5*3 + 2*5=40 5*7 + 2*6=47 5*1 + 2*8=21
1*2 + 6*4=26 1*3 + 6*5=33 1*7 + 6*6=43 1*1 + 6*8=49
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 72/73
Matrix Multiplication
x =2 3 7 1
4 5 6 8
3 4
5 2
1 6
22 29 45 35
18 40 47 21
26 33 43 49
i=0
i
jj
3 42
4x
j=0
k
k =
c[i][j] =
a[i][k=0]*b[k=0][j] +
a[i][k=1]*b[k=1][j]
0 1 2 3
i
0
1
2
0 1 2 3
0
1
2
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 73/73
Matrix Multiplication cont’d#define N 3
#define M 2
#define L 4
void matrix_mul(a[N][M], int b[M][L], int c[N][L])
{
int i, j, k;
for(i=0; i < N; i++) {
for(j=0; j < L; j++) {
c[i][j] = 0;
for(k=0; k < M; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return;
}