Arrays are the fixed size, sequence collection of elements of same type.
It used to handle large volume of data in the term storing, Accessing and manipulating.
It is a derived data type, because we have to take help of primary data type when we use an array.without primary data type array can’t be used.
It shares common name for all its elements.
It works on index number and index begins with zero and ends with size -1.
Size of an array is known as subscript of an array.
It can be considered as set of elements stored in consecutive memory location but having same data type.
Types of Arrays
There are two types of array.
- single dimensional Array
- Double or Multi dimensional Array
Single dimensional Array:–
An Array which works on single index , is known as single dimensional array or 1- D Array.
It is used for searching , sorting etc.
Syntax :
Datatype Array_Name[size];
int ar[5];
Q.Find sum of five numbers without array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,d,e,f;
a=5;
b=7;
c=9;
d=11;
e=13;
f=a+b+c+d+e;
cout<<"Sum="<<f;
getch();
}
output
sum=45
Q.Find sum of Five numbers using array.(only for cocept)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],s;
a[0]=5;
a[1]=7;
a[2]=9;
a[3]=11;
a[4]=13;
s=a[0]+a[1]+a[2]+a[3]+a[4];
cout<<”Sum=”<<s;
getch();
}
Output
sum=45
OR
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5]={5,7,9,11,13},s;
s=a[0]+a[1]+a[2]+a[3]+a[4];
cout<<”Sum=”<<s;
getch();
}
Output
Sum=45
OR
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],s;
cout<<”Enter five numbers=”
cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4];
s=a[0]+a[1]+a[2]+a[3]+a[4];
cout<<”Sum=”<<s;
getch();
}
output
Enter five numbers=5
7
9
11
13
Sum=45
Q.Write a program to represent single dimensional array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],i;
cout<<”Enter five elements”;
for(i=0;i<5;i++)
{
cin>>a[i];
}
cout<<”Entered elements are”;
for(i=0;i<5;i++)
{
cout<<”\n”<<a[i];
}
getch();
}
Output
Enter five elements=11
12
13
14
15
Entered elements are
11
12
13
14
15
Q.Write a program to print Array’s elements in reverse order.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],i;
cout<<”Enter five element”;
for(i=0;i<5;i++)
{
cin>>a[i];
}
cout<<”Entered elements in reverse order”;
for(i=4;i>=0;i--)
{
cout<<”\n”<<a[i];
}
getch();
}
Output
Enter five elements
11
12
13
14
15
Entered elements in reverse order
15
14
13
12
11
Q.Write a program to enter number of elements in an array and print them
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],n,i;
cout<<”Enter number of elements=”;
cin>>n;
cout<<”Enter arrays elements=”;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<”Entered elements are”;
for(i=0;i<n;i++)
{
cout<<”\n”<<a[i];
}
getch();
}
output
Enter number of elements=5
Enter arrays elements=11
12
13
14
15
Entered elements are
11
12
13
14
15
Q.Write a program to enter number of elements in array and print them in reverse order.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],n,i;
cout<<”Enter number of elements=”;
cin>>n;
cout<<”Enter arrays elements=”;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<”Entered elements in reverse”;
for(i=n-1;i>=0;i--)
{
cout<<”\n”<<a[i];
}
getch();
}
output
Enter number of elements=5
Enter arrays elements=11
12
13
14
15
Entered elements in reverse
15
14
13
12
11
Q.Write a program to find sum of entered arrays elements.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],n,i,s=0;
cout<<”Enter number of elements=”;
cin>>n;
cout<<”Enter arrays elements=”;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<”Entered elements are”;
for(i=0;i<n;i++)
{
cout<<”\n”<<a[i];
s=s+a[i];
}
cout<<”\nSum of elements =”<<s;
getch();
}
output
Enter number of elements=5
Enter arrays elements=11
12
13
14
15
Entered elements are
11
12
13
14
15
sum of elements =65
Q.Write a program to find the smallest element in an array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],n,i,s;
cout<<"Enter number of elements=";
cin>>n;
cout<<"Enter arrays elements=";
for(i=0;i<n;i++)
cin>>a[i];
s=a[0];
for(i=1;i<n;i++)
{
if(s>a[i])
s=a[i];
}
cout<<"Smallest element ="<<s;
getch();
}
Output
Enter number of elements=5
Enter array’s elements=12
15
11
25
13
Smallest element=11
Q.Write program to find largest element in an array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],n,i,l;
cout<<”Enter number of elements=”;
cin>>n;
cout<<”Enter arrays elements=”;
for(i=0;i<n;i++)
cin>>a[i];
l=a[0];
for(i=1;i<n;i++)
{
if(l<a[i])
l=a[i];
}
cout<<”Largest element =”<<l;
getch();
}
Output
Enter number of elements=5
Enter array’s elements=12
15
11
25
13
Largest element=25
Q.Write a program to find largest and smallest element in an array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],n,i,l,s;
cout<<”Enter number of elements=”;
cin>>n;
cout<<”Enter arrays elements=”;
for(i=0;i<n;i++)
cin>>a[i];
s=a[0];
l=a[0];
for(i=1;i<n;i++)
{
if(s>a[i])
s=a[i];
if(l<a[i])
l=a[i];
}
cout<<”Smallest element=”<<s;
cout<<”Largest element =”<<l;
getch();
}
Output
Enter number of elements=5
Enter array’s elements=12
15
11
25
13
Smallest element=11
Largest element=25
Q.Write a program to represent Searching. OR Write a program to check particular is present or not in Array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],n,i,t,j=0;
cout<<”Enter number of elements=”;
cin>>n;
cout<<”Enter arrays elements=”;
for(i=0;i<n;i++)
cin>>a[i];
cout<<”Enter element which to be searched=”;
cin>>t;
for(i=0;i<n;i++)
{
if(t==a[i])
{
j=1;
break;
}
}
if(j==1)
cout<<”Element found at=”<<(i+1);
else
cout<<”Element not found”;
getch();
}
Output
Enter number of elements=5
Enter array’s elements=12
15
11
25
13
Enter element which to be searched=11
Element found at=3
Double Dimensional Array:-
An array which works on double index is called double dimensional array or 2 -D Array.
It is used for matrix manipulation or handle data in tabular form.
Syntax :
Datatype Array_Name[size][size];
int ar[5][5];
Q.Write a program to represent double dimensional array
to be continued…. Plz check for daily update….