Download presentation
Presentation is loading. Please wait.
1
1 Lecture 9 Arrays Declaration Initialization Applications Pointers Declaration The & and * operators NULL pointer Initialization Readings: Chapter 9, Section 1-8, Chapter 8, Section 1-4
2
2 What is an array? A sequence of variables of the same data type Array elements are indexed and can be accessed by the use of subscripts Array elements are stored contiguously in memory space C supports the use of multi-dimensional arrays int student_num[100]; int subject[3]; int mark[100][3]; 99 98 : 4 3 2 1 0
3
3 1-D Array int subject[3]; Number of elements Variable name Data type of elements subject[0] subject[1] subject[2] Memory Address 1024 1028 1032
4
4 2-D Array int mark[100][3]; Variable name Data type of elements Number of rows How many elements we have? mark[0][0] mark[1][0] mark[2][0] mark[3][0] mark[0][1] mark[1][1] mark[2][1] mark[3][1] mark[0][2] mark[1][2] mark[2][2] mark[3][2] mark[99][0]mark[99][1]mark[99][2] Number of columns
5
5 Memory View of 2D Array int mark[100][3]; Number of rows mark[0][0] mark[1][0] mark[2][0] mark[99][2] mark[0][1] mark[1][1] mark[2][1] mark[99][0] mark[0][2] mark[1][2] mark[2][2] mark[99][1] In memory, the array elements are stored contiguously Memory Number of columns
6
6 Important remarks on array In an array definition, the size of the array is specified by a constant expression within the square brackets. That value must be a positive integer value. The indexing of array elements always starts at 0; thus, the legal values for the index of student_num is from 0 to 99
7
7 Array initialization An array initializer is a sequence of initializing values separated by commas and enclosed with a pair of braces, e.g., int score[5]={27, 31, 25, 50, 70}; If you list fewer values than the array size, the remaining array elements will be initialized to zero int score[5]={27, 31, 25, 50}; // score[4]=0;
8
8 Arrays without initializers and other local variables (including those declared inside the main function) are not initialized If you initialize an array when it is declared, you can omit the size, e.g., int mark[]={34, 87, 72, 65}; is equivalent to int mark[4]={34, 87, 72, 65};
9
9 Searching an array Task: Write a program to read in values for an integer array a[0..N-1] (where N is a symbolic constant) and to search for an integer x in a[0..N-1] Inputs: the values for a[0..N-1], and the target x. Output: If x is present in a[0..N-1], output an i such that a[i]=x If x occurs more than once, output any index i such that a[i]=x If x is not present in a[0..N-1], output -1
10
10 Idea (sequential search): Step through the array from the start to the end, i.e., have an index i goes from 0 to N-1 ; return the value of i as soon as a[i]=x ; if x is not found, return -1 Testing the program: What happens if a[0..N-1] contains the value x more than once? Can you avoid using the break statement? Can you modify the program so that you don ’ t need to type in inputs for a[0..N-1] all the time during debugging?
11
11 #include #define N 10 void main() { int a[N], i, x, position; for (i=0; i<N; i++) scanf(“%d”, &a[i]); printf(“Input your target: “); scanf(“%d”, &x); for (i=0, position=-1; i<N; i++) if (a[i]==x) { position=i; break; } printf(“Target found at position %d\n”, position); }
12
12 Searching a sorted array When the array is sorted (say, in ascending order), there is a faster method called binary search Idea: Test if x=a[m] where a[m] is the middle element of a[0..n-1], i.e., m = n/2 If x=a[m], we are done If x<a[m], x will not be present in a[m+1..n-1]. So we just need to search for a[0..m-1], i.e., the search range is reduced by half If x>a[m], we just need to search for a[m+1..n-1]
13
13 To search the array a[0..m-1] or a[m+1..n-1], we repeat the same idea by testing x with the middle element of the respective array To search an unsorted array of size n, we need to look up n array elements in the worst case. E.g., when x is not present To search a sorted array of size n using binary search, we need to look up log(n) array elements in the worst case
14
14 #include #define N 8 void main() { int a[N], i, x, position; int lower=0; upper=N-1, m; for (i=0; i<N; i++) scanf(“%d”, &a[i]); printf(“Input your target: “); scanf(“%d”, &x); m=(lower+upper)/2; position=-1; while (lower<=upper && position==-1) { if (a[m]==x) position=m; if (a[m]<x]) lower=m+1; else upper=m-1; m=(lower+upper)/2; } /* while */ printf(“Target found at position %d\n”, position); }
15
15 Sample runs a[] = {10, 20, 30, 40, 50, 60, 70, 80} x = 65 iteration 1: lower=0, upper=7, m=3 iteration 2: lower=4, upper=7, m=5 iteration 3: lower=6, upper=7, m=6 iteration 4: lower=6, upper=5 a[] = {10, 20, 20, 40, 50, 60, 70, 80} x = 20 iteration 1: lower=0, upper=7, m=3 iteration 2: lower=0, upper=2, m=1
16
16 What is a pointer? A pointer is the memory address of a variable More precisely: the address of the first byte of the sequence of memory locations storing a variable E.g., Assume an integer x is stored in address 1000 - 1003; then address 1000 is a pointer to x 1000 1001 1002 1003
17
17 Pointer variable declaration A pointer variable is a variable that can store a pointer Pointers are classified by the type of variables they are pointing to Pointer variable definition: E.g. double *p; declares p to be a pointer variable that can hold a pointer that points to a double variable The compiler reserves space to store the pointer variable; however, no space is reserved to store the double variable pointed to by p
18
18 Pointer variable declaration (Cont ’ ) This pointer variable cannot hold a pointer that points to a variable of different type More examples: int *ptr1, *ptr2, v, w; The variables v and w are not pointer variables We can give a name to a data type using typedef E.g.: typedef int* IntPtr; IntPtr ptr1, ptr2;
19
19 Addressing: The & operators The & operator returns the address of a variable: ptr1 = &v; We say that pointer variable ptr1 points to variable v We call ‘ & ’ the address-of operator E.g. int v = 5, *ptr; ptr = &v; 1000 5 5 ptr 1000v v
20
20 NULL pointer value A special value that can be assigned to any type of pointer variable Is a symbolic constant defined in several standard library headers, e.g., Same as 0 When assigned to a pointer variable, that variable points to nothing
21
21 Dereferencing: The * operators The * operator returns the variable pointed to by a pointer: int v = 5, *ptr; ptr = &v; *ptr = 42; /* v now stores 42 */ The asterisk * is the same as the symbol for multiplication; here it is called the dereferencing operator Dereferencing a 0 pointer usually causes fatal error: int *ptr = 0; *ptr = 42;
22
22 Initializing pointer variables (I) Pointer variables can be initialized to 0, NULL or an address E.g. int *ptr1 = 0; /* same for NULL */ means ptr1 is a pointer to an integer and that initially ptr1 points to nothing. Note: integer 0 is converted to a pointer value of the appropriate type.
23
23 Initializing pointer variables (II) Example: int a, *p = &a; /* note the order */ means p is a pointer to an integer and that the initial value of p is the address of a DO NOT read it as “ the integer pointed to by p is initialized to the address of a ” Although an address is an integer, a pointer cannot be manipulated as an integer
24
24 Operations on pointers Assignment: p = q; Different from: *p = *q; 84 99 Before 84 99 After p q p q 84 99 Before p q 99 After p q
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.