Lesson 8 of 20Core Programming22 min

Arrays and Strings

Arrays store a fixed number of values of the same type. The string class stores and processes text. Both are common building blocks in beginner programs.

Fixed-Size Array

Array example
#include <iostream>
using namespace std;

int main()
{
    int marks[5] = {78, 85, 90, 67, 88};
    int total = 0;

    for (int mark : marks)
    {
        total += mark;
    }

    cout << "Total = " << total << endl;
    return 0;
}

Working with string

String example
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string course = "Modern C++";

    cout << "Course: " << course << endl;
    cout << "Length: " << course.length() << endl;
    cout << "First character: " << course[0] << endl;

    return 0;
}
Index reminder

Array and string indexes begin at 0. The first element is at index 0, not index 1.

Practice task

Store five temperatures in an array and display the highest value.

Lesson Summary

Store groups of values and work with text using arrays and string.