envirementalb Logo
envirementalb Logo

what is Array and types of arrary

An array is a data structure that contains a series of elements . The  array is used to store the collection of data. but it is mostly  more useful to think about an array as a collection of variables of the same type .The elements are all of the related  data type in .The Array  is a  collection of data items in the sequential form all  the same types, accessed using a related  name in the array .one dimensional array is like the  single link list and the  two dimensional array is like a table mean in two dimensional array  rows and columns  are shown.in array the data is stored in the different indexes but in sequence or  series.There are different types of array.how to declare an array or method of declaring an array.The array is always start with the index (o).

Arrays in declarationin C Programming Language

The array declaration is as follow
 Syntax : Data_type  array_name  [Array_size] ;


Example 
    integer Array : int employ [50];
    char Array : char employ [50];
    float Array : float employ [50];
    double Array : double employ [50];

  1. The datatype can be int, float, char and etc, is used to store the element of an array.
  2.  The brackets [ ] are used to specify the array size.
  3. The array name specify the name of the array which we declare for execution.

Initialization of Arrays in C Programming Language

Array in C programming language can initialize in the compile time or in the run time.

Initilization of arry in compile time Initilization of arry in run time
#include<stdio.h>
int main()
{
    //integer array
    int employ[5] = {60, 70, 65, 80, 85};
    //float array
    float employ[5] = {60.0, 75.5, 80.0, 67.0, 83.5};
    return 0;
}
#include<stdio.h>
int main()
{
    int a;                          //size of array
    scanf("%d",&a);
    int student[a];                 //array
    for(int i=0; i<a; i++)
        scanf("%d",&emply[i]);
    return 0;
}

Accessing the elements of Arrays in C Programming

The elements of array access with the help of index number of the element. Noteable point is that the 1st element is present at the 0th index, 2nd element is present at the 1st index, and so on.


We understand this in the following example program

#include<stdio.h>
int main()
{
    int no;                                                 //size of array
    scanf("%d",&no);                                 //input function
    int employ[n];                                        //decleration of array
    int average, sum=0;
    for(int i=0; i<no; i++)
    {
        scanf("%d",&no[i]);                           //initialization of array
    }
    employ[2] = employ[2] + 7;                           //accessing 3rd element
    employ[4] = employ[4] + 7;                           //accessing 5th element
    
    for(int i=0; i<no; i++)
    {
        sum  = sum + employt[i];                           //accessing elements via loop
    }
    average = sum / no;                                     //calculating average
    
    for(int i=0; i<no; i++)
    {
        printf("Marks of student %d : %d\n", i+1,student[i]);        
    }
    printf("Sum is : %d\n",sum);
    printf("Average is : %d",average);
    return 0;
}

How to find the size of an array 

To check  the size of the  array in the bytes (one byte is contain on 8 bits) , you can use the sizeof operator.  int a[17]; The sizeof() operator will only work for the  fixed size of an array.The sizeof() is  based on the compilation  time information.The sizeof operator is used to to give the size of the variable which is declared in the array.