Array In C Programming

 Array In C Programming


What is Array?

Array in C and C++ is a collection of similar data items stored at contiguous memory locations. The array elements are accessed randomly by accessing array indices. 

Array is a data structure of fixed size sequential collection of elements which have the same data type.

Properties of an Array:

1. Array is a variable which holds or stores similar data items or identical data values.

2. The elements of an array variable are stored at contiguous memory locations.

3. The size of an array variable is mentioned when it is declared.

4. The array elements are counted from zero index.

5. Array elements are accessed randomly by accessing its index position.

6. The array variables can have one or more dimensions.


Advantages of an Array:

1. Code Optimization: 

Less code is required to access array elements

2. Ease of Traversing:

The elements are searched and retrieved  from an array elements very easily

3. Ease of Sorting:

Sorting of array elements is very easy with very few lines of code.

4. Random Access:

It is very easy to access array elements randomly.


Disadvantages of Array:

Fixed  Size:

The array variable is declared with its size. The size whatever is defined for array variables we cannot exceed the length at run time. Dynamically  size cannot be changed at run time like linked list data structure.


Declaration of Array:

We can declare array variable by following way,

datatype  array_name[array_size];


datatype is used with array variables to mention what type of values or array elements are going to be stored, array_name  is the name of array variable to store the values, [array_size] the square bracket used to define fixed size of array variable.

For example,

int  number[5];

Here in this example array variable number is declared with its data type integer. The size of the array variable is defined as 5. i.e.  any 5 random values are going to be stored in the number variable. 

char ch[5];

In this example ch variable is declared with character data type and its size is 5. i.e.  5 characters are stored in ch variable.


Initialization of and Array elements:


int  number[5]={12,45,67,89,33};

Here array element number is declared with integer data type and its size is 5. So 5 random values are stored in an array. 

We can also initialise array elements as follows,

int  number[]={30,40,50,60,70};

Here in this example we didn't mention the size of an array in square brackets. As we have initialised 5 elements in an array so the compiler will consider the size of an above array is 5.


Access of an Array Elements:

We can access array elements by accessing its indices.

int  number[6]={30,40,50,60,70,80};

In the above example, the number array has its size 6. So array elements are stored at contiguous memory location as follows,


 0th Array Index=  First array element 30

1st Array Index=  Second array element 40

2nd Array Index=   Third array element 50

3rd Array Index=  Fourth array element 60

4th Array Index= Fifth array element 70

5th Array Index = Sixth array element

The array index always starts from 0. 




C program to initialise array elements and then display on screen

/*************************************

 * C program to initialise array elements and then display on screen

 * ***********************************************/


#include<stdio.h>

#include<conio.h>

void main()

{

    int i;

    int n[10]={10,20,30,40,50,60,70,80,90,100};

    clrscr();


    printf("The array elements are\n");

    for(i=0;i<10;i++)

    {

        printf("%d\t",n[i]);

    }

    printf("\nEnd of program");

}


The array elements are

10 20 30 40 50 60 70 80 90 100

End of program


Here array n is declared with integer data type and fixed size 10. For loop is used to access array elements from array n. Array index is always start with 0 so the initial value of i variable is initialised as 0, test condition is i is less than 10 because array size is 10 but array index is start with 0 so first element 10 of an array is stored at oth index, second element 20 is stored at 1st index, 3rd array element 30 is stored at 2nd index and so on in this way last array element 100 is stored at 9th index. Array elements are displayed on screen using printf("%d\t",n[i]); % format specifier is used because variable i is integer type, then n[i] is used to display array elements. Array n with loop variable i is subscripted because array indexes are accessed with for loop.  



Input and Output Array Elements:

Following is example to input array elements and display array elements on screen,

int n[10];

for(i=0;i<10;i++)

{

scanf(“%d”, &n[i]);

}

for(i=0;i<10;i++)

{

printf(“%d”, n[i]);

}

 Here array n is declared with size 10. To enter the array elements at the run time in the for loop scanf() is used. For loop help to access array index and at the contiguous memory location all array elements are stored in array n.

Again for loop is used to access array elements and display on screen. In the scanf() and printf() array is subscripted with loop variable i. It denotes the array index. In C and C ++ array elements are accessed withs its array indices.


C program to store 10 array elements in variable and then display on screen


/*************************************

 * C program to store 10 array elements in variable and then display on screen

 * ***********************************************/


#include<stdio.h>

#include<conio.h>

void main()

{

    int i,n[10];

    clrscr();

    printf("Which numbers you wants to store in n variable\n");

    for(i=0;i<10;i++)

    {

        scanf("%d",&n[i]);

        

    }

    printf("The array elements are\n");

    for(i=0;i<10;i++)

    {

        printf("%d\t",n[i]);

    }

    printf("\nEnd of program");

}


Which numbers you wants to store in n variable

10 20 30 40 50 60 70 80 90 100

The array elements are

10 20 30 40 50 60 70 80 90 100

End of program


टिप्पणी पोस्ट करा

0 टिप्पण्या