Presentation is loading. Please wait.

Presentation is loading. Please wait.

010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.

Similar presentations


Presentation on theme: "010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3."— Presentation transcript:

1 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3

2 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Arrays An array is a data structure containing a certain number of elements, all of which have the same type To declare an array, specify the type and number of the elements For example, int a[10]; declares a to be an array of 10 integers

3 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Arrays (contd.) To access an element of an array, write the array name followed by a subscript In C, subscripts always start with 0 For example, the elements of array a are a[0], a[1], …, a[9]

4 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Arrays (contd.) An array is initialized by listing the values If the number of values is less than that of the array elements, the remaining elements are given value 0 int a[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; int a[10] = {0}; float a[3] = { 0.1, 0.2, 0.0 }; int a[3] = { 3, 4 }; int a[] = { 2, -4, 1 }; int a[4] = { 2, -4, 1 }; char s[] = “abcd”; char s[] = { ‘a’,‘b’,‘c’,‘d’,‘\0’ }

5 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Finding Maximum The program below reads ten values into an array ab and then finds the maximum among the values #include int main(void) { int n, i, max; int ab[10]; printf("Enter n: "); scanf("%d", &n); printf("Enter n numbers: "); for (i=0; i<n; i++) scanf("%d", &ab[i]); max = ab[0]; for (i=1; i<n; i++) if (ab[i] > max) max = ab[i]; printf("max %d\n", max); return 0; }

6 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Bubble Sort Sorts array ab of n elements in non-decreasing order The first iteration of the inner for loop brings the maximum element to the last position, the second iteration brings the second maximum to the second-to-last position, etc. The three assignments inside the if statement exchange the values of ab[j] and ab[j+1]

7 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Bubble Sort (contd.) for (i=1; i<n; i++) for (j=0; j<n-i; j++) if (ab[j] > ab[j+1]) { temp = ab[j]; ab[j] = ab[j+1]; ab[j+1] = temp; }

8 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee void insertionSort( int a[], int n ) { int i, j, val; for( i = 1; i < n; i++){ val = a[i]; j = i - 1; while ( ( j >= 0 ) && ( a[j] > val ) ) { a[j+1] = a[j]; j--; } a[j+1] = val; printIntArray( a, n ); } Insertion Sort

9 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee #include #define N 10 void insertionSort( int *, int ); void printIntArray( int *, int ); int main( void ) { int a[N] = { 23, -3, 5, 9, 11, 33, 87, -7, -24, 50 }; printIntArray( a, N ); insertionSort( a, N ); return 0; } void printIntArray( int a[], int n ) { int i; for( i = 0; i < n; i++) printf("%4d ", a[i]); printf("\n"); } Insertion Sort (contd.)

10 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee 23 -3 5 9 11 33 87 -7 -24 50 -3 23 5 9 11 33 87 -7 -24 50 -3 5 23 9 11 33 87 -7 -24 50 -3 5 9 23 11 33 87 -7 -24 50 -3 5 9 11 23 33 87 -7 -24 50 -7 -3 5 9 11 23 33 87 -24 50 -24 -7 -3 5 9 11 23 33 87 50 -24 -7 -3 5 9 11 23 33 50 87 Insertion Sort (contd.)

11 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Search Suppose that n elements are stored in an array ab Given a new element x, we want to find if x is in array ab x is one of the elements stored in ab An easy solution for search is to scan the elements in array ab one by one and check if it is equal to x Linear search

12 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Linear Search #include int main(void) { int n, i, x; int ab[100]; printf("Enter n: "); scanf("%d", &n); printf("Enter n numbers: "); for (i=0; i<n; i++) scanf("%d", &ab[i]); printf("Enter x: "); scanf("%d", &x); for (i=0; i<n; i++) if (x == ab[i]) { printf("%d\n", i); return 0; } printf("%d\n", -1); return -1; }

13 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Binary Search If the elements in array ab are stored in non- decreasing order after sorting, we can solve the search problem more efficiently than linear search We first compare x with the element in the middle (i.e., median) If x is equal to the median, we have found it If x is smaller than the median, we are sure that x is not in the upper part of array ab, so we look for x in the lower part Otherwise, x is larger than the median, we look for x in the upper part Binary search is faster than linear search

14 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Binary Search (contd.) low = 0; high = n-1; while (low <= high) { mid = (low+high) / 2; if (x < ab[mid]) high = mid – 1; else if (x > ab[mid]) low = mid + 1; else { printf("%d\n", mid); return 0; } printf("%d\n", -1); return -1;

15 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Two-dimensional Arrays Two dimensional arrays can be visualized as a multicolumn table or grid int b[2][5]; Declares a two-dimensional array b that has 2 rows and 5 columns We can initialize a two-dimensional array as follows: int b[2][5] = {{1,0,0,1,1},{0,0,1,1,1}};

16 010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Math.h The library contains mathematical functions (x is of type double, and all functions return double) sqrt(x)square root of x exp(x)exponential function ex log(x)natural logarithm ln(x)log10(x) sin(x)sine of x cos(x)cosine of x tan(x)tangent of x


Download ppt "010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3."

Similar presentations


Ads by Google