- Array is a data structure storing a group of elements , all of which are of same data type.
- All the elements of an array share the same name,and they are distinguished from one another with the help of an index.
- Random access to every element using a numeric index.
- A simple data structure,used for decades,which is extremely useful.
- Abstract Data type list is frequently associated with the array data structure.
The declaration of an array is just like any variable declaration with additional size or dimension.In the following section we will see how an array is declared.
Syntax of declaration is as follows :
data-type array_name[constant-size];
Data-type refers to the type of elements you want to store
Constant-size is the number of elements
The following are some declaration for arrays:
int char[80];
float farr[500];
static int iarr[80];
char charray[40];
There are two restrictions for using arrays in C :
- The amount of storage for a declared array had to be specified at compile time before execution.This means that an array has a fixed size.
- The data type of an array applies uniformly to all the elements ;for this reason,an array is called a homogeneous data structure.
The size of an array should be declared using symbolic constant rather a fixed integer quantity .The use of a symbolic constant makes it easier to modify a program that uses an array .All reference to maximize the array size can be altered simply by changing the value of the symbolic constant.
Example
Write a program to declare and read values in an array and display them.
#include<stdio.h>
#define SIZE 5
main()
{
int i=0;
int stud_marks[SIZE];
for(i=0;i<SIZE;i++)
{
printf("Element no.=%d",i+1);
printf("Enter the value of element");
scanf("%d",&stud_marks[i]);
}
printf("\n Following are the values stored in the corresponding array elements:\n\n ")
for(i=0;i<SIZE;i++)
{
printf("value stored in a [%d] is %d\n"i,stud_marks[i]);
}
}
OUTPUT
Element no.=1 Enter the Value of the element=11
Element no.=2 Enter the Value of the element=12
Element no.=3 Enter the Value of the element=13
Element no.=4 Enter the Value of the element=14
Element no.=5 Enter the Value of the element=15
Values stored in a corresponding array elements:
Value stored in a[0] is 11
Value stored in a[1] is 12
Value stored in a[2] is 13
Value stored in a[3] is 14
Value stored in a[4] is 15
0 comments:
Post a Comment