Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input 100...... Example –test scores,employees,temperatures.

Similar presentations


Presentation on theme: "1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input 100...... Example –test scores,employees,temperatures."— Presentation transcript:

1 1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input 100...... Example –test scores,employees,temperatures

2 2 Array structured data type array ‑ group of variables referred to by one name,composed of elements element or member – each item in the array –referred to by one name with an index: a[0],a[1],... index or subscript – references array element

3 3 Array Declaration type name[size]; type - type of each member in array –Can be of any type name - name of the array size - number of members in the array(constant) Dimension each array before it is used, one time only

4 4 Example float arr[20]; float testScores[30]; Or declare size as a constant const int MAX_TESTS = 100; int tests[MAX_TESTS];

5 5 C++ Examples int main() { float days[365]; char code[12]; int buf[100];

6 6 Storage float testScores[10];

7 7 Accessing Array Elements reference array element using subscript or index First element – subscript is 0 Last element – subscript is one less than size –int buf[100]; –buf[0] - first element of buf –buf[99] - last element of buf no checking for "Out of Bounds"

8 8 Valid subscripts can use variables,expression, or constants as subscripts angle[0] = 4.93; angle[1] = -15.2; val[i+2] = val[i] + val[i+1]; x = val[j] cout << score[k] <<score[k+1]<<score[k+2];

9 9 Array elements Each array element is treated exactly as a variable. buf[5] = 30 scores[1] = 95; i = buf[8] * 92; cout << buff[80]; if (a[i] < 14) cout << array [3]; x = y * nums[14]; cin >> sc[3]

10 10 Cannot operate on arrays as unit. The following are invalid: array = 0; cout << scores cin >> array arraya = arrayb;

11 11 Counted Loops and Arrays counted loop - based on reaching a fixed number of repetitions for (initialize;test;increment) for (i = 0; i < 10; i++)...

12 12 To read in an array: const int N = ?; int test[N]; int index; for (index = 0; index < N; index++) { cout << "Enter test “ <<index; cin >> test[index]; }

13 13 Printing an array const int N = ?; int test[N]; int index; for (index = 0; index < N; index++) { cout << test[index]; } Or with a label: cout << "test[“ << index <<"] is “<< test[index];

14 14 To set the entire array to 0: const int N = ?; int test[N]; int index; for (index = 0; index < N; index++) { test[index] = 0; }

15 15 To sum an array: const int N = ?; int test[N]; int index; sum = 0; for (index = 0; index < N; index++) { sum = sum + test[index]; }

16 16 To print an array in reverse order: const int N = ?; int test[N]; int index; for (index = N-1; index >= 0; index--) { cout << test[index]; } Or with a label: cout << "test[“ << index <<"] is “<< test[index];

17 17 To copy one array to another: must have same # of elements int array1[N], array2[N]; for (index = 0; index < N; index++) array1[index] = array2[index].

18 18 Reading any number of values into an array: string name [50]; //must allocate maximum size string tempName; int numNames; numNames = 0; infile >> tempName; while (infile && numNames < 50) { name[numNames] = tempName; numNames = numNames + 1; infile >> tempName; }

19 19 Multidimensional arrays arrays can have any number of dimensions Two dimensional –int hiTemp[52][7]; –Must use two subscripts to access –hiTemp[6][3] = 56.6; Three dimensional –int graph[10][20][30]; etc.

20 20 float table[3][4];

21 21 Stored in row major order table[0][0] table[0][1] table[0][2] table[0][3] table[1][0] table[1][1] table[1][2] table[1][3] table[2][0] table[2][1] table[2][2] table[2][3]

22 22 Initialize int table[10][6]; int row,col; for (row = 0; row < 10; row++) for (col = 0; row < 6; row++) table[row][col] = 0;

23 23 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow Sequential or Linear Search

24 24 int LinearSearch(/*in */ int array[], int size, int key) bool found; int ix; found = false; ix = 0; while (ix < size && !found) { if (array[ix] == key) found = true; else ix = ix + 1 } if (found) return ix; else return -1; }

25 25 BINARY SEARCH requires sorted list – (Ex: phone books, dictionaries). keep dividing list in half, compare against middle argument eliminates one half of the elements after each comparison. efficient

26 26 Simple Binary Search Algorithm: locates the middle element and compares it with the search key, till a match is found or no elements are left for comparison. The array: 1 5 7 11 14 17 19 25 29 37 50 How to search for x: 1. Compare with the middle element, m 2. If x = m, done 3. If x > m, continue searching in the right half 4. If x < m, continue the search in the left half

27 27 void BinSearch( string list[], string item, int length, int& index, bool& found) { int first, last, middle; first = 0; // lower bound on list last = length - 1; // upper bound on list found = FALSE; while (last >= first && !found) { middle = (first + last)/2; if (item < list[middle] last = middle - 1; else if (item > list[middle]) first = middle + 1; else found = true; } index = middle; }

28 28 Average Number of Iterations

29 29 Records or Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT, unlike arrays, members also have names - field names, and may be different types; member

30 30 Declaration 1. define template - form of the structure; struct RecordName type member_1; type member_2; … endrecord 2. Declare variable RecordName varname;

31 31 Example: struct Employee { integer idNumber; string name; float salary; }; essentially creates a new variable type Employee emp1; Employee newEmp;

32 32 Reference use. Operator => recordName.membername example: newEmp.idNumber = 527; cout << newEmp.name;

33 33 Pointers Contains the address of another memory location DataType* Variable; int* iptr; & - address of * - location pointed to by

34 34 int i; int* iptr; iPtr = &i; *iptr = 5; iptri 5


Download ppt "1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input 100...... Example –test scores,employees,temperatures."

Similar presentations


Ads by Google