CS JournalprgmsFinal2024
CS JournalprgmsFinal2024
BUBBLE SORT
AIM: Write a program in C++ that first initializes an array of 10 numbers. The program
must sort numbers in ascending/ descending order using Bubble sort method. It should
print the given list of numbers as well as the sorted list.
Source Code:
#include <iostream.h>
#include <conio.h>
void main()
{
int a[10];
int i=0,k,j=0,temp=0;
clrscr();
cout<<"Enter 10 numbers :"<<endl;
for(i=0;i<=9;i++)
{
cin>>a[i];
}
clrscr();
cout<<"Original Array: "<<endl;
for(i=0;i<=9;i++)
{
cout<<a[i]<<" ";
}
//Bubble Sorting
for(k=1;k<10;++k) //Loop for Pass
{
for(j=0;j<=9-k;++j) //Loop for comparing array elements
{
if(a[j]>a[j+1])
{
temp=a[j]; //Interchange elements
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\n\nSorted Array: "<<endl;
for(i=0;i<=9;i++)
{
cout<<a[i]<<" ";
}
getch();
}
PROGRAM 2
BINARY SEARCH
AIM: Write a program in C++ that first initializes an array of 10 sorted numbers. The
program must verify whether a given element belong to 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”.
Source code:
#include<iostream.h>
#include<conio.h>
void main()
{
int A[10],i,Mid,item;
int B=0,E=9;
clrscr();
cout<<"\nEnter 10 integers in Ascending Order:\n";
for(i=0;i<=9;i++)
cin>>A[i];
clrscr();
cout<<"\nOriginal Array: ";
for(i=0;i<=9;i++)
cout<<A[i]<<" ";
/*Binary Search*/
Mid=(B+E)/2;
while(B<=E && item!=A[Mid])
{
if(item>A[Mid])
B=Mid+1;
else
E=Mid-1;
Mid=(B+E)/2;
}
if(item==A[Mid])
cout<<"\nITEM FOUND at Position: "<<Mid+1;
else
cout<<"\nITEM NOT FOUND";
getch();
}
PROGRAM 3
SWAPPING
AIM: Write a program in C++ that exchanges data (assign by reference) using swap
function to interchange the given numbers.
Source code:
#include <iostream.h>
#include <conio.h>
void main()
{
int x,y;
void swap(int *x,int *y);
x=100;
y=20;
cout<<"Before Swapping:\n";
cout<<"x = "<<x<<" y = "<<y<<endl;
swap(&x,&y);
cout<<"After Swapping:\n";
cout<<"x = "<<x<<" y = "<<y<<endl;
getch();
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
PROGRAM 4
CLASS
AIM: Write a program in C++ with a ratio class using member functions 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.
Source code:
#include <iostream.h>
#include <conio.h>
class ratio
{
private:
int num,den;
public:
void assign(int,int);
void convert();
void invert();
void print();
};
void ratio::assign(int n, int d)
{
num=n;
den=d;
cout<<"\nNumerator= "<<num<<endl;
cout<<"\nDenominator= "<<den<<endl;
}
void ratio::convert()
{
double con;
if(den>0)
{
con=double(num)/double(den);
}
cout<<" = "<<con<<endl;
}
void ratio::invert()
{
int temp;
temp=num;
num=den;
den=temp;
}
void ratio::print()
{
cout<<"\nRatio = "<<num<<"/"<<den;
convert();
invert();
cout<<"\nReciprocal = "<<num<<"/"<<den;
}
void main()
{
clrscr();
ratio a;
a.assign(22,7);
a.print();
getch();
}
PROGRAM 5
CLASS - Circle
AIM: Implement a class circle. Include a constructor in it which accepts value of radius
from user. Include 2 or more function in it, one which calculates area and circumference
and the other which prints answer.
Source code:
#include<iostream.h>
#include<conio.h>
class circle
{
private:
float r,c,a;
public:
circle();
void calculate();
void print();
};
circle::circle()
{
cout<<"\nEnter radius of circle: ";
cin>>r;
}
void circle::calculate()
{
c=2*3.14*r;
a=3.14*r*r;
}
void circle::print()
{
cout<<"\nCircumference of circle="<<c;
cout<<"\nArea of circle="<<a;
}
void main()
{
clrscr();
circle obj;
obj.calculate();
obj.print();
getch();
}
PROGRAM 6
AIM: Write a program in C++ to display a series of Fibonacci numbers using the
constructor. Write destructor member function to destroy class objects created using
constructor.
Source code:
#include <iostream.h>
#include <conio.h>
class fibonacci
{
private:
unsigned long int f,s,fib;
public:
fibonacci();
~fibonacci();
void increment();
void display();
};
fibonacci :: fibonacci()
{
f=-1;
s=1;
fib=f+s;
}
fibonacc i:: ~fibonacci()
{
cout<<”\nObject has been deleted”;
}
void Fibonacci :: increment()
{
f=s;
s=fib;
fib = f+s;
}
void fibonacci ::display()
{
cout<<fib<<", ";
}
void main()
{
fibonacci number;
cout<<”\nFIBONACCI SERIES:”;
for(int i=1;i<=15;i++)
{
number.display();
number.increment();
}
getch();
}
PROGRAM 7
OPERATOR OVERLOADING
AIM: Write a program in C++ to overload binary operator + for addition of complex
numbers A = 2. 5 + i 3.5 & B = 1.8 + i 4.6. The program should print the given complex
numbers and their sum.
Source code:
#include <iostream.h>
#include <conio.h>
class complex
{
float x,y;
public:
complex(){}
complex(float real, float imag)
{
x=real;
y=imag;
}
complex operator+(complex);
void display();
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display()
{
cout<<x<<" +j"<<y<<'\n';
}
void main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1 = ";
c1.display();
cout<<"c2 = ";
c2.display();
cout<<"c3 = ";
c3.display();
getch();
}
PROGRAM 8
INHERITANCE
AIM: 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 result to
calculate the total marks. The program must print the roll no., individual marks obtained in
two subjects, sports and total marks.
Source code:
#include <iostream.h>
#include <conio.h>
class student
{
protected:
int roll_no;
public:
void get_rollno(int a)
{
roll_no=a;
}
void put_rollno()
{
cout<<"Roll No. = "<<roll_no<<endl;
}
};
class test:public student
{
protected:
float sub1,sub2;
public:
void get_mark(float x, float y)
{
sub1=x;
sub2=y;
}
void put_mark()
{
cout<<"Marks obtained"<<endl;
cout<<" Subject1 = "<<sub1<<endl;
cout<<" Subject2 = "<<sub2<<endl;
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score()
{
cout<<" Sports weight: "<<score<<'\n';
}
};
class result:public test,public sports
{
float total;
public:
void display();
};
void result::display()
{
total=sub1+sub2+score;
put_rollno();
put_mark();
put_score();
cout<<" Total Score = "<<total<<'\n';
}
void main()
{
clrscr();
result r1;
r1.get_rollno(100);
r1.get_mark(25.0,45.5);
r1.get_score(8.0);
r1.display();
getch();
}
PROGRAM 9
VIRTUAL FUNCTION
AIM: Write a program in C++ using virtual function. The program must declare p to be a
pointer to objects of the base class person. First the program must assign p to point an
instance x(name of the person e.g. ‘BOB’) of class person. The program must then assign p
to point at an instance y(name of the student e.g. ‘TOM’) of the derived class student. Define
a display function in the base class such that it invokes the same base class function to print
the name of the person by default. The second call for the same should evoke the derived
class function to print the name of the student.
Source code:
#include <iostream.h>
#include <conio.h>
class person
{
public:
void display()
{
cout<<"\n Display Base";
}
virtual void show()
{
cout<<"\n Name of the person : BOB";
}
};
getch();
}
PROGRAM 10
AIM: Write a program in C++ that creates two files country that stores the names of three
countries and capital that stores the capital of corresponding countries. The program should
read the name of the country from one text file and name of its corresponding capital from
another text file. The program must display the country name and indicate its corresponding
capital in the output.
Source code:
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <process.h>
void main()
{
clrscr();
ofstream fout;
fout.open("country");
fout<<"India \n";
fout.close();
fout.open("capital");
fout<<"Washington DC \n";
fout<<"London \n";
fout<<"Delhi \n";
fout.close();
ifstream fin1,fin2;
fin1.open("country");
fin2.open("capital");
for(int i=1;i<=3;i++)
fin1.getline(line,size);
cout<<"Capital of "<<line<<endl;
if(fin1.eof()!=0)
{
cout<<"Exit from country\n";
exit(1);
}
if(fin2.eof()!=0)
{
cout<<"Exit from capital\n";
exit(1);
}
fin2.getline(line,size);
cout<<line<<endl;
}
getch();
}
PROGRAM 11
HTML
AIM: Create a simple HTML page on any of the following topics: College profile,
Computer Manufacturer or Software Development Company. The page must consist of at
least 3 paragraph of text. The page must have appropriate title, background colour or
background image and hyperlinks to other pages. The paragraphs must have text consisting
of different colours and styles in terms of alignment and font size.
College.html
<HTML>
<HEAD>
<TITLE>College Profile</TITLE>
</HEAD>
<BODY bgcolor=WhiteSmoke>
<H1 align="center">St. Anne's Junior College</H1>
<hr>
<P align="center"><font color="red" size="+1">
St. Annes Jr. College is a highly reputed educational institution run by the sisters of St
Annes Congregation, Tirchirapalli.</font></p>
<P align="left"><FONT color="purple" size="+3">
Regularity and punctuality is observed in this institute. 75% attendance is compulsory for
all the students, failing which may not be qualified for examination.
</FONT></P>
<P align="right"><FONT color="green" size="+4">St. Anne's Jr. College has several
subjects.</FONT></P>
orgboard.html
<HTML>
<HEAD>
<TITLE>Organising Board</TITLE>
</HEAD>
<BODY bgcolor="yellow">
<FONT color="blue" size="20">
<UL> Organising Board
<li>Treasurer - Abc
<li>Principal - Xyz
<li>Incharge - Pqr
</UL>
</FONT>
</BODY>
</HTML>
Subjects.html
<HTML>
<HEAD><TITLE>Junior College Subjects</TITLE></HEAD>
<BODY bgcolor="grey">
<OL type="I">
<li>Science
<li>Commerce
<li>Arts
</OL>
</FONT>
</BODY>
</HTML>
College.html
Orgboard.html
Subjects.html
PROGRAM 12
AIM: Create a simple HTML page on any of the following topics: College profile,
Computer Manufacturer or Software Development Company. The page must consist of a
scrolling marquee displaying an appropriate message. The page must include a table with
at least 5 rows and 3 columns having merged cells at least at 1 place. The page must also
display an image, such that when the same is viewed through a browser and the mouse is
placed on the image, an appropriate text message should be displayed. The image itself
should also act as hyperlink to another page.
Collegeprofile.html
<HTML>
<HEAD>
<TITLE>College Profile</TITLE>
</HEAD>
<BODY>
<FONT color="blue" size="24">
<MARQUEE>St. Anne's Junior College</MARQUEE></FONT>
<TABLE align=center border=4 cellspacing=6 cellpadding=6>
<CAPTION align="center"><B>Mark Sheet</B></CAPTION>
<TR>
<TH rowspan=2>
<TH colspan=3 align=center>Year
</TR>
<TR>
<TH>2012
<TH>2013
<TH>2014
</TR>
<TR>
<TH>No. of students appeared
<TD>100
<TD>100
<TD>200
</TR>
<TR>
<TH>Distinction
<TD>10
<TD>20
<TD>30
</TR>
<TR>
<TH>First Class
<TD>50
<TD>57
<TD>90
</TR>
<TR>
<TH>Second Class
<TD>38
<TD>20
<TD>80
</TR>
<TR>
<TH>Failed
<TD>2
<TD>3
<TD>0
</TR>
</TABLE>
<A href="education.html">
<img src="education.jpg" width=200 height=200 border=1 alt="This is drop
image"></A>
</BODY>
</HTML>