Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 14: Problems with Lots of Similar Data

Similar presentations


Presentation on theme: "Lecture 14: Problems with Lots of Similar Data"— Presentation transcript:

1 Lecture 14: Problems with Lots of Similar Data

2 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]; a[0] 5 memory 10 15 3 23 a[1] a[2] a[3] a[4] Name of the array Specifying the type of each element The number of the elements

3 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. Not a variable, cannot assign it a value in an executable statement.

4 Referring to Array Elements
Formally called a subscript or an index 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. Name of array (all elements of this array have the same name a) a[0] 5 memory 10 15 3 23 a[1] a[2] a[3] a[4] subscript of the element within array a

5 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. a[0] 5 memory 10 15 3 23 a[1] a[2] a[3] a[4] 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 int n[ ] = {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); return 0; } int aFunc( int x) x *= 10; return x; 10 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 Q. What is the value of aray[5]? 4 Not sure 5
#define SIZE 5 int aray[SIZE] = {2, 3, 4, 5}; Q. What is the value of aray[5]? 4 Not sure 5 Solution: C Q. What is the value of aray[1]? 2 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


Download ppt "Lecture 14: Problems with Lots of Similar Data"

Similar presentations


Ads by Google