Wednesday, December 20, 2017

What is Element of Array in c language


Element of Array

The elements of an array can be accessed through the offsets of names. Then, they are counted, starting from zero. 
           This explains why the first element of an array is given a 0 value. For example, you can write ArrayName [ 0 ] . 
       If you choose to use the name LongArray, your first array element would be LongArray [ 0 ]  while your second array element would be LongArray [ 1 ]. 
        You will continue to count from that value until you reach your target destination.
Consider a scenario where you need to find out the average of 100 integer numbers entered by user.

In C, you have two ways to do this:

 1) Define 100 variables with int data type and then perform 100 scanf() operations to store the entered values in the variables and then at last calculate the average of them.

 2) Have a single integer array to store all the values, loop the array to store all the entered values in array and later calculate the average.
This section covers C programming examples on Arrays. Every example program includes the description of the program, C code as well as output of the program. 
            All examples are compiled and tested on a Linux system. These examples can be simple C programs or advanced C programs.

Various types of array :-

1). Initialization array
2). Multidimensional array
3). Array declaration 

Initialization Array 

When you declare an array, you may initialize it as a simple array, such as characters and integers. 
          See to it that you never forget to include the equal sign ( = ). You also have to include a series of values that are separated by commas. Always remember that you have to enclose everything within braces.

For example:-
                           int IntegerArray [ 5 ] = { 10, 20, 30, 40, 50};

In the above given sample statement, the IntegerArray is declared as an array with five integers. It allocates the value of 10 to IntegerArray [ 0 ] and the value of 20 to IntegerArray [ 1 ] . 
           The same process of allocating values goes on and on until the last IntegerArray. If you skip the array size, you produce an array that is big enough for initialization.

Multidimensional array:-

You can use arrays with more than one dimension in the C programming language. Take note that every dimension is represented as an array subscript. 
                 This means that a 2-dimensional array has two subscripts while a 3-dimensional array has three. 
   An array can have any number of dimensions, depending on what is necessary. However, it is most likely that you will create an array with 1 or 2 dimensions.

For example :-

SQUARE Cardboard [ 8 ] [ 8 ]

Array declaration :-

As the programmer, you are free to use any legal variable name you want for your array. However, you should avoid using a name that is the same as that of another variable and an array within the scope.                    When you declare arrays, you must define the type of object stored, the name of your array, and a subscript that contains the number of objects to be held in the array at all times.
  Take a look at the following examples to understand the concept further:

 int MyIntegerArray [ 90 ] ; long * ArrayOfPointersToLongs [ 100 ] ;

  If you decide to access the members of an array, you can use the subscript operator. 
       Again, consider the following examples: 

int theNinthInteger = MyIntegerArray [ 8 ] ; long * pLong = ArrayOfPointersToLongs [ 8 ] 

All the arrays are counted starting from zero. The arrays of n items are numbered from 0 to n – 1.  Read more


Please comment and follow this site blog inbox