Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 8 Array typical problems, Search, Sorting Department of Computer Engineering
Outline Array typical problems Search Sorting Department of Computer Engineering 2/73
Find Maximum Find maximum value in data array Department of Computer Engineering 3/73
Find Average Department of Computer Engineering 4/73
Number of elements greater than average After finding the average as shown in previous slide, use the following code Department of Computer Engineering 5/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 } } } Department of Computer Engineering 6/73... pair[0]=12 pair[1]=20 pair[49]=15
solution Department of Computer Engineering 7/73
Randomly re-shuffle numbers 30 times data[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 Department of Computer Engineering 8/73
solution Department of Computer Engineering 9/73
Reverse an array 0 1 2 3 4 5 6 3 1 9 7 2 2 7 9 1 3 6 Department of Computer Engineering 10/73
Reverse an Array Department of Computer Engineering 11/73
Print sum of top-bottom pairs A[0]=3 A[1]=6 A[49]=5 A[50]=3 + + +. A[98]=4 A[99]=5 Department of Computer Engineering 12/73
Group avg Suppose we have a sorted array of hundred grades. Grade[0] } Grade[1] We want to find the average of. Grade[9] top ten, second top ten students etc. Grade[10] Grade[19] Grade[20] } Grade[90]. Grade[99] } Department of Computer Engineering 13/73
Initializing Variable Length arrays Department of Computer Engineering 14/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 = (1.0 * sum) / SIZE; for(i = 0; i < SIZE; i++) if(number[i] >= average) large_size++; } else small_size++; printf("average = %f\n", average); printf("small Size = %d, Large Size = %d\n", small_size, large_size); Department of Computer Engineering 15/73
#include <stdio.h> void main(void){ } int n; printf("enter n: ", n); scanf("%d", &n); int number[n]; double average; int sum, large_size, small_size, i; sum = large_size = small_size = 0; for(i = 0; i < n; i++) for(i = sum average for(i = scanf("%d", &(number[i])); 0; i < n; i++) += number[i]; = (1.0 * sum) / n; 0; i < n; i++) if(number[i] >= average) else large_size++; small_size++; printf("average = %f\n", average); printf("small Size = %d, Larg Size = %d\n", small_size, large_size); برنامهایبنویسیدکهتعداداعدادویکرشتهاز ورودیدریافتکندوتعداداعدادکوچکترو بزرگترازمیانگینراچاپکند. Department of Computer Engineering 16/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 Department of Computer Engineering 17/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 Department of Computer Engineering 18/73
Arrays in Functions Department of Computer Engineering 19/73
Exercise a[0]=3 a[1]=5 c=? 8 b= n=2 i=0 1 2 sum=0 3 8 Department of Computer Engineering 20/73
Exercise a[0]=3 20 a[1]=5 c=? 8 b= n=2 i=0 1 2 sum=0 3 8 Department of Computer Engineering 21/73
تابعیکهیکآرایهبهطول 10 رامیگیردو اعضایآنرابااعداد 0 تا 9 مقداردهی میکند. Department of Computer Engineering 22/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 Department of Computer Engineering 23/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 Department of Computer Engineering 24/73
تابعیبنویسیدکهیکآرایهرادریافتو محلبزرگترینعنصرآنرابازگرداند. Department of Computer Engineering 25/73
Liner search Binary search Search function Department of Computer Engineering 26/73
Unordered list linear search Department of Computer Engineering 27/73
Ordered list linear search Department of Computer Engineering 28/73
Binary Search Key = 76 Department of Computer Engineering 29/73
Binary Search Department of Computer Engineering 30/73
Selection Sort Bubble Sort Sort function Department of Computer Engineering 31/73
Selection Sort Department of Computer Engineering 32/73
Implementation Department of Computer Engineering 33/73
34 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 Department of Computer Engineering 34/73
Bubble Sort Department of Computer Engineering 35/73
Bubble Sort Department of Computer Engineering 36/73
Bubble Sort Department of Computer Engineering 37/73
Bubble Sort Department of Computer Engineering 38/73
Bubble Sort Department of Computer Engineering 39/73
Bubble Sort Department of Computer Engineering 40/73
Bubble Sort Department of Computer Engineering 41/73
Bubble Sort Department of Computer Engineering 42/73
Bubble Sort Department of Computer Engineering 43/73
Bubble Sort Department of Computer Engineering 44/73
Bubble Sort Department of Computer Engineering 45/73
Bubble Sort Department of Computer Engineering 46/73
Bubble Sort Department of Computer Engineering 47/73
Bubble Sort Department of Computer Engineering 48/73
Bubble Sort Department of Computer Engineering 49/73
Bubble Sort Department of Computer Engineering 50/73
Bubble Sort Department of Computer Engineering 51/73
Bubble Sort Department of Computer Engineering 52/73
Bubble Sort Department of Computer Engineering 53/73
Bubble Sort Department of Computer Engineering 54/73
Bubble Sort Department of Computer Engineering 55/73
Bubble Sort Department of Computer Engineering 56/73
Bubble Sort Department of Computer Engineering 57/73
Implementation Department of Computer Engineering 58/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} Department of Computer Engineering 59/73
Solution Department of Computer Engineering 60/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 1 7 2 4 5 8 Department of Computer Engineering 61/73
Solution 6 3 1 9 7 2 4 2 5 6 1 8 6 1 2 Department of Computer Engineering 62/73
Multidimensional Arrays in Functions Can be used as input of functions All 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 Department of Computer Engineering 63/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" Department of Computer Engineering 64/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]){ int i, j; for(i = 0; i < SIZE; i++) for(j = i; j < SIZE; j++) } swap(a, i, j); Department of Computer Engineering 65/73
2-Dim Arrays as Arguments to Functions void print_m(int m[3][4], int r, int c) 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); Department of Computer Engineering 66/73 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; }
Matrix sum Compute the addition of two matrices 0 1 2 3 0 1 2 3 0 1 2 3 0 0 1 0 2 0 3-1 3 1 0 3 0 3 3 1-1 2 4 3 + 1 1 4 2 0 = 1 0 6 6 3 2 0-1 3 1 2 2 1 1 3 2 2 0 4 4 Department of Computer Engineering 67/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]; Department of Computer Engineering 68/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 Department of Computer Engineering 69/73
Transpose void transpose(int a[nrows][ncols], int b[ncols][nrows]) { /* Declare Variables. */ } int i, j; /* Transfer values to the transpose matrix. */ for(i=0; i<nrows; i++) { for(j=0; j<ncols; j++) { b[j][i] = a[i][j]; } } return; a 1 5 3 4 2 6 b 1 4 5 2 3 6 Department of Computer Engineering 70/73
Matrix multiplication double a[3][2], b[2][4], c[3][4]; Find c = a * b; 3 4 5 2 1 6 2 3 7 1 x = 4 5 6 8 22 29 45 35 18 40 47 21 26 33 43 49 3*2 + 4*4=22 3*3 + 4*5=29 3*7 + 4*6=45 3*1 + 4*8=35 5*2 + 2*4=18 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 Department of Computer Engineering 71/73
Matrix Multiplication i 0 1 2 i=0 3 4 5 2 1 6 3 4 k 2 3 7 1 x = 4 5 6 8 x 2 4 Department of Computer Engineering 72/73 j 0 1 2 3 j=0 k = i 0 1 2 j 0 1 2 3 22 29 45 35 18 40 47 21 26 33 43 49 c[i][j] = a[i][k=0]*b[k=0][j] + a[i][k=1]*b[k=1][j]
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; } Department of Computer Engineering 73/73