Small Steps I — Creating with Text and Numbers

Use Arrays

Use Arrays

Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.

Keeping several values together

What you will learn

An array is an ordered collection of values of the same type. Small's Array<int> is a type that holds several integers.

Try it

Example · example.cpp
void small_main()
{
    Array<int> scores = {80, 95, 70};
    print(scores[0]);
    scores[1] = 100;
    print(scores[1]);
}

One value in an array is an element; its position number is an index. Like string positions, indices start at 0. With three elements, valid indices are 0, 1, and 2. Brackets let you read or change a value.

Expected output:

80
100

Looking at array values one at a time

What you will learn

Using a loop variable as an array index lets you read the elements in order. length() tells you how many elements there are.

Try it

Example · array_loop_example.cpp
void small_main()
{
    Array<int> scores = {80, 95, 70};
    for (int i = 0; i < scores.length(); i = i + 1)
    {
        print(scores[i]);
    }
}

The program prints when i is 0, 1, and 2. At 3, i equals the number of elements and the condition becomes false. That is why the condition uses <, not <=. A length of 0 runs no iterations.

Expected output:

80
95
70

Creating an array by choosing its size

What you will learn

Instead of listing initial values, you can choose the number of positions first. Array<int>(3) creates three positions for integers.

Try it

Example · array_create_example.cpp
void small_main()
{
    Array<int> numbers(3);
    for (int i = 0; i < numbers.length(); i = i + 1)
    {
        numbers[i] = i + 1;
        print(numbers[i]);
    }
}

Array<int> numbers(3); has three positions. Array<int> numbers = {3}; has one position containing the value 3. Keep the two forms distinct.

Small Array has no operation to increase its length after creation. You can still change each element's value.

Expected output:

1
2
3

Exercise

Change the third score to 90 and print it.

Exercise starter · exercise_starter.cpp
void small_main()
{
    Array<int> scores = {80, 95, 70};
    print(scores[0]);
    scores[1] = 100;
    print(scores[1]);
}

Hint

Change the parts of the example that you need.

show solution
void small_main()
{
    Array<int> scores = {80, 95, 70};
    scores[2] = 90;
    print(scores[2]);
}
Try More (Optional)

Exercise

Print twice each score. You do not need to store the results back in the array.

Exercise starter · array_loop_exercise_starter.cpp
void small_main()
{
    Array<int> scores = {80, 95, 70};
    for (int i = 0; i < scores.length(); i = i + 1)
    {
        print(scores[i]);
    }
}

Hint

Change the parts of the example that you need.

show solution
void small_main()
{
    Array<int> scores = {80, 95, 70};
    for (int i = 0; i < scores.length(); i = i + 1)
    {
        print(scores[i] * 2);
    }
}

Exercise

Create five positions, store 1 through 5 in them, and print them.

Exercise starter · array_create_exercise_starter.cpp
void small_main()
{
    Array<int> numbers(3);
    for (int i = 0; i < numbers.length(); i = i + 1)
    {
        numbers[i] = i + 1;
        print(numbers[i]);
    }
}

Hint

Change the parts of the example that you need.

show solution
void small_main()
{
    Array<int> numbers(5);
    for (int i = 0; i < numbers.length(); i = i + 1)
    {
        numbers[i] = i + 1;
        print(numbers[i]);
    }
}
← Previous lessonNext lesson →