A data structure is the way data is stored in the machine and the functions used to access that data. An easy way to think of a data structure is a collection of related data items. An array is a data structure that is a collection of variables of one type that are accessed through a common name. Each element of an array is given a number by which we can access that element which is called an index. It solves the problem of storing a large number of values and manipulating them.

Previously we use variables to store the values. To use the variables we have to declare the variable and initialize the variable i.e, assign the value to the variable. Suppose there are 1000 variables are present, so it is a tedious process to declare and initialize each and every variable and also to handle 1000 variables. To overcome this situation we use the concept of array .In an Array values of same type are stored.

An array is a group of memory locations related by the fact that they all have the same name and same type. To refer to a particular location or element in the array we specify the name to the array and position number of particular element in the array.
One Dimensional Array
Before using the array in the program it must be declared
data_type array_name[size];
data_type represents the type of elements present in the array.
array_name represents the name of the array.

Size represents the number of elements that can be stored in the array.

int age[100];
float sal[15];
char grade[20];

Here age is an integer type array, which can store 100 elements of integer type. The array sal is
floating type array of size 15, can hold float values. Grade is a character type array which holds
20 characters.

We can explicitly initialize arrays at the time of declaration.
data_type array_name[size]={value1, value2,……..valueN};

Value1, value2, valueN are the constant values known as initializers, which are assigned to the
array elements one after another.

int marks[5]={10,2,0,23,4};
The values of the array elements after this initialization are:
marks[0]=10, marks[1]=2, marks[2]=0, marks[3]=23, marks[4]=4

1. In 1-D arrays it is optional to specify the size of the array. If size is omitted during initialization then the compiler assumes the size of array equal to the number of initializers.

int marks[]={10,2,0,23,4};

Here the size of array marks is initialized to 5.

2. We can’t copy the elements of one array to another array by simply assigning it.
int a[5]={9,8,7,6,5};
int b[5];
b=a; //not valid
we have to copy all the elements by using for loop.

for(a=i; i<5; i++)

For processing arrays we mostly use for loop. The total no. of passes is equal to the no. of elements present in the array and in each pass one element is processed.

#include <stdio.h>
int a[3],i;
for(i=0;i<=2;i++) //Reading the array values
printf(“enter the elements”);
for(i=0;i<=2;i++) //display the array values

This program reads and displays 3 elements of integer type.

Share with : Share on Linkedin Share on Twitter Share on WhatsApp Share on Facebook