Chp 7 - Arrays and Vectors

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/11

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

12 Terms

1
New cards

Array

A variable that can story multiple values of the same type

  • values are stored in adjacent memory locations

  • declared using [ ] operator

    • int tests[5];

  • the size of an array is the total number of bytes allocated for it: (number of elements) * (size of each element)

2
New cards

Accessing Array Elements

each element in an array is assigned a unique subscript (starting at 0 to n - 1)

  • array elements can be used as regular variables

  • arrays must be accessed via individual elements

  • can access element with a constant or literal subscript

  • can use integer expression as subscript

<p>each element in an array is assigned a unique subscript (starting at 0 to n - 1)</p><ul><li><p>array elements can be used as regular variables</p></li><li><p>arrays must be accessed via individual elements</p></li><li><p>can access element with a constant or literal subscript</p></li><li><p>can use integer expression as subscript</p></li></ul><p></p>
3
New cards

Using a Loop to Step Through an Array

knowt flashcard image
4
New cards

Array Initialization

  • global array all elements initialized to 0 by default

  • local array all elements uninitialized by default

  • arrays can be initialized with an initialization list

    • int tests[5] = {79, 82, 91, 77, 84};

    • you can also determine the array size by the size of the initialization list

5
New cards

No Bounds Checking in C++

  • when you use a value as an array subscript, C++ does not check it to make sure it is a valid subscript

  • you can use subscripts beyond the bounds of the array, doing so can corrupt other memory locations, crash program, lock up computer, cause elusive bugs…BE CAREFUL!

6
New cards

The Range-Based for Loop

  • C++ 11

  • a loop that iterates once for each element in an array

  • each time the loop iterates, it copies an element from the array to a built-in variable, known as the range variable

  • the range-based for loop automatically knows the number of elements in an array, you do not have to:

    • use a counter variable

    • worry about stepping outside the bounds of the array

  • can be used in any situation where you do not need to use the element subscripts

<ul><li><p>C++ 11</p></li><li><p>a loop that iterates once for each element in an array</p></li><li><p>each time the loop iterates, it copies an element from the array to a built-in variable, known as the range variable</p></li><li><p>the range-based for loop automatically knows the number of elements in an array, you do not have to:</p><ul><li><p>use a counter variable</p></li><li><p>worry about stepping outside the bounds of the array</p></li></ul></li><li><p>can be used in any situation where you <strong><em>do not</em></strong> need to use the element subscripts</p></li></ul><p></p>
7
New cards

Copying one array to another

assign element-by-element

<p>assign element-by-element</p>
8
New cards

Printing an array

print element-by-element

<p>print element-by-element</p>
9
New cards

Structured Binding Declarations

defines a set of variables and initialized them with the values that are stored in an array

  • auto [variable1, variable2, etc…] = array;

<p>defines a set of variables and initialized them with the values that are stored in an array</p><ul><li><p><strong>auto [variable1, variable2, etc…] = array;</strong></p></li></ul><p></p>
10
New cards

Parallel Arrays

two or more arrays that contain related data

  • a subscript is used to relate arrays (elements at the same subscript are related)

  • arrays may be of different types

11
New cards

Arrays as Function Arguments

  • to pass an array to a function, use the array name

    • showScores(tests);

  • to define a function that takes an array parameter, use empty [] for array argument

    • void showScores(int []);

  • it is common to pass array size so that the function knows how many elements to process

    • showScores(tests, ARRAY_SIZE);

  • array size must be reflected in prototype header

12
New cards

Two-Dimensional Arrays

First declarator is number of rows, second is number of columns.

  • const int ROWS = 4, COLS = 3;

  • int exams[ROWS][COLS]

<p>First declarator is number of rows, second is number of columns.</p><ul><li><p><strong>const int ROWS = 4, COLS = 3;</strong></p></li><li><p><strong>int exams[ROWS][COLS]</strong></p></li></ul><p></p>

Explore top flashcards

respiratory system
Updated 696d ago
flashcards Flashcards (22)
Exam 2 For Dorth
Updated 229d ago
flashcards Flashcards (110)
The Immune System
Updated 324d ago
flashcards Flashcards (35)
Biology Unit 7
Updated 908d ago
flashcards Flashcards (210)
religion final
Updated 887d ago
flashcards Flashcards (29)
respiratory system
Updated 696d ago
flashcards Flashcards (22)
Exam 2 For Dorth
Updated 229d ago
flashcards Flashcards (110)
The Immune System
Updated 324d ago
flashcards Flashcards (35)
Biology Unit 7
Updated 908d ago
flashcards Flashcards (210)
religion final
Updated 887d ago
flashcards Flashcards (29)