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 types. The Array is a  collection of data items in the sequential form of all the same types, accessed using a related name in the array .one dimensional array is like a single link list and the two-dimensional array is like a table mean in two-dimensional array rows and columns are shown.in the array, the data is stored in different indexes but in sequence or series. There are different types of arrays. how to declare an array or the method of declaring an array. The array always starts with the index (o).

Arrays in declaration C Programming Language

The array declaration is as follows
 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 data type can be int, float, char and etc, is used to store the elements of an array.
  2.  The brackets [ ] are used to specify the array size.
  3. The array name specifies 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.

Initialization of array in compile 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;
}

Initialization of array in run time

#include<stdio.h>
int main()
{
    int a;
    scanf("%d",&a);
    int student[a];           
    for(int i=0; i<a; i++)
        scanf("%d",&emply[i]);
    return 0;
}

 

Accessing the elements of Arrays in C Programming

The elements of the array access with the help of the index number of the element. Notable point is that the 1st element is present at the 0th index, the 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 contained 8 bits), you can use the size of the operator.  int a[17]; The size of the () operator will only work for the fixed size of an array. The size of () is based on the compilation time information. The size of the operator is used to give the size of the variable which is declared in the array.

Advertisements
Advertisements
Advertisements