Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 8 Arrays and Strings Introduction to C Programming.

Similar presentations

Presentation on theme: "Unit 8 Arrays and Strings Introduction to C Programming."— Presentation transcript:

1 Unit 8 Arrays and Strings Introduction to C Programming

2 Unit 7 Review Unit 8: Review of Past Material

3 Functions - Review Function Header Return Type Identifier - name of function Parameter List Data type and Identifier for each parameter Comma-separated list, enclosed in parentheses Function Body Compound Statement in braces Must contain a return statement unless return type is void

4 Variable Scope Global variable Declared at top of a file Accessible throughout the compilation Local variable Declared inside a function at top of function body Known only within the function Block scope Declared inside a nested compound statement (block of code) Known only within the block Two or more variables with same name - the one at the smaller scope hides the one(s) at larger scope(s)

5 Pointers Pointer has a data type corresponding to the variable pointed to Pointers are declared using the "*" asterisk character The address of a variable is obtained using the "&" character To use a pointer to access memory, precede it with "*" Used to create output parameters for functions Each output parameter is declared as a pointer The function call must use the address of a variable as the argument Inside the function, the external variable is accessed by pointer

6 Arrays Unit 8: Arrays and Strings

7 Data Structures Group of related data items Stored in one contiguous area of memory Can be accessed and manipulated as a group or unit Record (C language "struct") Combination of data items of various data types Each data item called a "field" in the record Array (C language "array") Multiple data items of matching data type Each data item called an "element" in the array

8 Arrays - Declaration Data type, variable identifier, size in square brackets Size must be a constant expression, not a run-time expression Size must be a positive integer int numbers [ 5 ]; The example above declares an array variable of 5 elements The elements are numbered starting from 0: numbers[0], numbers[1], numbers[2], numbers[3], numbers[4] Notice that the last element is "4", not "5" !

9 Arrays - Access to Array Elements The element is identified by its index (numerical position) Name of array (identifier) followed by subscript Subscript is index in square brackets Index value must be integer expression Examples using the numbers array: value = numbers[10]; value = numbers[i+2]; numbers[4] = value + 3; numbers[0] =

10 Arrays - Caution! Array element numbering (index) starts at 0, not 1 The elements are numbered [0], [1], [2], … [n-1] int numbers[5]; numbers[0], numbers[1], numbers[2], numbers[3], numbers[4] Common Programming Error: Accessing past end of array - logic error numbers[5] C Language does not check for this error

11 Array Initializers Follow array declaration with a list of expressions in braces int numbers[5] = {3, 12, -42, 0, 6}; If a size is specified, and list is too short, zeros are used at end If a size is specified, and list is too long, this is an error If a size is not specified (empty square brackets) The compiler counts the number of initializers The count is used as the size int numbers[] = {3, 12, -42, 0, 6};

12 Array Usage - Parallel Arrays Parallel Arrays - Arrays of related data items Example: A list of 100 rectangles, dimensions length and width Use two arrays of doubles, each size 100 double length[100], width[100]; length[i] = 40; width[i] = 25;

13 Array Usage - Use with Functions Arrays and Functions An element can be passed as an argument to a function area = calcArea(length[3], width[3]); An entire array can be passed as an argument to a function Size not passed automatically, must be an explicit separate parameter f(numbers, 5); An array cannot be returned from a function

14 Multi-Dimension Arrays Arrays can have one to six dimensions A two-dimensional array is like a grid The first dimension is the row, second is the column Specify each size in separate square brackets double grid[50][25]; When accessing, provide separate subscripts grid[r][c] = 42.5;

15 Multi-Dimension Arrays When initializing, initialize one row at a time Initializer is "list of lists" Use braces for the entire list Use braces for list of initializers for each row double xy[4][3] = { {0, 12.2, 14.7}, {2.5, 8.3, -6}, {2.0}, /* "Short" list filled with zeros */ {1E6, 3.2E-1, 47.9} };

16 Searching and Sorting Unit 8: Arrays and Strings

17 Searching Often-used algorithm Goal: Find a value, discover it's index May have result of "not found" Array is list of values, look through the list Is the list unsorted (random order)? Linear Search pp Look through each element from beginning Finds the first occurrence Is the list sorted (sequential order) ? Binary Search Divide list in half Finds one occurrence, not necessarily the first More efficient than linear search (over the long run)

18 Sorting Common, often-used algorithms Many sort algorithms have been developed Simple, but less efficient Complex, faster performance Example: Selection Sort pp Find smallest element (first occurrence) in list [0] … [n-1] Swap smallest element with first element Now, use Selection Sort on remaining list [1] … [n-1] Continue until the "remaining unsorted list" has 1 element

19 String Variables: Arrays of Characters Unit 8: Arrays and Strings

20 Strings Important, commonly-used data type Group of characters treated as a unit Examples of common uses Names of people, places, devices, etc Instructions to user Error messages Web addresses and URLs

21 C Language Strings C does not actually have a string data type A C string uses a character array The string begins with the 0 th element, might not fill array To indicate end of string, a sentinel 0-value character is used The sentinel must always be present char s1[10] = "George"; The format specifier for printf and scanf is "%s" G e o r g e \0

Download ppt "Unit 8 Arrays and Strings Introduction to C Programming."

Similar presentations

Ads by Google