datastructure (2)_merged
datastructure (2)_merged
datastructure (2)_merged
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[10],i,n;
cout<<"\n Enter the number of elements: ";
cin>>n;
cout<<"\n Enter elements : ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n Array : ";
for(i=0;i<n;i++)
{
cout<<"\n "<<a[i];
}
getch();
}
Output:-
1
2. Program to calculate the average of ‘n’ number .
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],n,i,sum=0,avg;
cout<<"\n Enter n: ";
cin>>n;
cout<<"\n Enter the elements : ";
for(i=0;i<n;i++)
{ cin>>a[i];
sum=sum+a[i];
}
avg=sum/n;
cout<<"\n Average ="<<avg;
getch();
}
Output:-
2
3. Program to illustrate the concept of 2D array.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[10][10],m,n,i,j;
cout<<"\n Enter the rows and columns : ";
cin>>m>>n;
cout<<"\n Enter the elements in array : ";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ cin>>a[i][j]; }
}
cout<<"\n 2D array : ";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ cout<<" \t"<<a[i][j]; }
cout<<" \n";
}
getch();
}
Output:-
3
4. Program to calculate the sum of all element of the matrix.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],s[10][10],m,n,i,j;
cout<<"\n Enter rows and columns : ";
cin>>m>>n;
cout<<"\n Enter the 1st matrix : ";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"\n Enter 2nd matrix : ";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}}
{
s[i][j]=a[i][j]+b[i][j];
4
}
cout<<"\n Sum of two matrix : ";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t"<<s[i][j];
}
cout<<"\n";
}
getch();
}
Output:-
5
5. Program to calculate the product of two matrix.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
cout<<"\n Order of matrix A : ";
cin>>m>>n;
cout<<"\n Order of matrix B : ";
cin>>p>>q;
if(n==p)
{
cout<<"\n Enter element of A : ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"\n Enter element of B : ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>b[i][j];
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
cout<<"\n After multiplication of A and B : ";
for(i=0;i<m;i++)
6
{
for(j=0;j<q;j++)
cout<<c[i][j]<<" ";
}
getch();
}
Output:-
7
6. Program to insert the element in the array.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[10],n,i,item,loc;
cout<<"\n Enter the number of elements: ";
cin>>n;
cout<<"\n Enter the elements in array : ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n Array : ";
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
cout<<"\n Enter element to insert: ";
cin>>item;
cout<<"\n Enter location : ";
cin>>loc;
for(i=n;i>=loc;i++)
{
a[i]=a[i-1];
}
a[i]=item;
n=n+1;
cout<<"\n Array after insertion : ";
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
8
getch();
}
Output:-
9
7. Program to delete element from array.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[10],n,i,item,loc;
cout<<"\n Enter the number of elements: ";
cin>>n;
cout<<"\n Enter the elements in array : ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n Array : ";
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
cout<<"\n Enter location : ";
cin>>loc;
item=a[loc];
cout<<"\n Element to be deleted: "<<item;
for(i=loc;i<n-2;i++)
{ a[i]=a[i+1];
}
n=n-1;
cout<<"\n Array after deletion : ";
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
getch();
}
10
Output:-
11
8. Program to illustrate the concept of linear search.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[10][10],m,n,i,j;
cout<<"\n Enter the rows and column : ";
cin>>m>>n;
cout<<"\n Enter the element in array : ";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ cin>>a[i][j]; }
}
cout<<"\n 2D array : ";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ cout<<a[i][j]<<"\t";
cout<<"\n";
}
}
getch();
}
Output:-
12
9. Program to illustrate the concept binary search .
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],n,loc=-1,item,mid,i;
cout<<"\n Enter total number of element : ";
cin>>n;
cout<<"\n Enter sorted number : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n Enter element to be sreached : ";
cin>>item;
int beg=0;
int end=n-1;
while(beg<=end && loc==-1)
{
mid=(beg+end)/2;
if (item==a[mid])
{
loc=mid;
cout<<"\n Element founded : "<<loc;
}
else if(item<a[mid])
end=mid-1;
else
beg=mid+1;
}
if(loc==-1)
cout<<"\n Element is not present ";
getch();
}
13
Output:-
14
10. Program to illustrate the concept of bubble sort .
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],k,i,n,temp;
cout<<"\n Enter the number of element : ";
cin>>n;
cout<<"\n Enter element : ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(k=1;k<n;k++)
{
i=0;
while(i<n-k)
{
if(a[i]>a[i+1]
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
i=i+1;
}
}
cout<<"\n Array : ";
for(i=0;i<n;i++)
{
cout<<"\n"<<a[i];
}
15
getch();
}
Output:-
16
11. Program to illustrate the concept selection sort .
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n,a[30],temp,j,loc,min;
clrscr();
cout<<"\n Enter the number of element : ";
cin>>n;
cout<<"\n Enter the element : ";
for(i=0;i<n;i++)
{ cin>>a[i];
}
for(i=0;i<n;i++)
{
min=i;
loc=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
cout<<"\n After sorting : ";
for(i=0;i<n;i++)
{
cout<<"\n"<<a[i];
}
getch();
}
17
Output:-
18
12. Program to illustrate the concept of insertion sort.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n,a[30],temp,j;
clrscr();
cout<<"\n Enter the number of element : ";
cin>>n;
cout<<"\n Enter element : ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while (temp<a[j] && j>=0)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
cout<<"\n After sorting : ";
for(i=0;i<n;i++)
{
cout<<"\n"<<a[i];
}
getch();
}
19
Output:-
20