Loading...

Arrays and Strings in C++

Imagine taking attendance in your class. You don’t write each student's name in a separate notebook, you keep them in a list, one after another, in a specific order.
Computers do the same using arrays: neat rows of data, all stored together in memory.

Today, you’ll show C++ how to work with lists of numbers and strings, helping your programs store more than one value at a time.

Daily Temperature Tracker

You’ll create a program that stores the temperatures of 7 days and prints them all together.

Here’s what the final output looks like:

text
4 lines
|
24/ 500 tokens
1
2
3
4
========================
   WEEKLY TEMPERATURES
   32 30 31 33 29 28 30
========================

This small program teaches you how arrays store and display groups of values.

1D Arrays

A 1D array stores a list of values in a single row.Perfect for things like marks, temperatures, or scores. All elements share the same data type.

Example

cpp
1 lines
|
9/ 500 tokens
1
int marks[5] = {85, 90, 78, 92, 88};
Code Tools

This stores five integers side by side in memory.

2D Arrays

A 2D array is like a table with rows and columns. Useful for seating charts, game boards, or matrices. Think of it as an “array of arrays.”

Example

cpp
1 lines
|
11/ 500 tokens
1
int grid[2][3] = { {1, 2, 3}, {4, 5, 6} };
Code Tools

This creates two rows and three columns of numbers.

Common Array Problems

Arrays are often used for tasks like:

  • Finding the total or average
  • Searching for a number
  • Printing values in order
  • Storing repetitive data cleanly

Small Example

cpp
2 lines
|
15/ 500 tokens
1
2
int sum = 0;
for (int i = 0; i < 5; i++) sum += marks[i];
Code Tools

This loop adds all elements to compute total marks.

C-Style Strings (char[])

A C-style string is an array of characters. Each letter is stored in a char array, ending with a special '\0' character. This is the old, traditional way of storing text in C++.

Example

cpp
1 lines
|
6/ 500 tokens
1
char name[10] = "Ali";
Code Tools

This stores A, l, i, and a null terminator inside a fixed-size array.

Basic String Functions

C-style strings can be manipulated with functions like:

  • strlen() → counts characters
  • strcpy() → copies strings
  • strcmp() → compares strings

Example

cpp
1 lines
|
8/ 500 tokens
1
strlen(name);  // returns length
Code Tools

Let’s Build the Temperature Tracker

cpp
17 lines
|
86/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main() {
    int temp[7] = {32, 30, 31, 33, 29, 28, 30};

    cout << "========================" << endl;
    cout << "   WEEKLY TEMPERATURES" << endl;

    for (int i = 0; i < 7; i++) {
        cout << temp[i] << " ";
    }

    cout << endl << "========================" << endl;

    return 0;
}
Code Tools

How It Works

  • The array stores 7 numbers in order.
  • The loop prints each temperature one by one.
  • Changing any value in the array updates the entire output.

Try it instantly in the DevsCall AI Code Runner.

Learn Together with AI

Ask your AI Copilot:

  • “Create a 2D array in C++ and print it as a table.”
  • “Show me how to use strlen and strcpy in C++.”
  • “Write a program that finds the highest value in an array.”

AI can help generate variations, fix errors, or explain new ideas.

Practice Time

Write a C++ program that:

  1. Creates a 1D array of 5 student marks.
  2. Loops through the array to print each mark.
  3. Calculates the total and average.
  4. Prints the result in this format:
text
6 lines
|
28/ 500 tokens
1
2
3
4
5
6
========================
   MARKS REPORT
   88 75 90 85 92
   Total: 430
   Average: 86
========================

For help, ask AI:

“How do I calculate the average of numbers stored in an array in C++?”