What is the index of the first element in a C++ array
0
What is the term for accessing an array element beyond its bounds?
Out-of-bounds access
array index out of bounds
Anything out of bounds works.
What is the purpose of a function in C++?
To group a block of code that performs a specific task
Any of these valid:
Modularity
Reusability
Managment of Code
etc.
Given an array int arr[] = {1, 5, 2, 8, 3};, what is the sum of the first and last elements?
4
What is a static array?
An array whose size is fixed at compile time.
Declare a C++ array named numbers that can hold 5 integers
Initialize a C++ array named evens with the first 5 even positive integers.
Then show me how to access the third element in the array.
int evens[5] = {2, 4, 6, 8, 10};
evens[3];
What does a function declaration specify, what is the syntax of a function declartion?
return type, The function's name, and parameters
What is the value of the third element in the array float values[] = {2.5, 1.0, 7.3, 9.0};
7.3
What happens if you try to access an array element with an index that is out of bounds?
It leads to undefined behavior
What is the index of the last element in a C++ array of size n?
Answer: n-1
How do you access the last element of the array int data[15];
data[14];
What is the difference between a function declaration and a function definition?
A declaration provides the function's signature (name, return type, parameters), while a definition provides the actual code that the function executes.
Given the array int scores[5] = {60, 70, 80, 90, 100};, write the C++ code to change the value of the third element to 85.
scores[2] = 85;
int numbers[5] = {1, 2, 3, 4, 5};
numbers[2] = "hello";
You are trying to assign a string to an integer array element.
True or False: When declaring an array, you must specify its size at compile time.
True
What is the value of numbers[2] after the following code:
int numbers[4] = {1, 2, 3, 4};
numbers[2] = numbers[0] + numbers[3];?
5
What keyword is used to return a value from a function?
Answer: return
Arrays can store elements of different data types.
Answer: False
What is the purpose of a null terminator character in a character array?
It marks the end of the string stored in the character array.
Write code to declare an integer array of size 5, initialize it with the values 10, 20, 30, 40, and 50, and then print each element of the array to the console, one element per line.
int myArray[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i ++) {
cout << myArray[i];
}
Write code to declare an integer array of size 5, initialize it with the values 1, 2, 3, 4, and 5, Print a reversed array. (Manipulate a new array (Harder) OR just print the numbers in reverse (Easier) ).
int originalArray[] = {10, 20, 30, 40, 50};
const int SIZE = 5;
int reversedArray[SIZE];
int j = 0;
for (int i = SIZE - 1; i >= 0; --i) {
reversedArray[j] = originalArray[i];
j++;
}
Write a function that takes two integer arguments and returns their product.
int productFunction(int num1, int num2) {
return num1 * num2
}
Write code to declare an integer array of size 5, initialize it with 5 numbers and find the largest number in the array. Print the largest number to the console.
int currLargestNumber = 0;
for (int i = 0; i < 5; i++) {
if(curLargestNumber < arr[i]) {
curLargestNumber = arr[i];
}
cout << curLargestNumber << endl;
}
Write code to declare two integer arrays of size 5. Initialize the first array with the values 1, 2, 3, 4, 5, and the second array with the values 6, 7, 8, 9, 10. Then, create a third array of size 5, and store the sum of the corresponding elements from the first two arrays in the third array. Print the elements of the third array to the console. For example, the first element of the third array should be 1 + 6 = 7.
~Show in Class~