Podcast
Questions and Answers
What is the index of the first element in a C++ array?
What is the index of the first element in a C++ array?
- It is randomly assigned
- 1
- It depends on the data type
- 0 (correct)
If myArray
is an array, which of the following is the correct way to access the element at index 5?
If myArray
is an array, which of the following is the correct way to access the element at index 5?
- myArray(5)
- myArray[5] (correct)
- myArray.5
- myArray{5}
Which of the following is a correct way to declare an array of 10 integers named numbers
?
Which of the following is a correct way to declare an array of 10 integers named numbers
?
- int numbers(10);
- int numbers{10};
- int numbers = [10];
- int numbers[10]; (correct)
What happens in C++ if you try to access an array element with an index that is out of bounds?
What happens in C++ if you try to access an array element with an index that is out of bounds?
Given the array int arr[] = {5, 10, 15};
, what is the value of arr[1]
?
Given the array int arr[] = {5, 10, 15};
, what is the value of arr[1]
?
How can you initialize all elements of an integer array to zero during declaration?
How can you initialize all elements of an integer array to zero during declaration?
Which of the following is the correct way to declare a constant integer array named sizes
of size 5?
Which of the following is the correct way to declare a constant integer array named sizes
of size 5?
What is the purpose of a null terminator in a C-string?
What is the purpose of a null terminator in a C-string?
Which of the following functions can be used to determine the length of a C-string?
Which of the following functions can be used to determine the length of a C-string?
Which header file should be included to use functions like strlen()
and strcpy()
?
Which header file should be included to use functions like strlen()
and strcpy()
?
What will be the output of the following code?
char str[] = "Hello";
cout << str[2];
What will be the output of the following code?
char str[] = "Hello";
cout << str[2];
Which function is used to copy one C-string to another?
Which function is used to copy one C-string to another?
What is a potential risk when using strcpy()
?
What is a potential risk when using strcpy()
?
Which of the following is the correct way to read a line of text from the user into a C-string named input
with a maximum size of 100 characters?
Which of the following is the correct way to read a line of text from the user into a C-string named input
with a maximum size of 100 characters?
What does the following code do?
int numbers[5] = {1, 2};
What does the following code do?
int numbers[5] = {1, 2};
Why is it generally better to pass the size of an array to a function along with the array itself?
Why is it generally better to pass the size of an array to a function along with the array itself?
Which of the following is NOT a valid operation for processing arrays?
Which of the following is NOT a valid operation for processing arrays?
What is the time complexity of linear search on an array of size n?
What is the time complexity of linear search on an array of size n?
For binary search to work correctly, what condition must be met?
For binary search to work correctly, what condition must be met?
What is the return value of the linear search algorithm if the element is not found in the array?
What is the return value of the linear search algorithm if the element is not found in the array?
What is the time complexity of binary search in the worst-case scenario?
What is the time complexity of binary search in the worst-case scenario?
Which searching algorithm is generally more efficient for large, sorted arrays?
Which searching algorithm is generally more efficient for large, sorted arrays?
In selection sort, how many swaps are performed in each pass?
In selection sort, how many swaps are performed in each pass?
What is the primary goal of the selection sort algorithm?
What is the primary goal of the selection sort algorithm?
What is the time complexity of selection sort in the worst case?
What is the time complexity of selection sort in the worst case?
Given the array int arr[] = {5, 2, 8, 1, 9};
, what would be the order of elements after the first pass of selection sort?
Given the array int arr[] = {5, 2, 8, 1, 9};
, what would be the order of elements after the first pass of selection sort?
Which of the following is the correct way to initialize a character array with the string 'Hello'?
Which of the following is the correct way to initialize a character array with the string 'Hello'?
What does the atoi()
function do?
What does the atoi()
function do?
In C++, can you directly assign one array to another using the assignment operator (=)?
In C++, can you directly assign one array to another using the assignment operator (=)?
Given the C-string char str[] = "C++Exam";
, what will strlen(str)
return?
Given the C-string char str[] = "C++Exam";
, what will strlen(str)
return?
Suppose you want to prevent a function from modifying an array passed to it. How can you achieve this?
Suppose you want to prevent a function from modifying an array passed to it. How can you achieve this?
What is the issue with the following code?
int arr[5];
arr = {1, 2, 3, 4, 5};
What is the issue with the following code?
int arr[5];
arr = {1, 2, 3, 4, 5};
Which of the following operations shuffles the elements of an array randomly?
Which of the following operations shuffles the elements of an array randomly?
What is the purpose of the shown code?
double temp = myList[0];
for (int i = 1; i < SIZE; i++)
{
myList[i - 1] = myList[i];
}
myList[SIZE - 1] = temp;
What is the purpose of the shown code?
double temp = myList[0];
for (int i = 1; i < SIZE; i++)
{
myList[i - 1] = myList[i];
}
myList[SIZE - 1] = temp;
Consider the following code that initializes an array with random values:
const int SIZE = 100;
double myList[SIZE];
for (int i = 0; i < SIZE; i++)
{
myList[i] = rand() % 100;
}
What range of values can elements of the array have?
Consider the following code that initializes an array with random values:
const int SIZE = 100;
double myList[SIZE];
for (int i = 0; i < SIZE; i++)
{
myList[i] = rand() % 100;
}
What range of values can elements of the array have?
What is wrong in the following code?
double myList[4];
myList = {1.9, 2.9, 3.4, 3.5};
What is wrong in the following code?
double myList[4];
myList = {1.9, 2.9, 3.4, 3.5};
What does the following code snippet do?
int arr[] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; ++i)
{
cout << "arr[" << i << "] = " << arr[i] << endl;
}
What does the following code snippet do?
int arr[] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; ++i)
{
cout << "arr[" << i << "] = " << arr[i] << endl;
}
Consider the following code:
int arr[4];
for (int i = 0; i < 4; ++i)
{
arr[i] = i + 1;
}
What is the value of arr[3]
after the loop?
Consider the following code:
int arr[4];
for (int i = 0; i < 4; ++i)
{
arr[i] = i + 1;
}
What is the value of arr[3]
after the loop?
Consider the following code. What is the output?
const int SIZE = 10;
int myList[SIZE];
int total = 0;
for (int i = 0; i < SIZE; i++)
{
total += myList[i];
}
cout << "The total is: " << total << endl;
Consider the following code. What is the output?
const int SIZE = 10;
int myList[SIZE];
int total = 0;
for (int i = 0; i < SIZE; i++)
{
total += myList[i];
}
cout << "The total is: " << total << endl;
Consider the following code. What is the output?
const int ARRAY_SIZE = 5;
int myList[] = {1, 6, 5, 6, 2};
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < ARRAY_SIZE; i++)
{
if (myList[i] >= max)
{
max = myList[i];
indexOfMax = i;
}
}
cout << "index is: " << indexOfMax;
Consider the following code. What is the output?
const int ARRAY_SIZE = 5;
int myList[] = {1, 6, 5, 6, 2};
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < ARRAY_SIZE; i++)
{
if (myList[i] >= max)
{
max = myList[i];
indexOfMax = i;
}
}
cout << "index is: " << indexOfMax;
A programmer wants to reverse an array in C++. They use the following code:
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
}
What are the values of newList after the procedure call, given int[] list1 = {1, 2, 3, 4, 5, 6}?
A programmer wants to reverse an array in C++. They use the following code:
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
}
What are the values of newList after the procedure call, given int[] list1 = {1, 2, 3, 4, 5, 6}?
Why cant arrays be directly returned from functions?
Why cant arrays be directly returned from functions?
Flashcards
Array
Array
A data structure representing a collection of the same data types.
Declaring an array
Declaring an array
The data type of the elements the array will hold, followed by a variable name and the number of elements in square brackets.
Array Indices
Array Indices
Elements in the array are accessed using their index. Indices start at 0.
Indexed variable
Indexed variable
Signup and view all the flashcards
No Bound Checking
No Bound Checking
Signup and view all the flashcards
Array Initializers
Array Initializers
Signup and view all the flashcards
Shorthand Notation
Shorthand Notation
Signup and view all the flashcards
Implicit Size
Implicit Size
Signup and view all the flashcards
Partial Initialization
Partial Initialization
Signup and view all the flashcards
Printing an array
Printing an array
Signup and view all the flashcards
Copying arrays
Copying arrays
Signup and view all the flashcards
Summing array elements
Summing array elements
Signup and view all the flashcards
Linear search
Linear search
Signup and view all the flashcards
Binary search
Binary search
Signup and view all the flashcards
Binary search condition
Binary search condition
Signup and view all the flashcards
Sorting
Sorting
Signup and view all the flashcards
Selection sort
Selection sort
Signup and view all the flashcards
C-Strings
C-Strings
Signup and view all the flashcards
Null terminator
Null terminator
Signup and view all the flashcards
Printing a character array
Printing a character array
Signup and view all the flashcards
cin.getline
cin.getline
Signup and view all the flashcards
strlen
strlen
Signup and view all the flashcards
strcpy
strcpy
Signup and view all the flashcards
strcmp
strcmp
Signup and view all the flashcards
atoi
atoi
Signup and view all the flashcards
Study Notes
- Arrays are data structures holding collections of the same data type.
- Arrays address the opening problem of reading a hundred numbers, computing their average, and determining how many are above the average.
Declaring Array Variables
- General syntax:
datatype arrayRefVar[arraySize];
- Example:
double myList[10];
- The array size must be a constant expression.
- This code is illegal if
size
is a variable:int size = 4;
double myList[size]; // Wrong
- Use
const int size = 4;
for a valid constant size. - Array elements receive arbitrary initial values upon creation.
Indexed Variables
- Array elements are accessed via an index, starting at 0.
- Indices range from 0 to
arraySize-1
. - Element syntax:
arrayName[index]
myList[9]
refers to the last element of arraymyList
.
Using Indexed Variables
- Indexed variables behave like regular variables after an array is created.
- Example:
myList[2] = myList[0] + myList[1];
adds the values inmyList[0]
andmyList[1]
and assigns the sum tomyList[2]
.
No Bound Checking
- C++ performs no array boundary checks.
- Accessing elements beyond the bounds (e.g.,
myList[-1]
ormyList[11]
) does not trigger syntax errors. - Out-of-bounds access may cause memory access violations reported by the OS.
Array Initializers
- Declaring, creating, and initializing in one step:
dataType arrayName[arraySize] = {value0, value1, ..., valuek};
- Example:
double myList[4] = {1.9, 2.9, 3.4, 3.5};
is equivalent to:double myList[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
- You have to declare, create, and initialize the array all in one statement to use shorthand notation.
- Splitting it would cause a syntax error.
- Invalid:
double myList[4];
myList = {1.9, 2.9, 3.4, 3.5}; // ERROR!
Implicit Size
- C++ allows omitting the array size when using an initializer.
- Example:
double myList[] = {1.9, 2.9, 3.4, 3.5};
- C++ can deduce the size based on the initializer list.
Partial Initialization
- It is possible to initialize only part of an array.
- Example:
double myList[4] = {1.9, 2.9};
- The uninitialized elements are set to zero.
- If an array is declared but not initialized, it contains "garbage" values.
Initializing Arrays with Random Values
- The following loop initializes the array
myList
with random values between 0 and 99:
const int SIZE = 100;
double myList[SIZE];
for (int i = 0; i < SIZE; i++) {
myList[i] = rand() % 100;
}
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.