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

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

एक सरणी एक चर है जो कई मानों को संग्रहीत कर सकता है। उदाहरण के लिए, यदि आप 100 पूर्णांक संग्रहीत करना चाहते हैं, तो आप इसके लिए एक सरणी बना सकते हैं।

 int data(100); 

ऐरे को कैसे घोषित करें?

 dataType arrayName (arraySize); 

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

 फ्लोट मार्क (5);

यहां, हमने फ्लोटिंग-पॉइंट प्रकार की एक सरणी, चिह्न, घोषित किया। और इसका आकार 5. अर्थ है, यह 5 फ्लोटिंग-पॉइंट मान रख सकता है।

यह नोट करना महत्वपूर्ण है कि एक बार घोषित होने के बाद किसी सरणी का आकार और प्रकार नहीं बदला जा सकता है।

ऐरे तत्वों तक पहुँचें

आप सूचक द्वारा सरणी के तत्वों तक पहुँच सकते हैं।

मान लीजिए आपने ऊपर के रूप में एक सरणी चिह्न घोषित किया है। पहला तत्व चिह्न (0) है, दूसरा तत्व चिह्न (1) और इसी तरह है।

कुछ keynotes :

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

कैसे एक सरणी को इनिशियलाइज़ करें?

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

 int mark(5) = (19, 10, 8, 17, 9);

आप इस तरह एक सरणी को इनिशियलाइज़ भी कर सकते हैं।

 int mark() = (19, 10, 8, 17, 9);

यहाँ, हमने आकार निर्दिष्ट नहीं किया है। हालाँकि, संकलक जानता है कि इसका आकार 5 है क्योंकि हम इसे 5 तत्वों के साथ शुरू कर रहे हैं।

यहाँ,

 मार्क (0) 19 मार्क के बराबर है (1) 10 मार्क के बराबर है (2) 8 मार्क के बराबर है (3) 17 मार्क के बराबर है (4) 9 के बराबर है

एरे तत्वों का मान बदलें

 int mark(5) = (19, 10, 8, 17, 9) // make the value of the third element to -1 mark(2) = -1; // make the value of the fifth element to 0 mark(4) = 0; 

इनपुट और आउटपुट एरे तत्व

यहां बताया गया है कि आप उपयोगकर्ता से इनपुट कैसे ले सकते हैं और इसे ऐरे तत्व में स्टोर कर सकते हैं।

 // take input and store it in the 3rd element scanf("%d", &mark(2)); // take input and store it in the ith element scanf("%d", &mark(i-1)); 

यहां बताया गया है कि आप किसी ऐरे के अलग-अलग एलिमेंट को कैसे प्रिंट कर सकते हैं।

 // print the first element of the array printf("%d", mark(0)); // print the third element of the array printf("%d", mark(2)); // print ith element of the array printf("%d", mark(i-1)); 

उदाहरण 1: एरियर इनपुट / आउटपुट

 // Program to take 5 values from the user and store them in an array // Print the elements stored in the array #include int main() ( int values(5); printf("Enter 5 integers: "); // taking input and storing it in an array for(int i = 0; i < 5; ++i) ( scanf("%d", &values(i)); ) printf("Displaying integers: "); // printing elements of an array for(int i = 0; i < 5; ++i) ( printf("%d", values(i)); ) return 0; ) 

आउटपुट

 5 पूर्णांक दर्ज करें: 1 -3 34 0 3 पूर्णांक प्रदर्शित करना: 1 -3 34 0 3 

यहां, हमने forउपयोगकर्ता से 5 इनपुट लेने और एक ऐरे में स्टोर करने के लिए एक लूप का उपयोग किया है । फिर, एक और forलूप का उपयोग करते हुए , इन तत्वों को स्क्रीन पर प्रदर्शित किया जाता है।

उदाहरण 2: औसत की गणना

 // Program to find the average of n numbers using arrays #include int main() ( int marks(10), i, n, sum = 0, average; printf("Enter number of elements: "); scanf("%d", &n); for(i=0; i  

Output

 Enter n: 5 Enter number1: 45 Enter number2: 35 Enter number3: 38 Enter number4: 31 Enter number5: 49 Average = 39 

Here, we have computed the average of n numbers entered by the user.

Access elements out of its bound!

Suppose you declared an array of 10 elements. Let's say,

 int testArray(10);

You can access the array elements from testArray(0) to testArray(9).

Now let's say if you try to access testArray(12). The element is not available. This may cause unexpected output (undefined behavior). Sometimes you might get an error and some other time your program may run correctly.

Hence, you should never access elements of an array outside of its bound.

Multidimensional arrays

In this tutorial, you learned about arrays. These arrays are called one-dimensional arrays.

In the next tutorial, you will learn about multidimensional arrays (array of an array).

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