You are on page 1of 36

Experiment .

Name :-Spandan Mendhe

Class :- 12th

write a program in C++ that first initialises an array of given 10 real numbers. The
program must Sort numbers in scending order using Bubble-Sort method. It should print
the given list of numbers as well as the sorted list. */

#include<iostream.h>

#include<conio.h>

void main()

int a[10],i,j,t;

clrscr();

cout<<"\n Enter the array elements";

for(i=0;i<10;i++)

cin>>a[i];

cout<<"******OUTPUT******"<<endl;

cout<<"\n Array elements are:-";

for(i=0;i<10;i++)

cout<<" "<<a[i];
for(i=0;i<9;i++)

for(j=0;j<10-i;j++)

if(a[i]>a[j+1])
{
t=a[j]’

a[j]=a[j+1];

a[j+1=t;

cout<<endl;

cout<<"\n Ascending order:-";

for(i=0;i<10;i++)

cout<<" "<<<a[i];

getch();

Output:-

Enter the array elements 6 99 45 123 80 46 73 80 19 71

*******OUTPUT*******

Array elements are:- 6 99 45 123 80 46 73 80 19 71

Ascending order: 6 19 45 46 71 73 80 80 99 123


Experiment . 2

Name :-Spandan Mendhe

Class :- 12th

write a program in C++ that first initialises an array of given 10 real numbers. The
program must Sort numbers in descending order using Bubble-Sort method. It should
print the given list of numbers as well as the sorted list. /

#include<iostream.h>

#include<conio.h>

void main()

int a[10],i,j,t;

clrscr();

cout<<"\n Enter the array elements";

for(i=0;i<10;i++)

cin>>a[i];

cout<<"******OUTPUT******"<<endl;

cout<<"\n Array elements are:-";

for(i=0;i<10;i++)

cout<<" "<<a[i];

for(i=0;i<9;i++)
for(i=0;ij<10-i;j++)

if(a[j]<aa[j+]

t-alil:

a[j]=a[j+1];

a[j+1]=t;

cout<<endl;

cout<<"\n Descending order:-";

for(i=0;i<10;i++)

cout<<" "<<a[i];

getch();

Output:

Enter the array elements 6 99 45 123 80 46 73 80 19 71

******OUTPUT******

Array elements are:- 6 99 45 123 80 46 73 80 19 71

Descending order: 123 99 80 80 73 71 46 45 19 6


Experiment . 3
Name :-Spandan Mendhe

Class :- 12th

/ Write a program in C++ that exchanges data (passing by refrence) using swap function
to.

initerchange the given two numbers"/

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

void swap(int,int)://funcution decleration

void main()

clrscr();

int a, b;

cout<<"Enter the values of A and B : "

cin>>>>>b;

swap (&a,&b); //call by reference

cout<<"\n\t\tA= "<<<<"and";

cout<<"B"<<b;

geten(); }
void swap (int *a, int b) //function definition

int t;

ta; a=b; b

Output:-

/*Enter the values of A and B :- 23 45

A=45and B= 23
Experiment . 4
Name :-Spandan Mendhe

Class :- 12th

/a) Write a program in C++ that first initialises an array of given 10 sorted real numbers.
The program must verify whether a given element belongs this array or not, using
Binary Search technique. The element (to be searched) is to be entered at the time of
execution. if the number is found, the program should print its position in the array
otherwise it should print "The number not found." "/

#include<iostream.h>

#include<conio.h>

int main()
{
clrscr();

int i, arr[10], num, first, last, middle;

cout<<"Enter 10 Elements (in ascending order): ";

for(i=0; i<10;1++)

{ cin>>arr[i]; }

cout<<"\nEnter Element to be Search: ":

cin>>num;
first = 0;

last 9;

middle (first last)/2

while(first last)

{
if(arr[middlel<num)

first middle+1; =

else if(arr[middle]==num)

cout<<"\nThe number "<<num<<" found at Position "<<middle+1;

break;
}
else

last = middle-1;

middle = (first+last)/2;
}
if(first>last)

cout<<"\nThe number "<<num<<" is not found in given Array":


cout<<endl;

getch();

return 0;
}
/* Output one:

Enter 10 Elements (in ascending order): 15 16 17 18 19 20 21 22 23 24

Enter Element to be Search: 17

The number 17 found at Position 3

Output two :-

Enter 10 Elements (in ascending order): 90 91 92 93 94 95 96 97 98 99

Enter Eldprent to be Search: 33

The number 33 is not found in given Array "/


Experiment . 5
Name :-Spandan Mendhe

Class :- 12th

a) Write a program in C++ that first intialises an array of given numbers (short/ float/
double). The program must add these numbers by traversing this array with a pointer.
The output should print the starting address of the array with the size of the number (in
bytes) to whit it points. The program must also print the sum and pointer address with
the addition of every number as well as the ending address. */

#include<iostream.h>

#include<conio.h>

void main()

{ float a[5]={1,2,3,4,5),sum,y,*p,x=0;

int i;

clrscr();

cout<<"\n the given array is=\n”;

for(i=0;j<5;i++)

{ cout<<"\t"<<a[i]; }

p=&a[0];
cout<<"\n starting address of element :- "<<p<<"\n";

sum=0;

for(i=0;i<5;i++)

{ sum=sum+a[i];

p=&sum;

cout<<" sum offartay="<<sum;

cout<<"mending address= "<<p; 22

fori-05

{ p=&sum;

y=sizeof(*p);

x=x+y; } cout<<"\n size in bytes"<<x;

getch(); }

/* Output:-

the given array is=

1 2 3 4 5 starting address of element:- 0x7fe96674bc

sum of array =15


ending address=0x7fe96674b8

size in bytes 20 */
Experiment . 6

Name :-Spandan Mendhe

Class :- 12th

Write function in C++ to input the given string (including space) and reverse it using a
function which locate the end of a string and swaps the frist character witth the last
character,second character with the second last character and so on."/

#include <iostream.h>

#include <conio.h>

#include <string.h>

int main()

18/44

{ clrscr();

char a[80];

cout<<"\n\n\tEnter the string :-";

gets(a);

int lent = strlen(a);


int mid = lent/2, i;

char temp

for(i=0;mid; i++)

{ temp = a[i];

a[i] = a[lent - 1];

a[lent - 1] = temp;

lent--;

cout << "\n\tReverse of string is :-";

puts(a);

return 0;

/*Output;

Enter the string:- shlok0987

Reverse of string is :-7890kahtras */


Experiment . 7
Name :-Spandan Mendhe

Class :- 12th

/*a) write a program in Cwith a ratio class using member function like assign function to
initialize its member data, integer numerator and denominator. Convert/function to
convert the ratio into double, invert function to get the inverse of the ratio and print
function to print the ratio and its reciprocal. */

#include<iostream.h>

#include<conio.h> #include<stdio.h>

class ratio

int a:
int b:
double rat.iny:
public:

void assign(int x.int y)

{ a=x: b=y: }

void convert

{ rat (double)a/b: }:

void invert()

{ iny I/rat: }
void print()
{

cout<<"\n\n\tGiven Numbers are"<<<<"and"<<b:

cout<<"\n\n\tRatio and Inverse of ratio are"<<rat<<"and"<<inv:

};
void main
{

int x:y:

ratio A:

int

clrscr():

cout<<"\nEnter two integer numbers":

cin>>>>y:

Aassignts. A.convert():

A.invert():

A print()

getch():

return(0)
}
OUTPUT
Enter two integer numbers 25 5 Given Numbers are25and5
Ratio and Inverse of ratio are5and0.2
Experiment . 8

Name :-Spandan Mendhe

Class :- 12th

/* implement a circle class in c++. Each object of this class will represent a circle storing
its radius and x y coordinates of its centers as float. Include a default constructor,
access function, an area function and a circumference function, the program must print
the coordinates with radius,

area and circumference */

#include<iostream.h>

#include<conio.h>

class circle
{

float x, y, r

void access()

{ cout<<"\n\n\tRadius, X and Y coordinates of circleis :"<<r<<","<<x<<" and "<<y; }

void area()

{ cout<<"\n\n\t Area of the circle is :"<<(22.0/7.0)*r*r.) }

void circumference()

{ cout<<"\n\n\tCircumference of the circle is: :"<<2 (22.0/7.0) r }

public:
circle()

{ cout<<"Enter Radius, X and Y coordinates of circle::"

cin>>>>>>>y; }

void output()

void circle::output()

{ access(); area();circumference() }

clrscr();

circle A;

A.output();

getch();

return(0);

/*Enter Radius,X and Y coordinates of cir

Radius, X and Y coordinates of circleis ::5,0 and 0

Area of the circle is ::78.571429

Circumference of the circle is ::31.428571


Experiment . 9
Name :-Spandan Mendhe

Class :- 12th

/* Write a program in C++ with a complex constructor to add the given two complex
number A and B The program should print the given complex numbers and their sum. "/

#include<iostream.h>

#include<conio.h>

class complex

{` double real,imag.

public:

complex(){:}

complex(double x,double y)

{ real=x; imag-y. }

complex sum(complex.complex);

void output()

{
cout<<"\n\n\tThe Complex Number is "<<real<<"+"<<imag<<"";
}

};
complex complex::sum(complex x,complex y)
{ complex.z;
z.real-x.real+y.real;

z.imagex.imag+y.imag

return(z); }

main()

int x,y;

clrscr();

cout<<"\nEnter Real and Imaginary part of Complex number:"

cin>>>>>y;

complex A(x,y):

cout<<"\nEnter real and imaginary part of complex number:",

cin>>>>>y;

complex B(x,y);

complex C-A.sum(A,B);

cout<<"\n\nGiven complex numbers are";

A.output();

B.output();

cout<<"\n\n\nSum of complex number";

C.output();

getch();
return(0);

/*Enter Real and Imaginary part of Complex number ::6 91

Enter real and imaginary part of complex number::

Given complex numbers are

The Complex Number is 6+91

The Complex Number is 6+91

Sum of complex number

The Complex Number is 12+18 /


Experiment . 10

Name :-Spandan Mendhe

Class :- 12th

*Two classes Polar and Rectangle in C to represent points in the polar and rectangular
systems, conversion) routines from one system to the other."/

#include<iostream.h>

#include<math.h>

#include<stdio.h>

#include<conio.h>

class polarClass
{
public

float r,th,

polarClass(float a,float b),

{ r=a th=b; }

void show()

{ cout<<"in polar form:\n\t r"<<r<<" and theta "<<th; }

};

class rectangular Class


{
float x,V.

public:

rectangular Class(polar Class p)

{ xp.rcos(p.th).

Y p.r*sin(p.th), }

void show()

{ cout<<"\n\In Rectangular form :\n\tx "<<x<<"and y "<<y; }

};

int main()

{ clrscr();

float rad, thet;

cout<<"\n\tEnter the value of radius and theta: ";

cin>>rad>>thet;

polarClass p[rad,thet);

p.show();

rectangular Class r;

r=p;

r show();

getch(),
return 0;

/*Output:

free the the value radius and theta 35.6) In polar form

r- and theta 5.6

In Rectangular form:

x-2.326697and y-1.8938 */
Experiment . 11

Name :-Spandan Mendhe

Class :- 12th

/*Write a program in C++ to implement the following class hierarchy: Class Student to
obtain Roll number, Class Test to obtain marks scored in two different subjects, Class
Sports to obtain weightage (marks) in sports and Class Result to calculate the total
marks. The program must print the roll numbers, individual marks obtained in two
subjects, sports and total marks."/

#include<iostream.h>

#include<conio.h>

class student

protected:

int roll number,

public:

void get number (int a)

(roll_number=a; }

void put number (void)

(cout<<"\n\tRoll No:"<<roll_number<<endl;)

};
class test:public student

protected:

float part1, part2

public:

void get marks (float x, float y)

(part1= x; part2 = y.)

void put marks (void)

cout<<"\n\tMarks obtained: \n";


cout<<"\t\t\tSubject1 = "<<part1;

{ clrscr(). int roln

float prt1.pr12,spr

cout<<"\n\nEnter your Roll no.

cin>>roin;

cout<<"\nEnter yourof class test Subject1 and Subject2 score."

cin>>prt1>>prt2;

cout<<"\nEnter Sports Score

cin>>spr.
cout<<endl<<endl;

result R

R.get_number(roln):

R.get marks(prt1,p12),

Rget score(sor);

R.display:

getch().

return(0).
/*Output

Enter your Roll na

Enter yourof class tast Subject and Subject2 score: 9898

Enter Sports Score 95

Roll No:9

Marks obtained

Subject1-98

Subject2 98

Sports Score 95

Total 291

cout<<"\n\t\t\tSubject2= "<<part2;

}
};

class sports

protected:

float score:

public:

void get score (float S)

{score=S; }

void put score (void)

{ cout<<"\n\t\t\tSports Score "<<score, }

};

class result:public test, public sports

protected:

float total;

public:

void display (void);};

void result::display (void)

{
total part1+part2+score;

put_number();

put_marks();

put_score();

cout<<"\n\n\t\t\tTotal = "<<total; }

int main()
Experiment . 12

Name :-Spandan Mendhe

Class :- 12th

/Write a program in using sus function. The program must declare p to be a pointer to
object of the base class person: First the program must sign p to point an instance x
(the name of the person cg Bob) of class person. The program must then assign p to
point at an instance y (name of student ep. Tom) of the derived class student. a print
function in the base class such that it invokes the same base class function to print the
name of the person by default. The call for the same should envoke the derived class
function to print the name of the student."/

#include<iostream.h>

#include<conio.h>

class x

(char name[10]:

public: x()

1 strcpy(name, "Bob"); } //constructor

virtual void print()

{ cout<<"\n\tPersons name is name "cename;) };

class y public x

{ char name[10];

public:y() { strcpy(name, "Tom"); //constructor


void print() { cout<<"\n\tPersons name is "<< name; } };

void main()

{ clrscr();

xA; yB;

x*p; p-&A;

cout<<"\nP now point to base class":

p>print();p&B:cout<<"\n

P now point to derived class";

p>print();

getch(), }

/*Output: P now point to base class

Persons name is name Bob

P now point to derived class

Persons name is Tom"/


Experiment . 13

Name :-Spandan Mendhe

Class :- 12th

a)Write a program in C to read the name of the country from one text file and name of
its corresponding capital city from another text file. The program must display the
country name and indicate corresponding capital in the output b)Enter the program and
verify proper execution of the same on the computer c)Obtain the hardcopy of the
program listing as well as output.

#include fstream.h>

#include<conio.h>

void main()

clrscr();

ofstream fout:

fout.open("country"): fout<<"United States of America in"<<endl;

fout<<"United Kingdom in"<<endl;

fout<<"South Korea in"<<endl;

fout.close():

fout.open("capital"): fout"Washington "endl;

fout"London n" endl fout<<"Scoul n" endl:


fout.close():

const int 80:

char linen]: ilstream fin:

fin.opent "country"

cout<"Contents of country file: n

while(fin.cof) ()

fin.getline(line.n):

coutline<<endl

fin.close

fin.openCapital”);

cout Contents of capital file:n":

whiletin.cof) 0)

fin.getline(line.ny):

cout<<line<endl

fin.close():
getch();

OUTPUT

Contents of country file: United States of America

United Kingdom

South Korea

Contents of capital file:

Washington

London

Seoul
Experiment . 14

Name :-Spandan Mendhe

Class :- 12th

a)Write a program in C++ that initialises a Ratio class with no parameters as a default
constructor. The program must print the message." OBJECT IS BORN " during
initalisation. It should display the message "NOW X IS ALIVE", when the first member
function Ratio x is called. The program must misplay "OBJECT DIES" when the class
destructor is called for the object when it reaches the end of its scope

#include<iostream.h> #include<conio.h>

class ratio

public: ratio()

{ } cout<<"In OBJECT IS BORN\n";

ratio(int)

cout<<"\n NOW X IS ALIVE\n"; 1212

~ratio()
cout<<"In OBJECT IS DIES\n";

void main()

clrscr(); fatio r.

r=ratio(80);

getch();

Output:-

OBJECT IS BORN

NOW X IS ALIVE

OBJECT IS DIES

You might also like