0
0
C++programming~10 mins

Common array operations 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 numbers[[1]];
Drag options to blanks, or click blank then click option'
A5
B10
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 as array size will not allocate enough space.
2fill in blank
medium

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

C++
int third = numbers[[1]];
Drag options to blanks, or click blank then click option'
A3
B0
C2
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 will cause out-of-bounds access.
3fill in blank
hard

Fix the error in the code to correctly initialize the array with values 1 to 5.

C++
int numbers[5] = {1, 2, 3, 4, [1];
Drag options to blanks, or click blank then click option'
A6
B5
C0
D7
Attempts:
3 left
💡 Hint
Common Mistakes
Using 6 or 7 adds incorrect values.
4fill in blank
hard

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

C++
int sum = 0;
for (int i = [1]; i [2] 5; i++) {
    sum += numbers[i];
}
Drag options to blanks, or click blank then click option'
A0
B<
C<=
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 skips the first element.
Using '<=' causes out-of-bounds access.
5fill in blank
hard

Fill all three blanks to create a loop that finds the maximum value in the array.

C++
int maxVal = numbers[[1]];
for (int i = [2]; i [3] 5; i++) {
    if (numbers[i] > maxVal) {
        maxVal = numbers[i];
    }
}
Drag options to blanks, or click blank then click option'
A0
B1
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop at 0 compares first element to itself unnecessarily.
Using '<=' causes out-of-bounds.