c_-_03 - Chapter 6 Arrays Objectives To describe why an...

Info iconThis preview shows pages 1–18. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 6 Arrays
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Objectives To describe why an array is necessary in programming (§6.1). To learn how to declare an array (§6.2.1). To access array elements using indexed variables (§6.2.2). To initialize the values in an array (§6.2.3). To develop and invoke functions with array arguments (§§6.3-6.4). To search elements using the linear (§6.5.1) or binary search algorithm (§6.5.2). To sort an array using the selection sort (§6.6.1) (Optional) To sort an array using the insertion sort algorithms (§6.6.2). To declare and create two-dimensional arrays (§6.7). (Optional) To declare and create multidimensional arrays (§6.8).
Background image of page 2
Introducing Arrays Array is a data structure that represents a collection of the same types of data. 5.6 4.5 3.3 13.2 4.0 34.33 34.0 45.45 99.993 111.23 myList [ ]; myList[0] myList[1] myList[2] myList[3] myList[4] myList[5] myList[6] myList[7] myList[8] myList[9] Element value Array element at index 5 Array Size Data Type
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Declaring Array Variables double myList[10]; int size = 4; double myList[size]; const int size = 4; double myList[size];
Background image of page 4
Initial Values of Arrays int mylist[10]; void main() { int mylist[10]; for (int i=0; i<10; i++) { cout << i << ":" << mylist[i] << " " << ::mylist[i] << endl; } } Arbitrary values when not assigned values Initial values are 0 when not assigned values;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
No Bound Checking int mylist[10]; void main() { for (int i=0; i<11; i++) { cout << i << ":" << mylist[i] << endl; } } Very Very Very Dangerous !!!
Background image of page 6
Array Initializers Declaring, creating, initializing in one step: dataType arrayName[arraySize] = {value0, value1, . .., value k }; double myList[4] = {1.9, 2.9, 3.4, 3.5};
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Declaring, creating, initializing Using the shorthand Notation double myList[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5; double myList[4] = {1.9, 2.9, 3.4, 3.5}; double myList[4]; myList = {1.9, 2.9, 3.4, 3.5}; double myList[ ] = {1.9, 2.9, 3.4, 3.5};
Background image of page 8
Partial Initialization double myList[4] = {1.9, 2.9}; double myList[4] = {1.9, 2.9, 0, 0};
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Initializing Character Arrays char city[] = {'D', 'a', 'l', 'l', 'a', 's'}; char city[] = "Dallas“; city[0] city[1] ' D ' ' a ' ' l ' ' l ' ' a ' ' s ' ' \0 ' city[2] city[3] city[4] city[5] city[6]
Background image of page 10
Printing Character Array char city[] = "Dallas"; cout << city << endl; int mylist[] = {1, 2}; cout << mylist << endl;
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copying Arrays for ( int i = 0; i < 10; i++) { list[i] = myList[i]; } int list[10]; int myList[10]; list = myList;
Background image of page 12
Passing Arrays to Functions void ClearArray( int list[] , int num) { for (int i=0; i<num; i++) { list[i] = 0; } } void PrintArray( int list[], int num) { for (int i=0; i<num; i++) { cout << list[i] << " "; } cout << endl; } void main() { int number[5] = {1, 2, 3, 4, 5}; PrintArray(number, 5); ClearArray(number, 5); PrintArray(number, 5); } When passing arrays to function, It is passing-by-reference.
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
const Parameters void PrintArray(int list[], int num) { for (int i=0; i<num; i++) { cout << list[i] << " "; list[i] = 0; } cout << endl; } void main() { int number[5] = {1, 2, 3, 4, 5}; PrintArray(number, 5); PrintArray(number, 5); }
Background image of page 14
const Parameters void PrintArray(const int list[], int num) { for (int i=0; i<num; i++) { cout << list[i] << " "; list[i] = 0; } cout << endl; } void main() { int number[5] = {1, 2, 3, 4, 5}; PrintArray(number, 5); PrintArray(number, 5); }
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Returning Arrays from Functions int[] ReserveArray(int List, int size); void reverse(const int list[], int reversedList[], int size) { for (int i=0, j = size - 1; i< size; i++, j--) { reversedList[j] = list[i]; } }
Background image of page 16
Some Algorithms About Arrays Searching Arrays The Linear Search Approach The Binary Search Approach Sorting Arrays (Exercises) Selection Sort Insertion Sort
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/12/2011 for the course ECON 101 taught by Professor Professor during the Spring '10 term at Cisco Junior College.

Page1 / 73

c_-_03 - Chapter 6 Arrays Objectives To describe why an...

This preview shows document pages 1 - 18. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online