Accessing array elements

  • • Array elements are accessed by appending a subscript in square brackets []
  • • First element subscript is zero. i.e., array subscript starts from zero.
  • • Subscript is one less than array element, i.e., 5 for sixth element, 11 for 12th element, etc.

The following code segment calculates the summation of an integer array.

int a [] = {5,10,15,20,25};
int i, (i =0; i<; i++)
for (i = 0; i <5; i++)
     sum = sum + a[i];

Program Initializes the value in the array sequentially and then them out

#include <stdio.0>
    int main ()
    {
           int a [5];
           int i;
           for (i = 0; I <5; i ++)
           a [i] = I;
       for (i = 0; i <5; i ++)
            printf (“a [%d] = %d\n”, i, a [i]);
    }
Output

The number of subscripts determines the dimensionality of an array. For example, a [i] refers to an element of a non- dimensional array, a. similarly, b [i] [j] refers to an element of a two-dimensional array, b, etc.
Single operations which involve entire arrays are not permitted in C. Thus, if x and y are similar arrays (i.e., the same data type, dimensionality, and size) then assignment operations, comparison operations, etc. involving these two arrays must be carried out on an element-by-element basis. This is usually accomplished within a loop (or within nested loops, for multi-dimensional arrays).
The program listed below is a simple illustration of the use of arrays in C. The program reads a list of numbers, entered by the user, into a one-dimensional array, list, and then calculates the average of these numbers.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext