ARRAY
Array is the collection of similar data types or collection of similar entity stored in contiguous memory location. Array of character is a string. Each data item of an array is called an element. And each element is unique and located in separated memory location. Each of elements of an array share a variable but each element having different index no. known as subscript.
An array can be a single dimensional or multi-dimensional and number of subscripts determines its dimension. And number of subscript is always starts with zero. One dimensional array is known as vector and two dimensional arrays are known as matrix.
ADVANTAGES:
array variable can store more than one value at a time where other variable can store one value at a time.
Example:
int arr[100];
DECLARATION OF AN ARRAY :
syntax is :
Data type array name [size];
Example:
int arr[100];
int mark[100];
int a[5]={10,20,30,100,5}
The declaration of an array tells the compiler that, the data type, name of the array, size of the array and for each element it occupies memory space. Like for int data type, it occupies 2 bytes for each element and for float it occupies 4 byte for each element etc. The size of the array operates the number of elements that can be stored in an array and it may be a int constant or constant int expression.
We can represent individual array as :
int ar[5];
ar[0], ar[1], ar[2], ar[3], ar[4];
Symbolic constant can also be used to specify the size of the array as:
#define SIZE 10;
INITIALIZATION OF AN ARRAY:
After declaration element of local array has garbage value. If it is global or static array then it will be automatically initialize with zero. An explicitly it can be initialize that
Data type array name [size] = {value1, value2, value3…}
Example:
in ar[5]={20,60,90, 100,120}
Array subscript always start from zero which is known as lower bound and upper value is known as upper bound and the last subscript value is one less than the size of array. Subscript can be an expression i.e. integer value. It can be any integer, integer constant, integer variable, integer expression or return value from functional call that yield integer value. So if i & j are not variable then the valid subscript are
ar [i*7],ar[i*i],ar[i++],ar[3];
The array elements are standing in continuous memory locations and the amount of storage required for hold the element depend in its size & type.
Total size in byte for 1D array is:
Total bytes=size of (data type) * size of array.
Example : if an array declared is:
int [20];
Total byte= 2 * 20 =40 byte.
ACCESSING OF ARRAY ELEMENT:
/*Write a program to input values into an array and display them*/
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5],i;
for(i=0;i<5;i++)
{
printf(“enter a value for arr[%d] \n”,i);
scanf(“%d”,&arr[i]);
}
printf(“the array elements are: \n”);
for (i=0;i<5;i++)
{
printf(“%d\t”,arr[i]);
}
getch();
}
OUTPUT:
Enter a value for arr[0] = 12
Enter a value for arr[1] =45
Enter a value for arr[2] =59
Enter a value for arr[3] =98
Enter a value for arr[4] =21
The array elements are 12 45 59 98 21
Example:
From the above example value stored in an array are and occupy its memory addresses 2000, 2002, 2004, 2006, 2008 respectively.
a[0]=12, a[1]=45, a[2]=59, a[3]=98, a[4]=21
/* Write a program to add 10 array elements */
#include<stdio.h>
void main()
{
int i ;
int arr [10];
int sum=o;
for (i=0; i<=9; i++)
{
printf (“enter the %d element \n”, i+1);
scanf (“%d”, &arr[i]);
}
for (i=0; i<=9; i++)
{
sum = sum + a[i];
}
printf (“the sum of 10 array elements is %d”, sum);
}
OUTPUT:
Enter a value for arr[0] =5
Enter a value for arr[1] =10
Enter a value for arr[2] =15
Enter a value for arr[3] =20
Enter a value for arr[4] =25
Enter a value for arr[5] =30
Enter a value for arr[6] =35
Enter a value for arr[7] =40
Enter a value for arr[8] =45
Enter a value for arr[9] =50
Sum = 275
while initializing a single dimensional array, it is optional to specify the size of array. If the size is omitted during initialization then the compiler assumes the size of array equal to the number of initializes.
For example:-
int marks[]={99,78,50,45,67,89};
If during the initialization of the number the initializes is less then size of array, then all the remaining elements of array are assigned value zero .
For example:-
int marks[5]={99,78};
Here the size of the array is 5 while there are only two initializes so After this initialization, the value of the rest elements are automatically occupied by zeros such as
Marks[0]=99 , Marks[1]=78 , Marks[2]=0, Marks[3]=0, Marks[4]=0
Again if we initialize an array like
int array[100]={0};
Then the all the element of the array will be initialized to zero. If the number of initializes is more than the size given in brackets then the compiler will show an error.
Array subscript always start from zero which is known as lower bound and upper value is known as upper bound and the last subscript value is one less than the size of array. Subscript can be an expression i.e. integer value. It can be any integer, integer constant, integer variable, integer expression or return value from functional call that yield integer value. So if i & j are not variable then the valid subscript are
ar [i*7],ar[i*i],ar[i++],ar[3];
The array elements are standing in continuous memory locations and the amount of storage required for hold the element depend in its size & type.
Total size in byte for 1D array is:
Total bytes=size of (data type) * size of array.
Example : if an array declared is:
int [20];
Total byte= 2 * 20 =40 byte.
ACCESSING OF ARRAY ELEMENT:
/*Write a program to input values into an array and display them*/
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5],i;
for(i=0;i<5;i++)
{
printf(“enter a value for arr[%d] \n”,i);
scanf(“%d”,&arr[i]);
}
printf(“the array elements are: \n”);
for (i=0;i<5;i++)
{
printf(“%d\t”,arr[i]);
}
getch();
}
OUTPUT:
Enter a value for arr[0] = 12
Enter a value for arr[1] =45
Enter a value for arr[2] =59
Enter a value for arr[3] =98
Enter a value for arr[4] =21
The array elements are 12 45 59 98 21
Example:
From the above example value stored in an array are and occupy its memory addresses 2000, 2002, 2004, 2006, 2008 respectively.
a[0]=12, a[1]=45, a[2]=59, a[3]=98, a[4]=21
/* Write a program to add 10 array elements */
#include<stdio.h>
void main()
{
int i ;
int arr [10];
int sum=o;
for (i=0; i<=9; i++)
{
printf (“enter the %d element \n”, i+1);
scanf (“%d”, &arr[i]);
}
for (i=0; i<=9; i++)
{
sum = sum + a[i];
}
printf (“the sum of 10 array elements is %d”, sum);
}
OUTPUT:
Enter a value for arr[0] =5
Enter a value for arr[1] =10
Enter a value for arr[2] =15
Enter a value for arr[3] =20
Enter a value for arr[4] =25
Enter a value for arr[5] =30
Enter a value for arr[6] =35
Enter a value for arr[7] =40
Enter a value for arr[8] =45
Enter a value for arr[9] =50
Sum = 275
while initializing a single dimensional array, it is optional to specify the size of array. If the size is omitted during initialization then the compiler assumes the size of array equal to the number of initializes.
For example:-
int marks[]={99,78,50,45,67,89};
If during the initialization of the number the initializes is less then size of array, then all the remaining elements of array are assigned value zero .
For example:-
int marks[5]={99,78};
Here the size of the array is 5 while there are only two initializes so After this initialization, the value of the rest elements are automatically occupied by zeros such as
Marks[0]=99 , Marks[1]=78 , Marks[2]=0, Marks[3]=0, Marks[4]=0
Again if we initialize an array like
int array[100]={0};
Then the all the element of the array will be initialized to zero. If the number of initializes is more than the size given in brackets then the compiler will show an error.