chap09 - Chapter 9 Chapter List Arrays and Vectors Mechanism for representing lists Key Concepts Key one-dimensional arrays array subscripting arrays as

chap09 - Chapter 9 Chapter List Arrays and Vectors...

This preview shows page 1 - 13 out of 105 pages.

Image of page 1
Chapter 9 : Chapter 9 : List Arrays and Vectors Mechanism for representing lists
Image of page 2
Key Concepts: Key Concepts: one-dimensional arrays array subscripting arrays as parameters array element as parameters character string Standard Template Library (STL) container class template class vector vector subscripting vector resizing string subscripting iterators iterator dereferencing vector of vector sorting function InsertionSort() function QuickSort() searching function BinarySearch() algorithm library function find() table matrices member initialization list multidimensional arrays
Image of page 3
Lists Problem solving often requires information be viewed as a list List may be one-dimensional or multidimensional C++ provides two list mechanisms Arrays Traditional and important because of legacy libraries Restrictions on its use Container classes First-class list representation Common containers provided by STL vector , queue , stack , map , … Preferred long-term programming practice Analogies Egg carton Apartments Cassette carrier
Image of page 4
Array Traditional and important because of legacy libraries
Image of page 5
Basic Array Definition BaseType ArrayName[ SizeExp ]; Bracketed constant Expression indicating number of elements in list Type of values in list Name of list double X[100];
Image of page 6
Array Terminology List is composed of elements Elements in a list have a common name The list as a whole is referenced through the common name List elements are of the same type — the base type Elements of a list are referenced by subscripting or indexing the common name double X[100]; double a = X[10];
Image of page 7
Array Terminology Subscripts are denoted as expressions within brackets: [ ] Base type can be any fundamental, library-defined, or programmer-defined type The index type is integer and the index range must be 0 ... n -1 where n is the size of array. Parameter passing style Always call by reference (no indication necessary) double X[100]; // Subscripts are 0 through 99 double a = X[100]; // What happen?
Image of page 8
// array of 10 ints char B[MaxStringSize]; // array of 80 chars double C[M*N]; // array of 800 floats int Values[MaxListSize]; // array of 1000 ints Rational D[N-15]; // array of 5 Rationals
Image of page 9
Subscripting Suppose int A[10]; // array of 10 ints A[0], … A[9] To access individual element must apply a subscript to list name A A subscript is a bracketed expression also known as the index First element of list has index 0 A[0] Second element of list has index 1, and so on A[1] Last element has an index one less than the size of the list A[9] Incorrect indexing is a common error A[10] // does not exist
Image of page 10
Array Elements Suppose int A[10]; // array of 10 uninitialized ints A A[4]A[5] A[6] A[3] A[0] A[2] A[8] A[9] A[7] A[1] -- -- -- -- -- -- -- -- -- -- To access an individual element we must apply a subscript to list name A
Image of page 11
Array Element Manipulation Consider int i = 7, j = 2, k = 4; A[0] = 1; A[j] = A[i] + 3; A[i] = 5; A[j+1] = A[i] + A[0]; cin >> A[k]; // where suppose next input value is 3 A[A[j]] = 12; A A[4]A[5] A[6] A[3] A[0] A[2] A[8] A[9] A[7] A[1] -- -- -- -- 1 5 8 6 12 3
Image of page 12
Image of page 13

You've reached the end of your free preview.

Want to read all 105 pages?

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes