🟡 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.

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 है।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में array क्या है?
Array एक ही type के elements का संग्रह है जो लगातार memory में रखा जाता है और index से access होता है। जैसे int marks[5]; पाँच integers रखता है, marks[0] से marks[4] से पहुँचा जाता है। इसका size declare करते समय तय हो जाता है।
C++ में array कैसे declare और initialise करते हैं?
आप type, नाम और size देते हैं, जैसे int a[5];, और साथ ही braces से initialise कर सकते हैं, जैसे int a[5] = {1, 2, 3, 4, 5};। अगर आप size से कम values दें, बचे elements zero हो जाते हैं।
C++ में array indexes 0 से क्यों शुरू होते हैं?
Array index असल में array की शुरुआत से offset है, तो पहला element offset 0 पर बैठता है। इसीलिए a[0] पहला item है और size n के array का आख़िरी item a[n-1] है।
C++ में array और vector में क्या अंतर है?
Built-in array का size compile time पर तय और स्थिर होता है और वह अपनी लंबाई नहीं जानता। std::vector run time पर बढ़-घट सकता है, .size() से अपना size जानता है, और आमतौर पर ज़्यादा सुरक्षित तथा सुविधाजनक है, इसीलिए आधुनिक C++ अक्सर इसे पसंद करता है।
C++ में array out of bounds access करने पर क्या होता है?
C++ array bounds नहीं जाँचता, तो अंत के पार पढ़ना या लिखना undefined behaviour है — यह garbage लौटा सकता है, दूसरा data खराब कर सकता है, या crash हो सकता है। आपको हर index 0 और array size घटा एक के बीच रखना होगा।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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