Q&A: Arrays and vectors

Table of Contents

1. Arrays

What is an element of an array?

What is an index of an array?

What is the subscript operator?

How do you declare a traditional array? (Write an example)

How do you declare an array object? (Write an example)

How do you access element 0 of an array? (Write an example)

How do you access element i of an array? (Write an example)

Given an array of size n, what are the minimum and maximum valid indices?

What happens if you try to access an element of the array at an invalid index?

Write an example line of code given each of these instructions. Assume the following code is already provided.

const int SIZE = 5;
string arr[SIZE];
int index = 2;

Write a for loop to iterate through every element of the arr array and display the index and value of each item.

Write several cout statements to display each element of the array arr at indices 0 through 4.

Write a cout statement that displays the element of arr at the index given by the variable.

Note that "Iterate over all the elements of the array arr, displaying each index and element value." translates to this code:

for ( unsigned int i = 0; i < SIZE; i++ )
{
  cout << i << ". " << arr[i] << endl;
}

2. Dynamic arrays

Dynamic arrays are allocated on what kind of memory space? How do we allocate space for a new dynamic array, via a pointer? How do we free space from a dynamic array?


3. STL Array objects

Note the documentation page for Arrays: https://cplusplus.com/reference/array/array/ Give example code on how to declare an array object. Give example code on how to get the size of an array object. Write an example for loop that iterates through all the elements of an array object and couts each one.


4. STL Vector objects

Note the documentation page for Vectors: https://cplusplus.com/reference/vector/vector/ Give example code on how to declare a vector object. Give example code on how to get the size of a vector object. Write an example for loop that iterates through all the elements of a vector object and couts each one.


Author: Rachel Wil Sha Singh

Created: 2023-10-27 Fri 16:18

Validate