CS Journal Final
CS Journal Final
Note: Kindly write the outputs on the blank page using “pencil” only.
Experiment #03
Aim: Write a program in C++ to sort an array of 10 integer numbers using the “bubble sort”
algorithm.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,j,c;
cout<<"Enter 10 numbers:";
for (i=0; i<10;i++)
{
cin>>a[i];
}
for (i=0;i<9;i++)
{
for (j=0; j<9;j++)
{
if (a[j]>a[j+1])
{
c=a [j];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
cout<<”\nSorted Elements are:";
for(i=0;i<10;i++)
{
cout<<endl<<a[i];
}
getch();
}
Output:
Enter 10 numbers:
Experiment #04
Aim: Write a program in C++ to initialize an array of 10 integer numbers. Search for a number
from this list using the “binary search” algorithm.
Code:
#include<iostream.h>
#include<conio.h>
if(x==a[mid])
{
cout<<"\nElement found at position:"<<mid+1;
break;
}
else if(x>a[mid])
{
low=mid+1;
}
else
if(low>high)
{
cout<<"\nElement not found!";
}
getch();
}
Output:
Enter 10 numbers:
10
90
98
76
65
43
54
32
21
12
Sorted Elements are:
10
21
32
43
54
65
76
Experiment #05
Aim: Write a C++ program to define a class called rectangle. Create member function called
area to calculate the area of rectangle. Use member variables as needed.
Code:
#include<iostream.h>
#include<conio.h>
class Rectangle Name of class
{ Opening of class
private: Access specifier
int length breadth;
public:
void getdata() Function definition
{
cout<<"Enter the length:”
cin>>length;
cout<<”\nEnter Breadth”;
cin>>breadth;
}
void area()
{
area=length *breadth;
cout<<"\nThe Area is = "<<area;
}
}; Closing class
void main() Execution of program start from here
{
clrscr();
Rectangle r1; Object r1 of class Rctangle is defined
r1.getdata(); getdata() function is called
r1.area(); area() function is called
getch();
}
Output:
Enter length: 10
Enter breadth: 20
Experiment #06
Aim: Write a program in C++ to implement the working of Constructor and Destructor in a class.
Use messages such as “object is born”, “object in use” and “object dies” to demonstrate the
output.
Code:
#include<iostream.h>
#include<conio.h>
class Ratio
{
public:
Ratio()//constructor
{
cout<<"Object is born"<<endl;
}
void print()
{
cout<<"\nObject is in use"<<endl;
}
~Ratio()//destructor
{
cout<<"\nObject dies"<<endl;
}
};
void main()
{
clrscr();
{
Ratio r;
r.print();
}
getch();
}
Output:
Object is born
Object is in use
Object dies
Experiment #7
Aim: Write a program in C++ to implement class ratio. Define member functions like assign(),
convert() and invert() outside the class using scope resolution operator.
Code:
#include<iostream.h>
#include<conio.h>
class ratio
{
public:
int num, den,c;
private:
void assign(int n, int d)
{
num=n;
den=d;
}
void convert(); //Function declaration
void invert();
};
void ratio::convert() //Function definition
{
cout<<”The ratio is:”<<num<<”/”<<den;
}
void ratio::invert()
{
c=num;
num=den;
den=c;
r.convert();
r.invert();
getch();
}
Output:
The ratio is: 7/22
The ratio after inversion is: 22/7
Experiment #8
Aim: Write a program in C++ to declare a class called Shape. Create another class called
Rectangle and inherit the functions of Shape to find area of a rectangle. Use member variables
as needed.
Code:
#include <iostream.h>
#include<conio.h>
class Shape //base class
{
public:
int l, b;
void getdata()
{ cout<<"Enter length:”;
cin>>l;
cout<<"\nEnter breadth:”;
cin>>b;
}
};
class Rectangle:public Shape //derived class
{
public:
void area()
{
cout<<"\nArea is:"<<l*b;
}
};
void main()
{
clrscr();
Rectangle r; //object creation of child class rectangle
r.getdata();
r.area();
getch();
Output:
Enter length:10
Enter breadth:2
Area is: 20
Experiment #09
Aim: Write a program in C++ to calculate area and circumference of circle using class Circle and
include default constructor in it.
Code:
#include<iostream.h>
#include<conio.h>
class Circle
{
private:
float r;
public:
Circle() //default constructor
{
r=0.0;
}
Circle(float rad) //parameterized constructor
{
r=rad;
}
void area()
{
cout<<"\n Area ="<<(3.14*r*r);
}
void circ()
{
cout<<"\n Circumference ="<<(2*3.14*r*r);
}
void main()
{
clrscr();
Circle obj(4);
obj.area();
getch();
}
Output:
Area =50.24
Circumference=25.12
Experiment #10
Aim: Write a program in C++ for Implementation of Constructor Overloading (Inheritance
Constructor).
Code:
#include<iostream.h>
#include<conio.h>
class Base
{
public:
Base() //base class default constructor
{
cout<<"\n Base class constructor";
}
};
class Derived : public Base
{
public :
Derived () //derived default constructor
{
cout<<"\n Derived class constructor";
}
Derived (int i) //derived parameterized constructor
{
cout<<"\n Value from main function: "<<i;
}
};
void main()
{
clrscr();
Base b1;
Derived d1;
Derived d2 (15);
getch();
}
Output:
Base class constructor
Base class constructor
Derived class constructor
Base class constructor
Value from main function: 15
Experiment #11
Aim: Write a program in C++ for printing start and end address of elements of an array along
with sum using pointer.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
float a[]={10.2,3.9,4.6,5.5,6.9,};
float *ptr,sum=0;
ptr=a;
clrscr();
cout<<"\nStarting address \t size \t ending address \t value of sum";
for(inti=0;i<5;i++)
{
sum=sum+*ptr;
cout<<"\n"<<ptr<<"\t\t"<<sizeof(*ptr)<<"\t";
ptr=ptr+1;
cout<<ptr<<"\t\t"<<sum;
}
getch();
}
Output: