datastructure (2)_merged

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Index

SR.NO. PROGRAM PAGE REMARKS


NO.
1. Program to illustrate the concept of 1D 1
Array
2. Program to calculate the average of ‘n’ 2
numbers
3. Program to illustrate the concept of 2D 3
Array
4. Program to calculate the sum of all 4-5
elements of the matrix
5. Program to calculate the product of two 6-7
matrices
6. Program to insert element in an array 8-9
7. Program to delete element from an array 10-11
8. Program to illustrate the concept of Linear 12
Search
9. Program to illustrate the concept of Binary 13-14
Search
10. Program to illustrate the concept of Bubble 15-16
Sort
11. Program to illustrate the concept of 17-18
Selection Sort
12. Program to illustrate the concept of 19-20
Insertion Sort
1. Program to illustrate the concept of 1D array.

#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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy