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:
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
1int marks[5] = {85, 90, 78, 92, 88};
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
1int grid[2][3] = { {1, 2, 3}, {4, 5, 6} };
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
1 2int sum = 0; for (int i = 0; i < 5; i++) sum += marks[i];
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
1char name[10] = "Ali";
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
1strlen(name); // returns length
Let’s Build the Temperature Tracker
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; }
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:
- Creates a 1D array of 5 student marks.
- Loops through the array to print each mark.
- Calculates the total and average.
- Prints the result in this format:
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++?”