UNIT 4 (ARRAY)
*Array:-
Defination :-
1] Array is the collection of elements having similar data types.
2] Array can be of integer, float, character type.
If an array is declare as integer then it can have only integer values.
3] Array always starts with 0.
4] There are two types of ARRAY
a) One-D array
b) Two-D array
* Array Declaration and initialization
Following are ways to declaration and initialization of an array.
int a[4] ; a
float a[5]; a
int a[4] = {5,10,15,20};
0 1 2 3
• INDEX OF ARRAY
The sequence of element is represented by in index in an array
Index gives the position of any elements in the array. Index of
array is always starts with zero. Therefore the last index will be (n-
1)
For example:-
If the size of array is 5 then its 1st index will be zero. And last index
will be four.
Int a[5]= {2,4,6,8,10};
a[0]=2
a[1]=4
a[2]=6
a[3]=8
UNIT 4 (ARRAY)
a[4]=10
int a[5]=
*Variable which Hold address [internal pointer variable]
Note:-Internal pointer variable holds the base address i.e 2046,
2048, 2050, 2052,2054
Note:-
Why need loop in array?
➔ 100 or more elements need to read that’s why we need
loops or iteration process based on index of array.
*1-D ARRAY:-
Program :-
// write a program to initialize array and print in two
different ways i.e
//individual elements as will as all the elements.
#include<stdio.h>
int main()
{
int a[5]={20,30,50,60,80};
int i;
printf("\n first element=%d",a[0]);
printf("\n\n second element=%d",a[1]);
printf("\n\n third element=%d",a[2]);
printf("\n\n fourth element=%d",a[3]);
printf("\n\n fifth element=%d",a[4]);
printf("\n\n all the element of an array array\n\n");
for(i=0;i<5;i++)
UNIT 4 (ARRAY)
{
printf("%d",a[i]);
}
return 0;
}
Program:-
//program Array
#include<stdio.h>
int main()
{
int a[10],i; //loop to except element in an array
for(i=0;i<10;i++)
{
printf("enter a number");
scanf("%d",&a[i]); //scan index
}
//loop to display elements
for(i=0;i<10;i++)
{
printf("%d\n",a[i]); //scan address and print element
}
return 0;
}