All lessonsOpen Open Open Open Open
6. Arrays
Arrays
0 of 5 activities0%
Reading 1
A row of boxes with one name
An array holds several values of the same type under one name. You pick the size when you declare it.
Indexes start at 0. For an array of size 5, valid indexes are 0, 1, 2, 3, and 4.
You can initialize values in braces. Use a for loop to walk every element.
int nums[5] = {10, 20, 30, 40, 50};
cout << nums[0] << endl; // 10
cout << nums[4] << endl; // 50
for (int i = 0; i < 5; i++) {
cout << nums[i] << " ";
}Check 2
First index
In int a[4] = {2, 4, 6, 8}; what is a[0]?
Fill in 3
Indexing
Try it 4
Print an array
Run this. Change one value in the initializer and run again.
main.cpp
Loading editor…
Output will appear here.
Assignment 5
Sum of five numbers
Read five integers into an array. Print their sum, followed by a newline.
main.cpp
Loading editor…