PRACTICAL NO. 1
Title: Swap two values using call by reference
Source code
#include<iostream.h>
#include<conio.h>
void swap (int &a,int &b);
void main()
{
int n1,n2;
clrscr();
cout<<”Enter the first value : “; cin>>n1;
cout<<”Enter the second value : “; cin>>n2;
cout<<”\n output ";
cout<<”\n Values before swap :”<<n1<<”\t”<<n2;
swap (n1,n2);
cout<<”\n Values after swap :”<<n1<<”\t”<<n2;
getch();
}
void swap (int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}
PRACTICAL NO. 2
Title: Reversing a string given by the user.
Source code
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
#include<string.h>
void reverse (char st[],int l);
void main ()
{
clrscr ();
char str [30];
int len;
cout <<”\n Enter the string to reverse : “;
gets(str);
cout <<”\n Original string is : “;
puts (str);
len=strlen (str);
cout<<”\n Output ";
reverse (str,len);
cout<<”\n Reverse string is : “;
puts (str);
getch();
}
void reverse (char st [], int l)
{
char t; int I ;
for( i=0; i<l/2 ; i++)
{
t=st[i];
st[i]=st [l-i-1];
st[l-i-1]=t;
}
}
PRACTICAL NO. 3
Title: Traversing an array using pointer.
Source code
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a[5]={3.1,5.2,7.4,10.2,15.4},s=0.0;
int i;
cout<<”\n Output "; cout<<”\n Starting element of the array is :”<<a;
cout<<”\n Size of the element to which it points “<<sizeof(*a)<<” in bytes\n”;
for(i=0;i<5;i++)
{
s=s+*(a+i);
cout<<”\n\nThe “<<i+1<<” element is “<<*(a+i)<<” and its address is “<<(a+i);
cout<<”\nThe sum of first “<<i+1<<” element (s) is“<<s;
}
cout<<”\n\nEnding element is “<<*(a+4)<<” and its address is “<<(a+4);
getch();
}
PRACTICAL NO. 4
Title: Searching a given number from an array using BINARY- SEARCH method.
Source code
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
float a[10]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1},x;
int lb=0,ub=9,mid,i;
cout<<”\n Array elements are: “;
for(i=0;i<10;i++)
{
cout<<a[i]<<” “;
}
cout<<”\n output ";
cout<<”\n Enter the value to be searched :”;
cin>>x;
while(lb<=ub)
{
mid=(lb+ub)/2; if (a[mid]==x)
{
cout<<”\n Element is found at location:”<<(mid+1);
getch();
exit(0);
}
else if(x>a[mid])
{
lb=mid+1;
}
else
{
ub=mid-1;
}
}
cout<<”\n The number is NOT found “;
getch();
}
PRACTICAL NO. 5
Title: Bubble sort.
Source code
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a[10] = {5.5,4.4,3.3,6.6,7.7,8.8,9.9,1.1,2.2,10.1},t;
int i,j;
cout<<”\n Output ";
cout<<”\n Array elements before sorting: \n”;
for(i=0; i<10;i++)
{
cout<<a[i]<<” “;
}
for(i=0; i<10; i++)
{
for(j=0;j<10-i-1;j++)
{
if(a[j] > a[j+1])
{
t=a[j];
a[j] =a[j+1]; a[j+1]=t;
}
}
}
cout<<”\n Array elements after sorting: \n”; for(i=0; i<10; i++)
{
cout<< a[i]<<” “;
}
getch();
}
PRACTICAL NO. 6
Title: Working with OOP concepts, class Ratio.
Source code
#include<iostream.h>
#include<conio.h>
class Ratio
{
int n,d;
public:
void assign(); double convert(); double invert(); void print();
};
void Ratio::assign()
{
cout<<"\n Enter the values of numerator and denominator: ";
cin>>n>>d;
}
double Ratio::convert()
{
return(double(n)/d);
}
double Ratio::invert()
{
return(double(d)/n);
}
void Ratio::print()
{
cout<<"\n The value of the ratio "<<n<<"/"<<d<<" is " <<convert()<<" and its reciprocal value is "<<invert();
}
void main()
{
clrscr();
Ratio R;
R.assign();
R.print();
}
0 Comments