Ds File Akshay
Ds File Akshay
Engineering
SUBMITTED TO:
Ms. AMANDEEP KAUR SUBMITTED BY: Akshay
(1903094)
Akshay 1903094
INDEX
Akshay 1903094
13. WAP to enter and display a matrix
20. WAP to display addresses of elements of array using both array and pointers
Akshay 1903094
INDEX
21. WAP to insert and display data entered by using pointer notation
23. WAP to demonstrate the use of stack (implemented using Linked Lists) in evaluating
arithmetic expression in postfix notation
24. WAP to demonstrate the use of stack (implemented using array) in converting
arithmetic expression from infix to postfix notation
Akshay 1903094
34. WAP to bubble sort for integer
40. WAP to create a binary search tree and showing its preorder, post order and in order
traversal
EXPERIMENT 1
WAP TO INSERT AND DISPLAY ELEMENTS OF AN ARRAY.
ALGORITHM:-
[Initialize counter] Set K:=LB
Akshay 1903094
Repeat steps 3 & 4 while K<=LB
[Visit element] apply
[Increment counter] Set K:=K+1
[End of step 2 loop]
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int arr[5],n,i;
cout<<"enter the element of array";
cin>>n;
for(i=0;i<5;i++)
{
cin>>arr[i];
}
for(i=0;i<5;i++)
{
cout<<"\n elements of array"<<i<<" "<<arr[i];
}
getch();
}
OUTPUT:-
EXPERIMENT 2
Akshay 1903094
[Initialize] max:=arr[i] & loc:=0
Repeat step 3 & 4 while loc:=0 & max< arr[i]
For(i=1;i<=n;i++)
If (max<arr[i])
Then min=arr[i] & loc:=i
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[5],i,max,loc=0,n;
cout<<"enter the no.of elements";
cin>>n;
for(i=1;i<=n;i++)
{
cin>>arr[i];
}
max=arr[1];
for(i=1;i<=n;i++)
{if (max<arr[i])
max=arr[i];
loc=i;
}
cout<<"\n max="<<max;
cout<<"\n loc="<<loc;
getch();
}
OUTPUT:-
Akshay 1903094
EXPERIMENT 3
WAP TO FIND MIN. ELEMENT & ITS LOCATION IN AN ARRAY.
Akshay 1903094
ALGORITHM:-
Enter an elements of an array.
[initialize] loc:=0 & min=arr[1]
Repeat step 3 & 4 while loc:=0 & min>arr[i]
For( i=1;i<=n;i++)
If (min>arr[i] }
Then min=arr[i] &loc:=i
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[5],i,min,loc=0,n;
cout<<"enter the no.of an elements";
cin>>n;
for(i=1;i<=n;i++)
{
cin>>arr[i]; }
min=arr[i];
for(i=1;i<=n;i++)
{if (min>arr[i])
{min=arr[i];
loc=i;
}}
cout<<"\n min="<<min;
cout<<"\n loc="<<loc;
getch();
}
OUTPUT:-
EXPERIMENT 4
Akshay 1903094
Write a program to insert an element in an array in kth position
ALGORITHM :-
Enter an elements an array.
[Initialize counter] set J:=N
Repeat steps 3 & 4 while J>=K
[Move Jth element downwards] set LA [J+1] := LA [ J ]
[ Decrease counter ] set J =J-1
[ End of step 2 loop ]
[Insert elements ] set LA[K] := ITEM
[Reset N ] set N=N+1
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[10],n,i,iteam,k;
cout<<"enter the size of the element";
cin>>n;
cout<<"number of elements are";
for(i=1;i<=n;i++)
{cin>>a[i];
}
cout<<"elements of array";
for(i=1;i<=n;i++)
{
cout<<a[i[<<endl;
}
cout<<"position at which element is inserted";
cin>>k;
cout<<"enter new elements";
cin>>iteam;
for(i=n;i>=k;i--)
{a[i+1]=a[i]; }
n=n+1;
for(i=1;i<=n;i++)
{cout<<a[i];}
getch(): }
OUTPUT:-
Akshay 1903094
EXPERIMENT - 5
Akshay 1903094
WAP TO DELETE AN ARRAY ELEMENT AT KTH POSITION
ALGORITHM:-
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[10],n,i,m,k;
cout<<"number of elements are";
cin>>n;
for(i=1;i<=n;i++)
{cin>>a[i];
}
cout<<"enter the elements of array are";
for(i=1;i<=n;i++)
{cout<<a[i]<<endl;
}
cout<<"enter the element";
cin>>k;
m=a[i];
for(i=k;i<=n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
cout<<"elements are";
for(i=1;i<=n;i++)
{
cout<<a[i];
}
getch();}
OUTPUT:-
Akshay 1903094
Akshay 1903094
EXPERIMENT 6
SOURCE CODE-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[10],i,n,sum=0;
cout<<"enter the size of the elements";
cin>>n;
cout<<"enter the elements";
for(i=1;i<=n;i++)
{cin>>a[i];
}
cout<<"elements are";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
for(i=1;i<=n;i++)
{sum=sum+a[i];
}
cout<<"sum is"<<sum;
getch();
}
OUTPUT:-
Akshay 1903094
Akshay 1903094
EXPERIMENT 7
WAP TO COPY THE ELEMENTS OF ONE ARRAY INTO ANOTHER
ARRRAY.
ALGORITHM:-
Enter the elements of original array.
[Initialize counter] setK:=LB
Repeat steps 3 and 4 whileK<=UB
[Visit elements ]Apply PROCESS b[i]:=a[i]
[Increase counter ] Set K:=K+1
[ End of step 2 loop ]
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,a[10],b[10];
cout<<"\nEnter the number of elements of original array"<<endl;
cin>>n;
cout<<"\nEnter the elements of original array"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nElements of original array are as follows ";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
for(i=0;i<n;i++)
b[i]=a[i];
cout<<"\nElemets ofcopied array are as follows";
for(i=0;i<n;i++)
cout<<b[i]<<" ";
getch();
}
OUTPUT:-
Akshay 1903094
EXPERIMENT-8
Akshay 1903094
WAP TO SWAPPING OF TWO ARRAY USING THIRD ARRAY.
ALGORITHM:-
Enter an elements in first array and second array.
[ Initialise ] C[I]:=TEMP
[Move fist element array into TEMP array] C[i]:=A[
i] [Move second array elements into A[i] ] A[i] :=B[i]
[Move TEMO array elements into B[I] ] B[i]:=C[i]
[ end of step 2 loop ]
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,b[30],a[30],c[30];
cout<<"enter the number of elements in array="<<endl;
cin>>n;
cout<<"enter the elements of the first array =";
for(i=0;i<=n;i++)
{cin>>a[i];
}
cout<<"enter the elements of the second array =";
for(i=0;i<=n;i++)
{
cin>>b[i];
}
for(i=0;i<=n;i++)
{
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
cout<<"\n the elements of the first array after swapping are = ";
for(i=0;i<=n;i++)
{cout<<a[i]<<" ";
}
cout<<" \n the elements of the second array after swapping are =";
for(i=0;i<=n;i++)
{ cout<< b[i]<<" ";
}
getch(); }
OUTPUT:-
Akshay 1903094
EXPERIMENT 9
Akshay 1903094
WAP TO INSERT A ELEMENT END OF THE ARRAY
ALGORITHM:-
Enter the size of & element of array.
[initialize counter] set I:=
N [initialize variable]
ITEM.
Set a[i+1]=ITEM
[Reset N] N=N+1
Exit.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],n,item;
cout<<"enter the element of array";
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"enter the element at the end of the array";
cin>>item;
n=n+1;
for(i=1;i<n;i++)
{
a[n]=item;
}
n=n+1;
for(i=1;i<n;i++)
{
Akshay 1903094
cout<<a[i]<<endl;
}
getch();
}
OUTPUT:-
EXPERIMENT:10
WAP TO DELETE AN ELEMENT WHOSE VALUE IS GIVEN.
ALGORITHM:-
Akshay 1903094
Enter the size & element of array.
[Initialize counter ] I<=N.
[ Initialize variable] ITEM,LOC=0.
If ITEM=a[I].
[successful] set LOC=I.
Repeat for J=I to N-1.
[ Move J + 1st element upward] Set a[I] =a[ J+1].
[Reset the N number elements in array] Set N =N-1
Else ITEM is not in array .[Unsuccessful].
Exit.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],n,item,loc=0;
cout<<"enter the size of array";
cin>>n;
cout<<"enter the the data in array"<<endl;
for(i=1;i<=n;i++)
{
cin<<a[i];
}
cout<<"\n stored data in array ";
for(i=1;i<=n;i++)
{
cout<<" "<<a[i];
}
cout<<"\n given element ";
cin>>item; for(i=1;i<=n;i+
+)
{
if(a[i]==item)
{
loc=i;
}}
cout<<"item is present at "<<loc;
for(i=loc;i<=n-1;i++)
{
a[i]=a[i+1];
}
cout<<"\n resultant array";
n=n-1;
for(i=1;i<=n;i++)
{
cout<<" "<<a[i];
Akshay 1903094
}
getch();
}
OUTPUT:-
EXPERIMENT-11
WAP TO PERFORM LINEAR SEARCH IN AN ARRAY.
ALGORITHM:-
Enter the elements of an array.
Akshay 1903094
[ Initialize ] set count:=1 and LOC:=0
Repeat steps 3 & 4 while count <=N and LOC:=0
If
ITEM:= A[ I ] Then set LOC:=I
Else
set count := count++
[ End of step 2 loop ]
[Successful ]
If LOC:=0 Then
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
int n,a[20],i,ITEM,loc=0,count=1;
cout<<"Enter the no.of elements ofan array"<<endl;
cin>>n;
cout<<"Enter the elements of an array"<<endl;
for(i=1;i<=n;i++)
cin>>a[i];
cout<<"the elements are"<<endl;
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<"enter the ITEM to be searched"<<endl;
cin>>ITEM;
for(i=1;i<=n;i++)
{ if(a[i]==ITEM)
{ loc=i;
count++;
cout<<" position of item "<<loc<<endl; }} if(count!
=0)
cout<<"The ITEM is present ";
else
cout<<"ITEM is not present in an array"<<endl;
getch();
return 0; }
OUTPUT:-
Akshay 1903094
EXPERIMENT-12
WAP TO PERFORM BINARY SEARCH IN AN ARRAY
ALGORITHM:-
Akshay 1903094
Enter the elements of an array in sorted order.
BINARY(DATA,LB,UB,ITEM,LOC)
[Initialize segment variable] Set BEG:=LB,END:=UB and MID:=INT (BEG+EN/2)
Repeat steps 3 & 4 while BEG<=END and DATA[ MID ] != ITEM
If ITEM<DATA [MID] Then set END:=MID-1
Else
Set BEG:=MID+1
[End of IF statement]
Set MID:=INT [BEG+END/2]
[ End of step 2 loop]
If DATA[ MID ]:=ITEM then set LOC:=MID
Else
Set LOC:=NULL
[End of if structure ]
Exit
SOURCE CODE :-
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,n,beg=1,end,e,mid;
clrscr();
cout<<"Enter the number of elements in array : ";
cin>>n;
cout<<"Enter the elements in sorted order : ";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"Enter the element you want to search : ";
cin>>e;
end=n;
mid=(end+beg)/2; while((a[mid]!
=e)&&(beg<=end))
{
if(a[mid]==e)
{
cout<<"locis:"<<mid<<endl;
}
break;
if(e>a[mid])
{beg=mid+1;}
Akshay 1903094
else
{end=mid-1;}
mid=(end+beg)/2;
}
if(a[mid]==e)
{
cout<<"The element is found in "<<mid<<" position ";
}
else
{
cout<<"There is no such element.";
getch();
}
OUTPUT :-
EXPERIMENT 13
WAP TO ENTER AND DISPLAY A MATRIX.
ALGORITHM:-
Enter the values of matrix a[i][j].
Print a[i][j].
Akshay 1903094
Exit.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[2][2],i,j;
cout<<"Elements of matrix entered are:"<<endl;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
cin>>a[i][j];
}
cout<<endl;
}
cout<<"Elements of matrix displayed are:"<<endl;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
getch();
}
OUTPUT:-
Akshay 1903094
EXPERIMENT 14
WAP FOR ADDITION OF TWO MATRICES.
ALGORITHM:-
Suppose A and B are two matrix arrays of order m x n, and C is another matrix array
to store the addition result. i, j are counters
Akshay 1903094
Start
Read: m and n
Read: Take inputs for Matrix A[1:m, 1:n] and Matrix B[1:m, 1:n]
Repeat for i := 1 to m by 1:
Repeat for j := 1 to n by 1:
C[i, j] := A[i, j] + B[i, j]
[End of inner for loop]
[End of outer for loop]
Print: Matrix C
Exit.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10],i,n,j;
cout<<"Enter the size of array a:";
cin>>n;
cout<<"Enter elements:";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
cout<<endl;
}
cout<<"Enter the size of array b:";
cin>>n;
cout<<"Enter elements:";
Akshay 1903094
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
cout<<endl;
}
cout<<"Elements after adding:";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
cout<<endl;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
getch();
}
OUTPUT:-
Akshay 1903094
EXPERIMENT 15
WAP TO MULTIPLICATION OF TWO MATRICES
ALGORITHM:-
[Initialize] A(M,P) &B( P,N) matrix
Enter the elements of matrices A &B
Repeat steps 4 to 6 for I:=1 to M
Repeat step 5 to 6 for J:=1 to N
Akshay 1903094
Set C(I,J):=0 [Initializes C(i,J) ]
Repeat for K:=1 to P
C(I,J):=C(I,J)+A[I,K]*B[K,J]
[End of inner loop ]
[end of step 3 loop]
[ end of step 4 loop]
Exit
Source Code:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10],i,j,n;
cout<<"Enter size of array a:";
cin>>n;
cout<<"Enter elements:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
cout<<endl;
}
cout<<"Enter size of array b:";
cin>>n;
cout<<"Enter elements:"<<endl;
for(i=0;i<n;i++)
{
Akshay 1903094
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
cout<<endl;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]*b[i][j];
}
cout<<endl;
}
cout<<"Elements after multiplication:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<endl;
getch();
}
OUTPUT:-
Akshay 1903094
EXPERIMENT 16
WAP FOR TRANSPOSE OF MATRIX.
ALGORITHM:-
Akshay 1903094
Start.
Take inputs for Matrix a[i,j].
Print a [i , j]
Print a [j , i]
[Transpose is done]
Exit.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],i,j,n;
cout<<"Elements of matrix are:"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
cout<<endl;
}
cout<<"Transpose of matrix:"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[j][i]<<"\t";
}
cout<<endl;
}
Akshay 1903094
getch();
}
OUTPUT:-
EXPERIMENT 17
WAP TO ENTER AND DISPLAY THE 3D MATRIX.
ALGORITHM:-
Enter the elements of 3-D matrix.
Akshay 1903094
Repeat step 3 and 4 while I<3, J<2 and k<3 .
[Visit element] Apply PROCESS to A[I][J][K].
[Increase counter] Set I:=I+1 && J=J+1&& K=K+1.
Set a[I][J][k]=(i*2*3)+(j*3)+k.
Display matrix a[I][J][K].
[End of step 2 loop]
Exit.
Source code:-
#include<iostream.h>
#include<conio.h>
void main()
{
int a[3][2][3],i,j,k;
clrscr();
cout<<"enter the elements of 3D matrix"<<endl;
for(i=1;i<=3;i++)
{
for(j=1;j<=2;j++)
{
for(k=1;k<=3;k++)
{
cin>>a[i][j][k];
}}}
cout<<"Resultant 3D matrix "<<endl;
for(i=1;i<=3;i++)
{
cout<<i<<endl;
for(j=1;j<=2;j++)
Akshay 1903094
{
cout<<endl;
for(k=1;k<=3;k++)
{
a[i][j][k]=(i*2*3)+(j*3)+k;
cout<<"\t"<<a[i][j][k];
}}
cout<<endl<<endl;
}
getch();
}
Output:-
EXPERIMENT 18
WAP TO DEMONSTRATE THE USE OF STRUCTURE.
ALGORITHM:-
[Initialize structure] Using Struct keyword.
Akshay 1903094
[Initialize variable data types] character array NAME , integer AGE, float SALARY.
[Structure object] P1.
Display full name,age and salary respectively.
[ Input] Using function P1.V
Display Information .
Exit.
Source Code:-
#include<iostream.h>
#include<conio.h>
struct person
{
char name[20];
int age;
float salary;
};
void main()
{
clrscr();
person p1;
cout<<"enter full name: ";
cin.get(p1.name,50);
cout<<"enter age: ";
cin>>p1.age;
cout<<"enter salary: ";
cin>>p1.salary;
cout<<"\nDisplaying Information"<<endl;
cout<<"Name: "<<p1.name<<endl;
cout<<"age: "<<p1.age<<endl;
cout<<"salary: "<<p1.salary;
Akshay 1903094
getch();
}
Output:-
EXPERIMENT 19
WAP TO DEMONSTRATE THE USE OF C++ POINTERS.
ALGORITHM:-
Akshay 1903094
[Initialize variable] Using pointer *PC,C=5.
Display Address and Value Variable.
Set PC=&C.[Address Variable]
(ii)Display Address and Content that variable hold.
Reset C=11. Repeat step 3 (ii).
Then: Set *PC=2.[Content Address variable]
Repeat step 2.
Exit.
Souce Code:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *pc,c;
c=5;
cout<<"Addresss of c: "<<&c<<endl;
cout<<"value of c: "<<c<<endl;
pc=&c;
cout<<"Address that pc hold: "<<pc<<endl;
cout<<"contents of pc hold: "<<*pc<<endl;
c=11;
cout<<"Address pointer pc holds:"<<pc<<endl;
cout<<"content of the address pointer pc hold: "<<*pc<<endl;
*pc=2;
cout<<"Address of c: "<<&c<<endl;
cout<<"value of c: "<<endl;
Akshay 1903094
getch();
}
Output:-
EXPERIMENT 20
WAP TO DISPLAY ADDRESSES OF ELEMENTS OF ARRAY USING BOTH
ARRAY AND POINTERS.
ALGORITHM:-
[Initialize variable] Array size [3].
Akshay 1903094
[Initialize pointer] *PTR.
Display address using array.
for I from I=0 to 3.
Print Address variable array A[I].
Set PTR=A. Display address using pointer.
Repeat 3 and increment with counter I.
Exit.
Source Code:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a[3];
float*ptr;
cout<<"Dispaly address using arrays: "<<endl;
for(int i=0;i<3;i++)
{
cout<<"& A["<<i<<"]="<<&a[i]<<endl;
}
ptr=a;
cout<<"Display address using pointers: "<<endl;
for(i=0;i<3;i++)
{
cout<<"ptr+"<<i<<"="<<ptr+i<<endl;
}
getch();
}
Akshay 1903094
Output:-
Akshay 1903094