Ch 5: Arrays and Vectors

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What must you include to use char library functions?

#include <cctype>

When using vectors what must be included in the code?

#include <vector>

How do you find a vector's size

.size() returns the number of vector elements

What errors can occur when using .at()?

A common error is accessing out of range vector element which typically causes the program to automatically abort execution.

What is a common method for swapping?

A common method for swapping uses a temporary variable.

What is a logical error that can occur when assessing max value?

A logical error would be to initialize maxVal to 0, because 0 is not in fact the largest value seen so far, and would result in incorrect output (of 0) if the user entered all negative numbers.

How do you append items into a vector?

A programmer can append a new element to the end of an existing vector using a vector's push_back() function.

What is a vector?

A vector is an ordered list of items of a given data type.

How do you resize a vector?

A vector's size can be set or changed while a program executes using resize(N).

What is an array?

An array is a special variable having one name that stores a list of data items, with each item being directly accessible.

How are most swaps performed?

Between two list elements

How do you find the maximum/minimum value in a vector?

During each iteration of a loop, if the vector's current element is larger than the max or smaller than the minimum seen thus far, then assign the current vector element to a variable (like maxVal). Before entering the loop, the variable (i.e. maxVal) must be initialized to some value because it will be compared with each vector element's value.

What is an element?

Each item in a vector

If comparing multiple vectors what should they have in common?

Each vector should have the same number of elements

For a larger vector how is initialization done?

First declare the vector and then using a loop to assign vector elements.

What is the index?

In an array, each element's location number is called the index.

What happens if the new size is larger when using .resize()?

It adds elements to the end.

What type can the vector's index NOT be?

It cannot be a floating-point type, even if the value is 0.0, 1.0, etc.

What happens if the new size smaller when using .resize()?

It deletes elements from the end.

What does the first c in #include <cctype> mean?

It indicates the library is a C language standard library

What does "ctype" mean?

It is short for "character type"

What type must the vector's index be?

It must be an unsigned integer type.

What is a temporary variable?

It's a variable used briefly to store a value.

Does vector declaration and initialization require specifying the vector size?

No because the vector's size is automatically set to the number of elements within the braces.

How do you compare strings without paying attention to case?

One technique is to first convert (a copy of) each string to lowercase (using a loop, discussed elsewhere) and then comparing.

What is swapping?

Swapping two variables x and y means to assign y's value to x, and x's value to y.

How do you do an element by element vector copy (vector copy operation)?

The = operator conveniently performs an element-by-element copy of a vector, called a vector copy operation. The operation vectorB = vectorA resizes vectorB to vectorA's size, appending or deleting elements as needed. vectorB commonly has a size of 0 before the operation.

How do you do an element by element vector comparison (vector equality operation)?

The == operator conveniently compares vectors element-by-element, called a vector equality operation. vectorA == vectorB evaluates to true if the vectors are the same size AND each element par is equal.

What is row major order?

The compiler maps two-dimensional array elements to one-dimensional memory, each row following the previous row, known as row-major order.

What is the first index in a vector?

The first vector element is at 0.

What is the number in at() parentheses called?

The index of the corresponding element

What is the last index in a vector?

The last vector element is at N1 (where N is the size).

What are a vector's elements automatically initialized to?

They are automatically initialized to 0s during the vector declaration.

What is the difference between the vectors?

They are commonly two different data types and contain corresponding information that is compared to one another.

What is a benefit to arrays?

They have simpler syntax

Why do programs commonly iterate through vectors?

To determine some quantity about a vector's items

What is the difference between a vector and an array?

Vectors are just like arrays but have the ability to resize itself whenever an element is added to or deleted from it.

How is a vector is a more powerful form of an array?

Vectors are safer because the access v.at(i) is checked during execution to ensure the index is within the vector's valid range. An array access a[i] involves no such check. Such checking is important; trying to access an array with an out-of-range index is a very common error and one of the hardest errors to debug. Vectors have more advantages, like resizing during runtime, easy insertion of items at the front or rear, determining vector size, etc.

What is the benefit of using multiple sized vectors to store related lists?

You can compare them

How many elements are in a two-dimensional array

int myArray[R][C] represents a table of int variable with R rows and C columns, so R*C elements total.


Kaugnay na mga set ng pag-aaral

A nurse is caring for an older adult client who has dementia. Which of the following findings should the nurse expect?

View Set

Combo with "APUSH Learning Curve: Chapter 16" and 2 others

View Set

Networking Essentials: Module 6: Network Design and Access Layer

View Set

Una historia de amor: a practicar los verbos en México D.F.

View Set

Lección 8 Contextos, Fotonovela, y Pronunciación - Contextos - Self-check

View Set