🟡 Control Flow  ·  Lesson 21

Arrays in C++

What is an array?

An array stores many values of the same type in one named block, reached by an index. Instead of m1, m2, m3, m4, m5, you write int marks[5]; and use marks[0] to marks[4].

Declaring and initialising

C++
int a[5];                     // five ints, uninitialised
int b[5] = {1, 2, 3, 4, 5};   // with values
int c[5] = {1, 2};            // rest become 0
int d[]  = {10, 20, 30};      // size inferred as 3

Accessing elements

Indexes run from 0 to size − 1. The first is a[0]; the last of a 5-element array is a[4].

C++
int b[5] = {1, 2, 3, 4, 5};
cout << b[0] << " " << b[4] << "\n";   // 1 5
b[2] = 99;                              // change an element

Looping through an array

C++
int b[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) sum += b[i];
cout << "Sum = " << sum << "\n";

for (int x : b) cout << x << " ";   // range-based for
Output:
Sum = 15
1 2 3 4 5

2D arrays

A 2D array is a grid with two indexes — row and column:

C++
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) cout << grid[i][j] << " ";
    cout << "\n";
}
Output:
1 2 3
4 5 6

A note on std::vector

Built-in arrays have a fixed size and do not track their own length. For most real code, C++ programmers prefer std::vector, which grows as needed and knows its .size(). See the vector lesson.

💡 Rule of thumb

Learn arrays to understand the basics, but reach for std::vector when you actually build programs.

Common mistakes

  • Going out of bounds — using a[5] on a 5-element array.
  • Assuming an array knows its length inside a function (it does not).
  • Looping with <= size instead of < size.
  • Forgetting that uninitialised array elements hold garbage.
🏋️ Practice

Read five numbers into an array, then print the largest and the average. Then make a 3×3 grid and print its diagonal (grid[0][0], grid[1][1], grid[2][2]).

Summary

  • An array holds many same-type values, accessed by index from 0.
  • Initialise with braces; missing values become 0.
  • Loop with a classic for or a range-based for.
  • 2D arrays use two indexes for rows and columns.
  • Prefer std::vector for real programs — it is safer and resizable.

Frequently Asked Questions

What is an array in C++?
An array is a collection of elements of the same type stored in consecutive memory, accessed by an index. For example int marks[5]; holds five integers, reached with marks[0] through marks[4]. Its size is fixed when you declare it.
How do you declare and initialise an array in C++?
You give a type, name and size, such as int a[5];, and can initialise with braces at the same time, like int a[5] = {1, 2, 3, 4, 5};. If you supply fewer values than the size, the remaining elements are set to zero.
Why do array indexes start at 0 in C++?
An array index is really an offset from the start of the array, so the first element sits at offset 0. That is why a[0] is the first item and the last item of an array of size n is a[n-1].
What is the difference between an array and a vector in C++?
A built-in array has a fixed size set at compile time and does not know its own length. A std::vector can grow and shrink at run time, knows its size with .size(), and is generally safer and more convenient, which is why modern C++ often prefers it.
What happens if you access an array out of bounds in C++?
C++ does not check array bounds, so reading or writing past the end is undefined behaviour — it may return garbage, corrupt other data, or crash. You must keep every index between 0 and the array size minus one.

Array क्या है?

Array एक ही type की कई values को एक नामित block में रखता है, index से पहुँचा जाता है। m1, m2, m3, m4, m5 के बजाय, आप int marks[5]; लिखते हैं और marks[0] से marks[4] इस्तेमाल करते हैं।

Declare और initialise करना

C++
int a[5];                     // paanch ints, uninitialised
int b[5] = {1, 2, 3, 4, 5};   // values ke saath
int c[5] = {1, 2};            // baaki 0 ho jaate hain
int d[]  = {10, 20, 30};      // size 3 samajh liya jaata hai

Elements access करना

Indexes 0 से size − 1 तक चलते हैं। पहला a[0] है; 5-element array का आख़िरी a[4] है।

C++
int b[5] = {1, 2, 3, 4, 5};
cout << b[0] << " " << b[4] << "\n";   // 1 5
b[2] = 99;                              // element badlein

Array पर loop

C++
int b[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) sum += b[i];
cout << "Sum = " << sum << "\n";

for (int x : b) cout << x << " ";   // range-based for
Output:
Sum = 15
1 2 3 4 5

2D arrays

2D array दो indexes वाला grid है — row और column:

C++
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) cout << grid[i][j] << " ";
    cout << "\n";
}
Output:
1 2 3
4 5 6

std::vector पर एक नोट

Built-in arrays का size स्थिर होता है और वे अपनी लंबाई नहीं जानते। अधिकांश असली code के लिए, C++ programmers std::vector पसंद करते हैं, जो ज़रूरत अनुसार बढ़ता है और अपना .size() जानता है। vector lesson देखें।

💡 अंगूठे का नियम

Basics समझने को arrays सीखें, पर जब आप वाकई programs बनाएँ तब std::vector की ओर बढ़ें।

आम गलतियाँ

  • Bounds के बाहर जाना — 5-element array पर a[5] इस्तेमाल करना।
  • यह मानना कि array function के अंदर अपनी लंबाई जानता है (नहीं जानता)।
  • size के साथ < के बजाय <= से loop करना।
  • यह भूलना कि uninitialised array elements garbage रखते हैं।
🏋️ अभ्यास

पाँच numbers array में पढ़ें, फिर सबसे बड़ा और औसत print करें। फिर एक 3×3 grid बनाएँ और उसका diagonal print करें (grid[0][0], grid[1][1], grid[2][2])।

सारांश

  • Array कई same-type values रखता है, 0 से index द्वारा access।
  • Braces से initialise करें; छूटी values 0 हो जाती हैं।
  • Classic for या range-based for से loop करें।
  • 2D arrays rows और columns के लिए दो indexes इस्तेमाल करते हैं।
  • असली programs के लिए std::vector पसंद करें — यह सुरक्षित और resizable है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।