0
0
C++programming~10 mins

Array size and bounds in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an array of 5 integers.

C++
int arr[[1]];
Drag options to blanks, or click blank then click option'
A-1
B10
C0
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero or negative numbers as array size causes errors.
Forgetting to specify the size inside the brackets.
2fill in blank
medium

Complete the code to access the third element of the array.

C++
int value = arr[[1]];
Drag options to blanks, or click blank then click option'
A2
B1
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 instead of 2 for the third element index.
Confusing 1-based indexing with 0-based indexing.
3fill in blank
hard

Fix the error in the code to prevent out-of-bounds access.

C++
for (int i = 0; i < [1]; i++) {
    cout << arr[i] << " ";
}
Drag options to blanks, or click blank then click option'
A5
B6
C10
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a loop limit greater than the array size causes out-of-bounds errors.
Using <= instead of < in the loop condition.
4fill in blank
hard

Fill both blanks to create a dictionary that maps array indices to their values for elements greater than 10.

C++
std::map<int, int> result = {
    { [1], arr[[2]] }
};
Drag options to blanks, or click blank then click option'
A2
B3
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using different indices for key and value.
Using an index outside the array bounds.
5fill in blank
hard

Fill all three blanks to create a loop that sums all elements in the array.

C++
int sum = 0;
for (int [1] = 0; [2] < [3]; [1]++) {
    sum += arr[[1]];
}
Drag options to blanks, or click blank then click option'
Ai
C5
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop header.
Using a loop limit greater than the array size.