Array - We
already know that a variable cannot store more than one data at a time. An array
is a group of same typed variables that are referenced by a common name. Arrays
can be any typed and one or more dimensions. A particular element can be
addressed by array name and index of the array enclosed in third bracket ([]).
Array can be declared with its initial values or can be defined with the size
and can input the values latter. An array index start from 0 by default.
int a[4]={1,2,3,4};
int a[10];
What are Arrays?
To store similar types of data
under a common variable name, instead of declaring N number of variables for
the N number of data, we use an array. An array is a collection of similar type
of data under a common variable. Thus we can have an Array of integers, floats,
characters (which are known as strings), but remember that all elements of any
given array must be of the same type, i.e. we cannot have an array of numbers
some of which are int and some float etc. The different components of an array
are called the Array Elements. Arrays also known as subscripted variable
How to Declare an Array:
Like all other variables, an
array needs to be declared first before it can be used. The declaration
consists of stating the type of data-type used in the array, the name of the
array and the number of elements in the array.
Data
Type Array_Variable[ number of elements ] ;
Example :
int age[20]; float salary[20];
char name[20];
In C char array indicate a
String.
Suppose
we declare an array if integer with 5 elements, N[5] then
N[0]
|
|
N[1]
|
|
N[2]
|
|
N[3]
|
|
N[4]
|
Here N is variable and [0]
to [4] is subscript. Above array also known as single dimensional
array. We can initialize the array with a list of values.
Initialization
can be done
when we use a fixed and known numbers of data but there is no shortcut method
to initialize large number of elements.
Multi dimensional Arrays:
Where Single Dimensional Arrays has one column and multiple rows,
multi-dimensional arrays has multiple rows as well as multiple columns. Multi-dimensional
data structure also known as matrices. A multi-dimensional array with 3 rows
and 3 columns is shown. There are two array subscripts. One subscript denotes
the row & the other the column.
N[0][0]
|
N[0][1]
|
N[0][2]
|
N[1][0]
|
N[1][1]
|
N[1][2]
|
N[2][0]
|
N[2][1]
|
N[2][2]
|
Array based Programs
:
Input five numbers in an array
and print the sum and average.
#include <conio.h>
#define MAX 5
//Sum and Average
int main()
{
int i,Arr[MAX],sum=0;
float avg;
printf("\nEnter Five No. :\n");
for(i=0;i<MAX;i++)
{
scanf("%d",&Arr[i]);
sum=sum+Arr[i];
}
avg=sum/5.0;
printf("\n\tNos. in Array :\n");
for(i=0;i<MAX;i++)
{
printf("\n\t\t%d",Arr[i]);
}
printf("\n\tSum : %d and Average : %f",sum,avg);
getch();
}
Input N numbers and print the
Maximum and Minimum value.
#include <conio.h>
//Maximum and Minimum
int main()
{
int Arr[20],i,n,max,min;
printf("\n\tEnter Array Size below 20 : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\tEnter Array Element at [%d] : ",i);
scanf("%d",&Arr[i]);
}
max=min=Arr[0];
printf("\n\tNumbers in Array : \n");
for(i=0;i<n;i++)
{
printf("\n\t\t%d",Arr[i]);
if(Arr[i]>max)
{
max=Arr[i];
}
if(Arr[i]<min)
{
min=Arr[i];
}
}
printf("\n\tMaximum : %d Minimum : %d",max,min);
getch();
return 0;
}
Input 5 numbers and reverse the
values in array.
#include <conio.h>
//Reverse
int main()
{
int Arr[20],i,n,k,j,tmp;
printf("\n\tEnter Array Size below 20 : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\tEnter Array Element at [%d] : ",i);
scanf("%d",&Arr[i]);
}
printf("\n\tNumbers in Array : \n");
for(i=0;i<n;i++)
{
printf("\n\t\t%d",Arr[i]);
}
for(i=0;i<n/2;i++)
{
tmp=Arr[i];
Arr[i]=Arr[n-i-1];
Arr[n-i-1]=tmp;
}
printf("\n\tNumbers After Reverse : \n");
for(i=0;i<n;i++)
{
printf("\n\t\t%d",Arr[i]);
}
getch();
return 0;
}
Input N numbers in an array,
while prevent any duplicate entry.
#include <conio.h>
//Prevent Duplicate Entry
int main()
{
int Arr[20],i,n,j;
printf("\n\tEnter Array Size below 20 : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\tEnter Array Element at [%d] : ",i);
scanf("%d",&Arr[i]);
for(j=0;j<i;j++)
{
if(Arr[i]==Arr[j])
{
printf("\n\t\t*Duplicate Entry **\n");
i--;
}
}
}
printf("\n\tNumbers in Array : \n");
for(i=0;i<n;i++)
{
printf("\n\t\t%d",Arr[i]);
}
getch();
return 0;
}
Print the Lucky Numbers up to N terms
#include <stdio.h>
#include <conio.h>
//Lucky Numbers
int main()
{
int
Arr[50],N,i,j,k,L;
printf("\nEnter
Size of N : ");
scanf("%d",&N);
printf("\nLucky
Number : \n");
if(N%2==0)
{
N=N+1;
}
for(i=0;i<N;i++)
{
Arr[i]=i+1;
}
L=1;
while(L<=N+1)
{
for(i=0;i<N;i++)
{
printf("%d
",Arr[i]);
}
printf("\n");
for(i=L;i<N;i=i+L)
{
for(j=i;j<N;j++)
{
Arr[j]=Arr[j+1];
}
N--;
}
L++;
}
getch();
return 0;
}
No comments:
Post a Comment