C ++ एरे (उदाहरण के साथ)

इस ट्यूटोरियल में, हम सरणियों के साथ काम करना सीखेंगे। हम उदाहरणों की सहायता से C ++ प्रोग्रामिंग में सरणी तत्वों को घोषित करना, आरंभ करना और एक्सेस करना सीखेंगे।

C ++ में, एक सरणी एक चर है जो एक ही प्रकार के कई मानों को संग्रहीत कर सकता है। उदाहरण के लिए,

मान लीजिए कि एक कक्षा में 27 छात्र हैं, और हमें उन सभी के ग्रेड को संग्रहीत करने की आवश्यकता है। 27 अलग-अलग चर बनाने के बजाय, हम बस एक सरणी बना सकते हैं:

 double grade(27);

यहां, ग्रेड एक ऐसा सरणी है जो अधिकतम 27 doubleप्रकार के तत्वों को पकड़ सकता है ।

C ++ में, ऐरे के आकार और प्रकार को उसके घोषणा के बाद नहीं बदला जा सकता है।

C ++ ऐरे घोषणा

 dataType arrayName(arraySize);

उदाहरण के लिए,

 int x(6);

यहाँ,

  • int - संग्रहित किए जाने वाले तत्व का प्रकार
  • x - सरणी का नाम
  • 6 - सरणी का आकार

C ++ एरियर में एक्सेस एलिमेंट्स

C ++ में, सरणी में प्रत्येक तत्व एक संख्या के साथ जुड़ा हुआ है। संख्या को एक सरणी सूचकांक के रूप में जाना जाता है। हम उन सूचकांकों का उपयोग करके किसी सरणी के तत्वों तक पहुँच सकते हैं।

 // syntax to access array elements array(index);

ऊपर दिखाए गए सरणी x पर विचार करें।

C ++ में एक सरणी के तत्व

याद रखने के लिए कुछ चीजें:

  • सरणी सूचकांकों के साथ शुरू होता है 0। अर्थ x (0) इंडेक्स पर संग्रहीत पहला तत्व है 0
  • यदि किसी सरणी का आकार है n, तो अंतिम तत्व इंडेक्स पर संग्रहीत है (n-1)। इस उदाहरण में, x (5) अंतिम तत्व है।
  • किसी सरणी के तत्वों में लगातार पते होते हैं। उदाहरण के लिए, मान लीजिए कि x(0)2120d का शुरुआती पता है। फिर, अगले तत्व x(1)का पता 2124d होगा, वसीयत का पता x(2)2128d और इसी तरह होगा।
    यहां, प्रत्येक तत्व का आकार 4 से बढ़ा है। ऐसा इसलिए है क्योंकि आकार int4 बाइट्स का है।

C ++ एरेना इनिशियलाइज़ेशन

सी ++ में, घोषणा के दौरान एक सरणी को इनिशियलाइज़ करना संभव है। उदाहरण के लिए,

 // declare and initialize and array int x(6) = (19, 10, 8, 17, 9, 15);
C ++ तत्व और उनका डेटा एरियर

घोषणा के दौरान सरणी को आरम्भ करने की एक और विधि:

 // declare and initialize an array int x() = (19, 10, 8, 17, 9, 15);

यहाँ, हमने सरणी के आकार का उल्लेख नहीं किया है। ऐसे मामलों में, कंपाइलर स्वचालित रूप से आकार की गणना करता है।

खाली सदस्यों के साथ C ++ सरणी

C ++ में, यदि किसी सरणी का आकार है n, तो हम सरणी में n संख्या के तत्वों को संग्रहीत कर सकते हैं। हालांकि, अगर हम तत्वों की संख्या से कम स्टोर करते हैं तो क्या होगा।

उदाहरण के लिए,

 // store only 3 elements in the array int x(6) = (19, 10, 8);

यहाँ, सरणी x का आकार है 6। हालाँकि, हमने इसे केवल 3 तत्वों के साथ आरंभीकृत किया है।

ऐसे मामलों में, कंपाइलर शेष स्थानों पर यादृच्छिक मान प्रदान करता है। अक्सर, यह यादृच्छिक मूल्य बस है 0

रिक्त सरणी सदस्यों को स्वचालित रूप से मान 0 असाइन किया जाता है

सरणी तत्वों को कैसे सम्मिलित और प्रिंट करें?

 int mark(5) = (19, 10, 8, 17, 9) // change 4th element to 9 mark(3) = 9; // take input from the user // store the value at third position cin>> mark(2); // take input from the user // insert at ith position cin>> mark(i-1); // print first element of the array cout <> mark(i-1);

उदाहरण 1: ऐरे तत्वों को प्रदर्शित करना

 #include using namespace std; int main() ( int numbers(5) = (7, 5, 6, 12, 35); cout << "The numbers are: "; // Printing array elements // using range based for loop for (const int &n : numbers) ( cout << n << " "; ) cout << "The numbers are: "; // Printing array elements // using traditional for loop for (int i = 0; i < 5; ++i) ( cout << numbers(i) << " "; ) return 0; )

आउटपुट

 संख्याएं हैं: 7 5 6 12 35 संख्याएं हैं: 7 5 6 12 35

यहाँ, हमने एक forलूप का उपयोग किया है जिससे इसे पुन: व्यवस्थित किया जा i = 0सके i = 4। प्रत्येक पुनरावृत्ति में, हमने मुद्रित किया है numbers(i)

We again used a range based for loop to print out the elements of the array. To learn more about this loop, check C++ Ranged for Loop.

Note: In our range based loop, we have used the code const int &n instead of int n as the range declaration. However, the const int &n is more preferred because:

  1. Using int n simply copies the array elements to the variable n during each iteration. This is not memory-efficient.
    &n, however, uses the memory address of the array elements to access their data without copying them to a new variable. This is memory-efficient.
  2. We are simply printing the array elements, not modifying them. Therefore, we use const so as not to accidentally change the values of the array.

Example 2: Take Inputs from User and Store Them in an Array

 #include using namespace std; int main() ( int numbers(5); cout << "Enter 5 numbers: " << endl; // store input from user to array for (int i = 0; i > numbers(i); ) cout << "The numbers are: "; // print array elements for (int n = 0; n < 5; ++n) ( cout << numbers(n) << " "; ) return 0; )

Output

 Enter 5 numbers: 11 12 13 14 15 The numbers are: 11 12 13 14 15

Once again, we have used a for loop to iterate from i = 0 to i = 4. In each iteration, we took an input from the user and stored it in numbers(i).

Then, we used another for loop to print all the array elements.

Example 3: Display Sum and Average of Array Elements Using for Loop

 #include using namespace std; int main() ( // initialize an array without specifying size double numbers() = (7, 5, 6, 12, 35, 27); double sum = 0; double count = 0; double average; cout << "The numbers are: "; // print array elements // use of range-based for loop for (const double &n : numbers) ( cout << n << " "; // calculate the sum sum += n; // count the no. of array elements ++count; ) // print the sum cout << "Their Sum = " << sum << endl; // find the average average = sum / count; cout << "Their Average = " << average << endl; return 0; )

Output

 The numbers are: 7 5 6 12 35 27 Their Sum = 92 Their Average = 15.3333

In this program:

  1. We have initialized a double array named numbers but without specifying its size. We also declared three double variables sum, count, and average.
    Here, sum =0 and count = 0.
  2. Then we used a range based for loop to print the array elements. In each iteration of the loop, we add the current array element to sum.
  3. हम 1प्रत्येक पुनरावृत्ति में गणना के मूल्य को भी बढ़ाते हैं , ताकि हम लूप के अंत तक सरणी का आकार प्राप्त कर सकें।
  4. सभी तत्वों को प्रिंट करने के बाद, हम सभी संख्याओं के योग और औसत को प्रिंट करते हैं। संख्याओं का औसत द्वारा दिया जाता हैaverage = sum / count;

नोट: हमने forसामान्य forलूप के बजाय एक लूप लूप का उपयोग किया।

एक सामान्य forलूप को हमें पुनरावृत्तियों की संख्या निर्दिष्ट करने की आवश्यकता होती है, जो कि सरणी के आकार द्वारा दिया गया है।

लेकिन एक forलूप वाले लूप को ऐसे विनिर्देशों की आवश्यकता नहीं होती है।

सी ++ अर्रे आउट ऑफ बाउंड्स

यदि हम आकार 10 की एक सरणी घोषित करते हैं, तो सरणी में 0 से 9 तक के तत्व शामिल होंगे।

हालांकि, अगर हम तत्व को इंडेक्स 10 या 10 से अधिक पर एक्सेस करने का प्रयास करते हैं, तो इसका परिणाम अपरिभाषित व्यवहार में होगा।

दिलचस्प लेख...