1 / 10

Lecture 14: Problems with Lots of Similar Data

Lecture 14: Problems with Lots of Similar Data. memory. …. Name of the array. a[0]. 5. a[1]. 10. a[2]. 15. a[3]. 3. Specifying the type of each element. The number of the elements. a[4]. 23. …. What is an Array?.

haile
Download Presentation

Lecture 14: Problems with Lots of Similar Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 14: Problems with Lots of Similar Data

  2. memory … Name of the array a[0] 5 a[1] 10 a[2] 15 a[3] 3 Specifying the type of each element The number of the elements a[4] 23 … What is an Array? • An array is a data structure consisting of related data items of the same type. • Stored in a group of memory locations • Defining arrays int arrayName[ 100 ]; • Examples int a[5]; float b[120], x[24];

  3. Not a variable, cannot assign it a value in an executable statement. What is an Array? • Defining arrays (cont.) • Using a symbolic constant for the size of an array. • Making programs more scalable. #define SIZE 100 int a[ SIZE ]; float b[ SIZE ]; • Using only uppercase letters for symbolic constant names.

  4. Formally called a subscript or an index memory Name of array (all elements of this array have the same name a) … a[0] 5 a[1] 10 a[2] 15 a[3] 3 a[4] 23 … subscript of the element within array a Referring to Array Elements • Format arrayName[ position number ] • First element at position 0 • The i-th element of array a is referred to as a[i-1] • A subscript must be an integer or an integer expression. Ex. (k = 2, j = 1) a[k+j] += 12; • The expression is evaluated to determine the subscript.

  5. memory … a[0] 5 a[1] 10 a[2] 15 a[3] 3 a[4] 23 … Referring to Array Elements • An array a of size of n has the following elements: a[0], a[1], a[2], …, a[n-1] • Array elements are like normal variables printf(“%d”, a[0]*a[3]); x = (a[2]+a[3])/2; • For a defined array without initialization, the values of the elements are undefined. • Avoid to referring to an element outside the array bounds. C has NO array bounds checking to prevent the computer from referring to an element that does not exist.

  6. Initializing the Array • Using a loop to initialize the array’s elements • Initialized to a uniform value • Initialized to different values - using scanf • Initializing an array in a definition with an initializer list • Defining an array followed by an equals sign and braces, { }, containing a comma-separated list of initializers. int n[5] = {1, 2, 3, 4, 5}; • If not enough initializers, rightmost elements become 0. int n[5] = {1, 2} int n[5] = {0} ---all elements are 0. • If too many initializers, a syntax error occurs • If size omitted, # of initializers determine the size intn[ ] = {1, 2, 3, 4, 5, 6}; • 6 initializers, therefore 6 element array.

  7. Practice Question Q. What is the output of the following program? #include<stdio.h> int aFunc( int ); int x = 1; /* global variable */ int main( void ) { int y = 0; x = 10; y = aFunc( x ); printf(”%d %d\n", x, y); return0; } int aFunc( int x) { x *= 10; return x; } 100 100 10 100 10 10 100 10 Solution: B

  8. Practice Question Q. What is the output of the following code fragment? #define SIZE 5 int aray[SIZE] = {2, 3, 4}; printf(“%d\n”, aray[1] + aray[2] + aray[3]); 2 5 9 7 Solution: D

  9. Practice Question #define SIZE 5 int aray[SIZE] = {2, 3, 4, 5}; Q. What is the value of aray[5]? 0 4 Not sure 5 Solution: C Q. What is the value of aray[1]? 2 0 Not sure 3 Solution: D

  10. Practice Question Q. To initialize an array to be {1, 2, 3, 4, 0}, which of the following is WRONG? int ary[ ] = {1, 2, 3, 4, 0}; int ary[5] = {0}; int k; for (k = 0; k < 4; k++) ary[k] = k+1; int ary[5]; ary = {1, 2, 3, 4, 0}; int ary[5] = {1, 2, 3, 4}; Solution: C

More Related