All Programs C++ Class 12 HSC
All Programs C++ Class 12 HSC
a) Write a program in C++ that first initializes an array of given 10 real numbers.
The program must sort numbers in ascending order using Bubble Sort method. It
should print the given list of numbers as well as the sorted list.
b) Enter the program and verify proper execution of the same on the computer.
c) Obtained a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,z[10],n;
cout<<"enter any 10 no\n";
for(x=0;x<=9;x++)
{
cin>>z[x];
}
cout<<"number after sorting="<<"\n";
for(x=0;x<=8;x++)
{
for(y=0;y<=9-x;y++)
{
if(z[x]<z[y])
{
int temp;
temp=z[x];
z[x]=z[y];
z[y]=temp;
}}}
for(x=0;x<=9;x++)
{
cout<<z[x]<<"\t";
}
getch();
}
-----------------------------------------------------------------------
output
enter any 10 no
87 88 90 81 85 86 84 82 89 83
number after sorting=
81 82 83 84 85 86 87 88 89 90
Experiment No 2
a) Write a program in C++ that first initializes 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.
b) Enter the program and verify proper execution of the same on the computer.
c) Obtained a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,z[10],n;
cout<<"enter any 10 no";
for(x=0;x<=9;x++)
{
cin>>z[x];
}
cout<<"number after sorting="<<"\n";
for(x=0;x<=8;x++)
{
for(y=0;y<=9-x;y++)
{
if(z[x]>z[y])
{
int temp;
temp=z[x];
z[x]=z[y];
z[y]=temp;
}}}
for(x=0;x<=9;x++)
{
cout<<z[x]<<"\t";
}
getch();
}
-----------------------------------------------------------------------
output
enter any 10 no 81 82 83 84 85 86 87 88 89 90
90 89 88 87 86 85 84 83 82 81
Experiment No 3
a) Write a program in C++ that exchanges data (passing by reference) using swap
function to interchange the given two numbers.
b) Enter the program and verify proper execution of the same on the computer
c) Obtain hardcopy of the program listing as well as output. The output must list the
given numbers before as well as after swapping.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void swap(int*,int*);//function declaration
void main()
{
int a,b;
clrscr();
cout<<"\nEnter the values of A and B:";
cin>>a>>b;
swap(&a,&b);//call by reference
cout<<"\n\t\tA="<<a<<"and"; cout<<"B="<<b;
getch();
}
void swap(int *a,int *b)//function definition
{
int t;
t=*a;
*a=*b;
*b=t;
}
OUTPUT: Enter the values of A and B: 78 43
A=43 and B=78
Experiment No
a) Write a program in C++ that first initializes 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; i++)
{cin>>arr[i]; }
cout<<"\nEnter Element to be Search: ";
cin>>num;
first = 0;
last = 9;
middle = (first+last)/2;
output:-
Enter 10 Elements (in ascending order):1 2 3 4 5 6 7 8 9 10
a=4.3+j1.3
b=2.3+j7.2
c=6.6+j8.5
Experiment No
a) Write a program in C++ to implement the addition and division operator for the
ration class. Hence print the given two ratios x and y, their sum (x + y) and their
division (x / y).
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
class ratio
{
float a;
public:
void input()
{
cout << "\nEnter any real number: ";
cin >> a;
}
void output()
{
cout <<”\n”<< a;
}
ratio operator +(ratio);
ratio operator /(ratio);
};
ratio ratio :: operator +(ratio y)
{
ratio x;
x.a = a + y.a;
return(x);
}
ratio ratio :: operator /(ratio y)
{
ratio x;
x.a = a / y.a;
return(x);
}
void main()
{
ratio x, y, z;
clrscr();
x . input();
y . input();
z = x + y;
cout<<"\
n******************************OUTPUT***********************\n"
cout << "\n\tAddition is:";
z . output();
z = x / y;
cout << "\n\tDivision is: ";
z . output();
getch();
}
Enter any real number: 9
9
Enter any real number: 3
3
******************************OUTPUT***********************
Addition is: 12
Division is: 3
Experiment No 09
a) Write a program in C++ to implement the following class hierarchy Class student
to obtain Roll No., 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 no. individual marks obtained in two
subjects, sports and total marks.
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno;
public:
void getdata()
{
cout<<"enter the roll no. of student\n";
cin>>rno;
}
void putdata()
{
cout<<"student roll no.="<<rno<<endl;
}
};
class test:public student
{
protected:
int sub1,sub2;
public:
void getmarks()
{
cout<<"enter the marks of two subject"<<endl;
cin>>sub1>>sub2;
}
void putmarks()
{
cout<<"marks in subject1="<<sub1<<endl;
cout<<"marks in subject2="<<sub2<<endl;
}
};
class sports
{
protected:
int score;
public:
void getscore()
{
cout<<"\nenter the sports score"<<endl;
cin>>score;
}
void putscore()
{
cout<<"score of sports="<<score<<endl;
}
};
class result:public test,public sports
{
private:
int total;
public:
void display()
{
total=sub1+sub2+score;
cout<<"total="<<total<<endl;
}
};
void main()
{
result obj;
obj.getmarks();
obj.putmarks();
obj.getscore();
obj.putscore();
obj.display();
getch();
}
OUTPUT
enter the roll no. of student
105
student roll no.= 105
enter the marks of two subject 60 61
marks in subject1=60
marks in subject2=61
enter the sports score 62
total=183
Experiment No.
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<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
cout<<"program to print the country and the capital:\n"<<endl<<endl;
ofstream fout;
fout.open("country");
fout<<"united states of america"<<endl;
fout<<"india"<<endl;
fout<<"south korea"<<endl;
fout<<"\njapan";
fout<<"\nchnina";
fout.close();
fout.open("capital");
fout<<"\nWaasington DC";
fout<<"\ndelhi";
fout<<"\nseout";
fout<<"\ntokyo";
fout<<"\ntibet";
fout.close();
const N=80;
char line[N];
ifstream fin;
fin.open("country");
cout<<"content of the country file"<<endl<<endl;
while(fin)
{
fin.getline(line,N);
cout<<line<<endl;
}
fin.close();
fin.open("capitel");
cout<<"content of thecapital file"<<endl<<endl;
while(fin)
{
fin.getline(line,N);
cout<<line<<endl;
}
fin.close();
getch();
}
output
strcpy(name,"Bob");
}
virtual void print()
{
};
class y:public x
{
char name[10];
public: y()
{
strcpy(name,"Tom");
}
void print()
{
};
void main()
{
clrscr();
x A;
y B;
x *p; p=&A;
cout<<"\nP now point to base class";
p->print();
p=&B;
cout<<"\n\n\P now point to derived class";
p->print();
getch();
}
OUTPUT:
P now point to base class
Person name is Bob
P now point to derived class
Person name is Tom
Experiment No. 13
a) Write a function of C++ to input the given string (including spaces) and reverse it
using a function which locate the end of string and swap the first character with last
character.
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[80];
int j, k, n;
clrscr();
cout << "Enter any string including spaces\n\n";
cin . getline(str, 80);
n = strlen(str);
for(j=0, k=n-1; j<k; j++, k--)
{
char temp = str[j];
str[j] = str[k];
str[k] = temp;
}
cout << "\n\nThe reverse string is\n\n" << str;
getch();
}
#output:-
Enter the string:- nlamar
Reverse of string:- raman
Experiment No. 10
a) Two classes polar and rectangle in C++ to represent points in polar and rectangular
systems from one system to other.
b) Enter the program and verify proper execution of the same on computer.
c) Obtain a hardcopy of the program listing as well as output.
#include<iostream.h>
#include<stdio .h>
#include<conio.h>
class polarClass
{
public:
float r,th;
polarClass (float a, float b)
{ r=a; th=b;}
void show()
{
float x,y,r;
public:
rectangular Class(polar Class p)
{
x=p.r*cos(p.th);
y=r*sin(p.th);}
voidshow()
{ cout<<"\n\In Rectangularform:\n\tx="<<x<<"and y="<<y;} };
void main()
{drscr();
float rad, thet;
cout<<"\n\tEnter the value of radius and theta:";
cin>>rad>>thet;
polar class p(rad,thet);
p.show();
rectangularClass r(P);
r=p;
r.show();
geth();
}
OUTPUT:
Enter the value of radius and theta:44 23
In polar form:
r=44 and theta=23
In rectangular form:
x=-23.444653 and y=-4.002091e-10