Cambo Freelance
ទំព័រដើមសេវាកម្មអត្ថបទការបង្រៀនក្រុមវគ្គបណ្តុះបណ្តាលទំនាក់ទំនង
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

ផ្ញើសាររបស់អ្នក

ត្រៀមចាប់ផ្តើមគម្រោងរបស់អ្នក? ទំនាក់ទំនងក្រុមរបស់យើង។

ទំនាក់ទំនង

សេវាកម្ម

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

តំណភ្ជាប់មានប្រយោជន៍

  • ទំព័រដើម
  • សេវាកម្ម
  • ការសិក្សា
  • អំពីយើង
  • ទំនាក់ទំនង
  • តម្លៃ

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Arrays
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 18 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++BeginnerLesson 18 of 50CamboFreelanceJune 19, 2026

C++ Arrays

Learn C++ arrays: declare, access, and iterate over single and multi-dimensional arrays. Find max/min with practical examples.


Tutorials › C++ › Arrays
Beginner8 min readLesson 18 of 50

In This Lesson

  • Declaring Arrays
  • Accessing Elements
  • Looping Arrays
  • Multi-dimensional
  • Quiz

What is an Array?

An array stores multiple values of the same type in a single variable. Elements are stored in contiguous memory.

// Syntax: type name[size] = {values};
int scores[5] = {90, 85, 78, 92, 88};

Accessing Elements

Access by index, starting at 0:

#include <iostream>
using namespace std;

int main() {
    int scores[5] = {90, 85, 78, 92, 88};
    cout << scores[0] << endl;   // 90 (first)
    cout << scores[4] << endl;   // 88 (last)
    scores[2] = 100;              // modify element
    cout << scores[2] << endl;   // 100
    return 0;
}

Looping Through an Array

int nums[] = {10, 20, 30, 40, 50};
int size = sizeof(nums) / sizeof(nums[0]);   // calculate size

for (int i = 0; i < size; i++) {
    cout << nums[i] << " ";
}
// Output: 10 20 30 40 50
// Range-based for (cleaner)
for (int n : nums) {
    cout << n << " ";
}

Multi-Dimensional Arrays

A 2D array is like a table of rows and columns:

int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        cout << matrix[i][j] << " ";
    }
    cout << endl;
}

Output:

1 2 3
4 5 6
Note: C++ does not check array bounds. Accessing arr[10] on a 5-element array is undefined behavior. Use std::vector or std::array for safe alternatives.

Exercise

Declare an array of 5 temperatures. Find and print the highest and lowest values.

Show Solution
#include <iostream>
using namespace std;

int main() {
    double temps[] = {36.5, 37.1, 38.0, 35.9, 37.5};
    int n = sizeof(temps) / sizeof(temps[0]);
    double highest = temps[0], lowest = temps[0];

    for (int i = 1; i < n; i++) {
        if (temps[i] > highest) highest = temps[i];
        if (temps[i] < lowest)  lowest  = temps[i];
    }
    cout << "Highest: " << highest << endl;
    cout << "Lowest: "  << lowest  << endl;
    return 0;
}

Quiz

  1. What is the index of the first element in a C++ array?

    • A) 1
    • B) -1
    • C) 0
    • D) It depends on the type
    Answer

    C) 0 — arrays are zero-indexed in C++.

  2. How do you calculate the size of an array int arr[]?

    • A) arr.size()
    • B) sizeof(arr) / sizeof(arr[0])
    • C) len(arr)
    • D) arr.length()
    Answer

    B) For C-style arrays, sizeof(arr) / sizeof(arr[0]) gives the element count.

Summary

  • Arrays store multiple values of the same type, indexed from 0.
  • Declare: type name[size] = {values};
  • Use sizeof(arr)/sizeof(arr[0]) to get array length.
  • 2D arrays use two index levels: arr[row][col].
← Previous: Break & Continue Next: C++ Functions →
c++arraysbeginnerdata-structures
PreviousLesson 17: C++ Break and ContinueNextLesson 19: C++ Functions
Back to All Tutorials