Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an array of 5 integers.
C++
int numbers[[1]]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 as array size will not allocate enough space.
✗ Incorrect
The array size must be specified as 5 to hold five integers.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 will cause out-of-bounds access.
✗ Incorrect
Array indexing starts at 0, so the third element is at index 2.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 6 or 7 adds incorrect values.
✗ Incorrect
The last element should be 5 to complete the sequence from 1 to 5.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 skips the first element.
Using '<=' causes out-of-bounds access.
✗ Incorrect
The loop starts at 0 and runs while i is less than 5 to cover all elements.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop at 0 compares first element to itself unnecessarily.
Using '<=' causes out-of-bounds.
✗ Incorrect
Start maxVal with the first element at index 0, loop from index 1 while i is less than 5 (index 4 is last).