Podcast
Questions and Answers
What assigns element 0 with the value 250?
What assigns element 0 with the value 250?
peoplePerDay = 250
What assigns element 1 with the value 99?
What assigns element 1 with the value 99?
peoplePerDay = 99
What is the value of peoplePerDay after these statements: peoplePerDay = 5; peoplePerDay = peoplePerDay - 3;
What is the value of peoplePerDay after these statements: peoplePerDay = 5; peoplePerDay = peoplePerDay - 3;
2
What is the value of peoplePerDay given: peoplePerDay[N] = 15; N = N + 1; peoplePerDay[N] = peoplePerDay[N - 1] * 3?
What is the value of peoplePerDay given: peoplePerDay[N] = 15; N = N + 1; peoplePerDay[N] = peoplePerDay[N - 1] * 3?
Signup and view all the answers
Assign the first element in scoresList with 77.
Assign the first element in scoresList with 77.
Signup and view all the answers
Assign the second element in scoresList with 77.
Assign the second element in scoresList with 77.
Signup and view all the answers
Assign the last element in scoresList with 77.
Assign the last element in scoresList with 77.
Signup and view all the answers
If the array has 100 elements, what is the last element's index?
If the array has 100 elements, what is the last element's index?
Signup and view all the answers
If the array's last index was 499, how many elements does the array have?
If the array's last index was 499, how many elements does the array have?
Signup and view all the answers
How many elements does the vector yearsList(4) create?
How many elements does the vector yearsList(4) create?
Signup and view all the answers
What value is assigned into yearsList.at(1)?
What value is assigned into yearsList.at(1)?
Signup and view all the answers
What value does curr = yearsList.at(2) assign to curr?
What value does curr = yearsList.at(2) assign to curr?
Signup and view all the answers
Is curr = yearsList.at(4) a valid assignment?
Is curr = yearsList.at(4) a valid assignment?
Signup and view all the answers
What is the proper way to access the first element in vector yearsList?
What is the proper way to access the first element in vector yearsList?
Signup and view all the answers
What are the contents of the vector after yearsList.at(0) = yearsList.at(2)?
What are the contents of the vector after yearsList.at(0) = yearsList.at(2)?
Signup and view all the answers
Study Notes
Array Basics
- An array
peoplePerDay
has 365 elements corresponding to each day of the year. - To assign a value to an array element, use the format
array[index] = value;
.
Assigning Values
-
peoplePerDay[0] = 250
assigns the first element the value 250. -
peoplePerDay[1] = 99
assigns the second element the value 99.
Arithmetic Operations
- If an element's value is modified through arithmetic, e.g.,
peoplePerDay = 5; peoplePerDay -= 3;
, it results inpeoplePerDay
holding a value of2
.
Indexed Assignments
- For
peoplePerDay[N] = 15;
, ifN
starts at 1, the first assignment initializespeoplePerDay[1]
to 15. - The subsequent assignment
peoplePerDay[N] = peoplePerDay[N - 1] * 3;
results inpeoplePerDay[2] = 45
.
Accessing Array Elements
- The first element of an array
scoresList
initialized with 10 elements is accessed viascoresList[0]
. - The last element of a 10-element array is
scoresList[9]
.
Array Indices
- For an array of size
N
, the last index isN - 1
. - An array with a last index of
499
has500
elements.
Vector Basics
- A vector
yearsList(4)
initializes four elements, which can be accessed using the.at(index)
method. - Each
yearsList.at(index)
corresponds to the element at that position within the vector.
Vector Assignments
-
yearsList.at(1) = 2012
assigns the second element the value 2012. - Using
curr = yearsList.at(2)
assignscurr
the value2025
.
Validity of Access
- Attempting to access
yearsList.at(4)
results in an error since valid indices for four elements are 0, 1, 2, and 3.
Accessing First Element
- The first element can be accessed with
yearsList.at(0)
.
Content Changes
- Assigning
yearsList.at(0) = yearsList.at(2)
changes the first element to2025
, resulting in the vector contents being2025, 2012, 2025, 0
.
Summary of Valid Indices
- Only indices
0
to3
are valid for a vector of size4
.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your understanding of vectors with these flashcards covering participation activities from Chapter 6. Each card presents an array-related question, helping reinforce your grasp of the material. Perfect for review and preparation before exams.