Date post: | 13-Dec-2015 |
Category: |
Documents |
Upload: | dulcie-copeland |
View: | 214 times |
Download: | 0 times |
010.133 Digital Computer Concept and PracticeCopyright ©2012 by Jaejin Lee
C Language Part 3C Language Part 3
010.133 Digital Computer Concept and PracticeCopyright ©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 elementsFor example, int a[10]; declares a to be an array of 10 integers
010.133 Digital Computer Concept and PracticeCopyright ©2012 by Jaejin Lee
Arrays (contd.)
To access an element of an array, write the array name followed by a subscriptIn C, subscripts always start with 0 For example, the elements of array a are a[0], a[1], … , a[9]
010.133 Digital Computer Concept and PracticeCopyright ©2012 by Jaejin Lee
Arrays (contd.)
An array is initialized by listing the valuesIf 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’ }
010.133 Digital Computer Concept and PracticeCopyright ©2012 by Jaejin Lee
Finding MaximumThe program below reads ten values into an array ab and then finds the maximum among the values#include <stdio.h>
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;}
010.133 Digital Computer Concept and PracticeCopyright ©2012 by Jaejin Lee
Bubble SortSorts array ab of n elements in non-decreasing orderThe 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]
010.133 Digital Computer Concept and PracticeCopyright ©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;
}
010.133 Digital Computer Concept and PracticeCopyright ©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
010.133 Digital Computer Concept and PracticeCopyright ©2012 by Jaejin Lee
#include <stdio.h>
#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.)
010.133 Digital Computer Concept and PracticeCopyright ©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
-3 5 9 11 23 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.)
010.133 Digital Computer Concept and PracticeCopyright ©2012 by Jaejin Lee
Search
Suppose that n elements are stored in an array abGiven 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
010.133 Digital Computer Concept and PracticeCopyright ©2012 by Jaejin Lee
Linear Search#include <stdio.h>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;}
010.133 Digital Computer Concept and PracticeCopyright ©2012 by Jaejin Lee
Binary SearchIf the elements in array ab are stored in non-decreasing order after sorting, we can solve the search problem more efficiently than linear searchWe 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
010.133 Digital Computer Concept and PracticeCopyright ©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;
010.133 Digital Computer Concept and PracticeCopyright ©2012 by Jaejin Lee
Two-dimensional Arrays
Two dimensional arrays can be visualized as a multicolumn table or gridint 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}};
010.133 Digital Computer Concept and PracticeCopyright ©2012 by Jaejin Lee
Math.hThe library <math.h> contains mathematical functions (x is of type double, and all functions return double)sqrt(x) square root of xexp(x) exponential function exlog(x) natural logarithm ln(x)log10(x) log10(x)sin(x) sine of xcos(x) cosine of xtan(x) tangent of x