Write a function C++ that exchanges data (passing by reference) using swap function to
interchange the given two numbers
CODE
#include<iostream.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
clrscr();
int x,y;
cout<<"\nEnter Value of x : ";
cin>>x;
cout<<"Enter Value of y : ";
cin>>y;
cout<<"\nBefore Swaping : "<<endl;
cout<<"\t\tValue of X : "<<x<<endl;
cout<<"\t\tValue of Y : "<<y<<endl;
swap(&x,&y);
cout<<"\nAfter Swaping : "<<endl;
cout<<"\t\tValue of X : "<<x<<endl;
cout<<"\t\tValue of Y : "<<y<<endl;
getch();
}
void swap(int *x,int *y)
{
int temp;
temp =*x;
*x=*y;
*y=temp;
}
OUTPUT
Enter Value of x : 10
Enter Value of y : 12
Before Swaping :
Value of X : 10
Value of Y : 12
After Swaping :
Value of X : 12
Value of Y : 10
Write function in C++ to input given string (including spaces) & reverse it using a
function which locates end of string & swaps 1st character with last character, the
2nd character with 2nd last character & so on.
CODE
#include<iostream.h>
#include<conio.h>
#include<string.h>
const max = 10;
void reverse (char *p);
void main()
{
clrscr();
char a[max];
cout<<"Enter Multi Word String : ";
[Link](a,max,'\n');
cout<<"Entered String is : "<<a<<endl;
reverse(a);
cout<<"Reversed String is : "<<a<<endl;
getch();
}
void reverse(char *p)
{
int n;
n = strlen(p);
char *t =new char[n+1];
strcpy(t,p);
for (int i=0;i<n;i++)
p[n-1-i]=t[i];
}
OUTPUT
Enter Multi Word String : My name is JOHN
Entered String is : My name is JOHN
Reversed String is : NHOJ si eman yM
Write program in C++ that 1st initializes an array of given 10 sorted real numbers.
Program verify whether a given element belongs this array or not, using Binary Search
technique. The element (to be searched) is to be entered at time of execution. If number is
found, program prints its position in the array otherwise it should print “The Number is
Not Found”.
CODE
#include<iostream.h>
#include<conio.h>
const max = 10;
void main()
{
clrscr();
int mid,start,end,a[max],t,x,i,j,n;
start=0;
end=max-1;
cout<<"\nEnter "<<max<<" Element One By One : \n";
for(i=0;i<max;i++)
cin>>a[i];
cout<<"\nEntered "<<max<<" Element Are :";
for(i=0;i<max;i++)
cout<<" "<<a[i];
for(i=0;i<max-1;i++)
{ for(j=0;j<max-1-i;j++)
{ if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
cout<<"\nSorted "<<max<<" Element Are :";
for(i=0;i<max;i++)
cout<<" "<<a[i];
cout<<"\nEnterd No. to be Found : ";
cin>>n;
while(start<=end)
{ mid =(start+end)/2;
if(a[mid]==n)
{ cout<<"No. Found At Position : "<<mid+1;
break;
}
if(a[mid]<n)
{
start=mid+1;
}
if(a[mid]>n)
{
end=mid-1;
}
}
if(start>end)
{
cout<<" The Number is Not Found ";
}
getch();
}
OUTPUT 1
Enter 10 Element One By One : 9 5 4 6 1 3 2 7 8 10
Entered 10 Element Are : 9 5 4 6 1 3 2 7 8 10
Sorted 10 Element Are : 1 2 3 4 5 6 7 8 9 10
Enterd No. to be Found : 5
No. Found At Position : 5
OUTPUT 2
Enter 10 Element One By One : 9 5 4 6 1 3 2 7 8 10
Entered 10 Element Are : 9 5 4 6 1 3 2 7 8 10
Sorted 10 Element Are : 1 2 3 4 5 6 7 8 9 10
Enterd No. to be Found : 11
The Number is Not Found
Write a program in C++ that 1st initializes an array of given 10 real numbers. Program
must sort numbers in ascending / descending order using Bubble-Sort method. Print given
list of numbers as well as sorted list.
CODE
#include<iostream.h>
#include<conio.h>
const max = 10;
void main()
{
clrscr();
int a[max],i,j,t;
cout<<"Enter "<<max<<" No. One by One : ";
for(i=0;i<max;i++)
cin>>a[i];
cout<<"Entered "<<max<<" No. Are : ";
for(i=0;i<max;i++)
cout<<a[i]<<" ";
for(j=0;j<max-1;j++)
{ for(i=0;i<(max-1-j);i++)
{ if(a[i]>=a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}
}
cout<<"\nSorted "<<max<<" No. Are : ";
for(i=0;i<max;i++)
cout<<a[i]<<" ";
getch();
}
OUTPUT
Enter 10 No. One by One : 4 6 8 7 9 2 3 1 5 0
Entered 10 No. Are : 4 6 8 7 9 2 3 1 5 0
Sorted 10 No. Are : 0 1 2 3 4 5 6 7 8 9
Write a program in C++ that 1st initializes an array of given 5 given numbers (short / float /
double). Program must add these numbers by traversing this array with pointer. The
output should print starting address of array with size of number (in bytes) to which it
points. Program must also print sum & pointer address with addition of every numbers as
well as ending address.
CODE
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float *a,sum=0;
int i=0;
a= new float[5];
cout<<"Enter 5 Numbers \n";
for(i=0;i<5;i++)
cin>>*(a+i);
cout<<"Starting Address : "<<a;
cout<<"\nSize of the Number (in bytes) : “<<sizeof(float);
cout<<"\nNumber \t Address\n";
for(i=0;i<5;i++)
{
cout<<" \ "<<*(a+i)<<" \t "<<a+i<<endl;
sum=sum + *(a+i);
}
cout<<"Sum of 5 Number : "<<sum;
cout<<"\nEnding Address : "<<a+4;
getch();
}
OUTPUT
Enter 5 Numbers
1.1 2.2 3.3 4.4 5.5
Starting Address : 0x8f8912dc
Size of the Number (in bytes) : 4
Number Address
1.1 0x8f8912dc
2.2 0x8f8912e0
3.3 0x8f8912e4
4.4 0x8f8912e8
5.5 0x8f8912ec
Sum of 5 Number : 16.5
Ending Address : 0x8f8912ec
Write a program in C++ that initializes a Ratio class with no parameters as a default
constructor. The must print message “OBJECT IS BORN” during initialization. It should
display message “NOW X IS ALIVE”, when the first member function Ratio x is called.
The program must display “OBJECT DIES” when the class destructor is called for the
object when it reaches the end of its scope.
CODE
#include<iostream.h>
#include<conio.h>
class Ratio
{
public:
Ratio()
{
cout<<"OBJECT IS BORN"<<endl;
}
void x()
{
cout<<"NOW X IS ALIVE"<<endl;
}
~Ratio()
{
cout<<"OBJECT DIES"<<endl;
}
};
void main()
{
clrscr();
Ratio R;
R.x();
getch();
}
OUTPUT
OBJECT IS BORN
NOW X IS ALIVE
OBJECT DIES
Write program in C++ with a ratio class using member functions like assign() function to
initialize its member data (integer numerator & denominator), convert() function to
convert ratio into double, invert() function to get inverse of ratio & print() function to print
ratio & its reciprocal.
CODE
#include<iostream.h>
#include<conio.h>
class ratio
{
private:
int numerator, denominator;
double r1,r2;
public:
void assign()
{
cout<<"Enter Numerator : ";
cin>>numerator;
cout<<"Enter Denominator : ";
cin>>denominator;
}
void convert()
{
r1 = (double) numerator/denominator;
}
void invert()
{
r2 = (double) denominator/numerator;
}
void print()
{
cout<<"\nThe Original Ratio in double : "<<r1;
cout<<"\nReciprocal of Ratio in double : "<<r2;
}
};
void main()
{
clrscr();
ratio r1;
[Link]();
[Link]();
[Link]();
[Link]();
getch();
}
OUTPUT
Enter Numerator : 10
Enter Denominator : 15
The Original Ratio in double : 0.666667
Reciprocal of Ratio in double : 1.5