Professional Documents
Culture Documents
cs1 Lab Submission File
cs1 Lab Submission File
cs1 Lab Submission File
Algorithm:
Input: An array of integers
Output: Sorted array of integers in ascending order
Steps:
1. Start
2. Take any ‘n’ numbers from the user into an array
3. for (n-1) passes
4. for all elements of the unsorted array
a. if array[j] > array[j+1]
b. swap(array[j], array[j+1])
c. end if
5. end for
6. end for
7. Display the sorted array to the user
8. Stop
Function swap(int x,int y)
1. Start
2. Exchange the data using temporary variable
3. Stop
FlowChart:
Start
Pass
number=1
Is pass Yes
Display the
number
sorted array
> (n-1)
No
Stop
Yes No
No
Is
a[j]>a[j+1] J++
Swap a[j]
and a[j+1]
/*Bubble Sort*/
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,j,k,t;
clrscr();
cout<<"Enter 10 Numbers to sort :";
for(i=0;i<10;i++)
cin>>a[i];
cout<<"Array Elements :"<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<"\t";
for(k=1;k<10;k++)
{
for(j=0;j<10-k;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
cout<<"Sorted Array :"<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<"\t";
getch();
}
OUTPUT:
Practical 2:
Exchanging data of two integers using pointers
Algorithm:
Input: 2 numbers
Output: Data of the 2 numbers exchanged
Steps:
1. Start
2. Take 2 numbers from the user
3. Display the numbers before swapping
4. Call the function ‘swap’ with the address of the 2 numbers being sent as
parameters(Call by reference)
5. Display the numbers after swapping
6. Stop
FlowChart:
/*Swap two numbers using pointers*/
#include<iostream.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int n1,n2;
clrscr();
cout<<"Enter Number 1 :";
cin>>n1;
cout<<"Enter Number 2 :";
cin>>n2;
cout<<"Numbers before swapping :"<<n1<<"\t"<<n2<<endl;
swap(&n1,&n2);
cout<<"Numbers after swapping :"<<n1<<"\t"<<n2<<endl;
getch();
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
OUTPUT:
Practical 3:
Searching a number in an array using Binary Search Technique
Algorithm:
Input: Array of numbers and the number to be searched
Output: If the number is found its’ position is displayed; else “number not found” is displayed.
Steps:
1. Start
2. Take any n numbers from the user into an array and the number to be searched.
3. Sort the numbers using Bubble_Sort technique
4. Call the function Binary_Search with array and the search element being passed as
parameters
5. Receive the result of search
6. If the result is -1; then display that the search element is not found in the array
7. Else display the position of the search element
8. Stop
FlowChart:
START
Input 10 Array
elements
Input Number to be
searched as key
Yes
If loc=-1 Print search
element not found
No
Print position of
search element
STOP
Bsearch () function
START
/*Binary Search*/
#include<iostream.h>
#include<conio.h>
Initialize low=0,
int binsearch(int [],int); //function prototype
high=arraysize-1
void main()
{
int a[10],i,j,k,t,s,loc;
clrscr();
No
cout<<"Enter 10 array elements:"<<endl;
If Return -1 to main
for(i=0;i<10;i++)low<=high Function
cin>>a[i];
cout<<"Array Elements :"<<endl;
for(i=0;i<10;i++) Yes
cout<<a[i]<<"\ t";
Mid=(low+high)/2
for(k=1;k<10;k++)
{
for(j=0;j<10-k;j++)
{
if(a[j]>a[j+1]) Yes
{ If search= Return mid to main
t=a[j]; array[mid] Function
a[j]=a[j+1];
a[j+1]=t;
}
No
}
}
cout<<"Sorted Array :"<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<"\t";
No
cout<<"Enter Number to search :";
If search<
cin>>s; Low=mid+1
array[mid]
loc=binsearch(a,s);
if(loc==-1)
cout<<"Number Yes not found";
else
cout<<"Number found at position :"<<loc+1;
High=mid-1
getch();
}
OUTPUT:
Practical 4:
Implementing Class Ratio with member functions like assign, convert and
invert.
Algorithm:
Input: No input given by user
Output: Result of the ratio converted in decimal format is displayed
Inverted ratio is displayed
Steps:
1. Start
2. Create the class ratio with member functions - assign, convert, invert, print and member
data - numerator and denominator
3. Create an object of class ratio.
4. Invoke Assign function
5. Call Convert function
6. Call Invert function
7. Call print function
8. Stop
Assign(int n, int d)
1. Start
2. numerator =n
3. denominator.=d
4. Stop
Convert()
1. Start
2. Return (double)num/den;
3. Stop
Invert()
1. Start
2. Exchange the numerator and denominator of the ratio
3. Stop
Print()
1. Start
2. Print the numerator and denominator
3. Stop
Flowchart:
START
Define Class Ratio
assign(n,d) with convert()
member functions
assign, convert, invert and
print
numerator=n return
Call assign()
(double)num/den;
denominator=d
Call print()
STOP STOP
Call convert()
invert() print()
Call invert()
Call print()
STOP
START START
/*Implementing class ratio with member functions like assign, convert and invert */
#include<iostream.h>
#include<conio.h>
set temp=num;
class ratio
Display the numerator and
{ num=den;
denominator of ratio
public:
den=temp;
void assign(int, int);
double convert();
void invert();
void print();STOP STOP
private:
int num, den;
};
void main()
{
ratio x;
x.assign(22,7);
clrscr();
cout<<"x=";
x.print();
cout<<"="<<x.convert()<<"\n";
x.invert();
cout<<"1/x=";
x.print();
cout<<"\n";
getch();
}
void ratio::assign(int numerator, int denominator)
{
num=numerator;
den=denominator;
}
double ratio::convert()
{
return (double)num/den;
}
void ratio::invert()
{
int temp=num;
num=den;
den=temp;
}
void ratio::print()
{
cout<<num<<"/"<<den;
}
OUTPUT:
Practical 5:
Implementing Class Circle with default constructor and accessing functions like
area and circumference.
Algorithm:
Input: Radius of Circle
Output: Area and Circumference of circle displayed
Steps:
1. Start
2. Define a class ‘circle’ with rad,x,y as data members, a default constructor to initialize
rad,x,y to 0.and member functions – getdata(),area(),circumference().
3. Create an object of class ‘circle’
4. Invoke getdata(), area(), circumference for the object and display the respective
results
5. Stop
Assign(int n, int d)
1. Start
2. numerator =n
3. denominator.=d
4. Stop
getdata()
1. Start
2. input x and y coordinates of the center point
3. input radius
4. Stop
area()
1. Start
2. ar=3.14*rad*rad
3. print ar as area of circle
4. Stop
circumference()
1. Start
2. print 2*3.14*rad as circumference
3. Stop
FlowChart:
START
Call getdata()
Call area()
Call circumeference()
STOP
getdata() area()
START START
ar=3.14*rad*rad
Input x,y,rad
Print ar
STOP
STOP
circumference()
START
Print 2*3.14*rad as
circumference
STOP
/* Implementing class circle with default constructor and accessing member functions like area and
circumference */
#include<iostream.h>
#include<conio.h>
class circle
{
private:
float rad,x,y;
public:
circle() //default constructor
{
rad=0;
x=0;
y=0;
}
void getdata()
{
cout<<"Enter x-coord and y-coord of center point :";
cin>>x>>y;
cout<<"Enter radius :";
cin>>rad;
}
void area()
{
float ar;
ar=3.14*rad*rad;
cout<<"Area :"<<ar<<endl;
}
void circumference()
{
cout<<"Circumference :"<<2*3.14*rad<<endl;
}
};
void main()
{
clrscr();
circle o;
o.getdata();
o.area();
o.circumference();
getch();
}
OUTPUT:
Practical 6:
Study scope of an object using constructor and destructor.
Algorithm:
Input: No input given by user
Output: The output of constructor and destructor is displayed. Scope of constructor and
destructor is seen.
Steps:
1. Start
2. Create class Ratio with constructor and destructor and print() function
3. Create an object of class ratio
4. Call print()
5. Stop
print() function
1. Start
2. Print that the object is alive
3. Stop
Constructor ratio()
1. Start
2. Print that the object is born
3. Stop
Destructor ~ratio()
1. Start
2. Print that the object is dead
3. Stop
FlowChart:
START
Call print()
STOP
START START
STOP
print() Print “Object is born” Print “Object is dead”
START
STOP
STOP
/*Scope of constructor and destructor */
#include<iostream.h>
#include<conio.h>
class Ratio
{
public:
void print()
{
cout<<"\nNow X is ALIVE"<<endl;
}
Ratio()
{
cout<<"OBJECT IS BORN"<<endl;
}
~Ratio()
{
cout<<"OBJECT DIES";
}
};
void main()
{
clrscr();
Ratio r1;
r1.print();
getch();
}
Output:
Practical 7:
Overloading + and / operator.
Algorithm:
Input: Numerator and denominator of 2 ratios are taken from user
Output: The result of addition and division of the 2 ratios are displayed to the user
Steps:
1. Start
2. Create class Ratio with data members - numerator and denominator
3. Define member functions get() and show()
4. Overload the operators ‘+’ and ‘/’ to work on ratio data type
5. Declare 3 objects of the class Ratio
6. Get ratio 1 and ratio 2 from the user
7. Perform ratio 1+ratio 2 using overloaded operator ‘+’; store the result in the 3 rd ratio
and display the result to the user
8. Perform ratio 1/ratio 2 using overloaded operator ‘/’; store the result in the 3 rd ratio and
display the result to the user
9. Stop
get()
1. Take the numerator and denominator of the ratio
show()
1. Display the numerator and denominator of the ratio
Overloaded operator ‘+’
1. If the denominators are the same add the numerators
2. If the denominators are not the same then L.C.M. is taken and addition is performed
FlowChart:
START
r1.get()
r2.get()
A
r3=r1+r2
r3=r1/r2
r3.show()
r3.show()
STOP
get() show()
START START
STOP STOP
Overloaded ‘+’ () Overloaded ‘/’()
START START
r3.num=num1*den2
No
Is r3.den=den1*num2
den1==
den2
Yes
Return r3
r3.num=num1+num2
STOP
Return r3
STOP
/* Overloading + and / operator */
#include<iostream.h>
#include<conio.h>
class Ratio
{
int num,den;
public:
void get()
{
cin>>num>>den;
}
void show()
{
cout<<num<<"/"<<den<<endl;
}
Ratio operator +(Ratio r)
{
//o1.operator(o2)
Ratio t;
if(den==r.den)
{
t.num=num+r.num;
t.den=den;
}
else
{
t.num=num*r.den+r.num*den;
t.den=den*r.den;
}
return t;
}
Ratio operator /(Ratio r)
{
Ratio t;
t.num=num*r.den;
t.den=den*r.num;
return t;
}
};
void main()
{
clrscr();
Ratio o1,o2,o3;
cout<<"Enter Num and Den of Ratio 1 :";
o1.get();
cout<<"Ratio 1:";
o1.show();
cout<<"Enter Num and Den of Ratio 2 :";
o2.get();
cout<<"Ratio 2:";
o2.show();
o3=o1+o2; //o1.operator +(o2)
cout<<"Addition of 2 ratios :";
o3.show();
cout<<"Division of 2 ratios :";
o3=o1/o2; //o1.operator /(o2)
o3.show();
getch();
}
OUTPUT:
Practical 8:
Implementing Inheritance.
Algorithm:
Input: Roll number and name of a Student is given by the user. Marks of 2 subjects and sports
marks is given by the user.
Output: Marks of all the subjects, the total marks scored by the student with the percentage
secured is displayed to the user.
Steps:
1. Start
2. Create Class Student with protected data memebers - Roll, name and member
functions getinfo(), showinfo().
3. Create Class test as child class of Student with protected data members – marks 1
and marks 2 and member functions getmarks() & showmarks().
4. Create Class sports with protected data member – sports marks and member
functions getsports() and showsports()
5. Create class result as the child class of test and sports with data members = Total
marks and percentage; member functions calculate() & showresult().
6. Create an object of result class
7. Call getinfo()
8. Call getmarks()
9. Call getsports()
10. Call calculate()
11. Call showinfo()
12. Call showmarks()
13. Call showsports()
14. Call showresult()
15. Stop
FlowChart:
START
Call getinfo(),getmarks(),getsports(),calculate()
STOP STOP
getmarks() showmarks()
START START
STOP STOP
getsports() showsports()
START
START
STOP STOP
calculate() showresult()
START
START
STOP
STOP
/*Implementing Inheritance */
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll;
char name[20];
public:
void getinfo()
{
cout<<"Enter rollno and name :";
cin>>roll>>name;
}
void showinfo()
{
cout<<"Roll No :"<<roll<<endl;
cout<<"Name :"<<name<<endl;
}
};
class test : public student
{
protected:
int m1,m2;
public:
void getmarks()
{
cout<<"Enter marks of Subject 1:";
cin>>m1;
cout<<"Enter marks of Subject 2:";
cin>>m2;
}
void showmarks()
{
cout<<"Subject 1 Marks :"<<m1<<endl;
cout<<"Subject 2 Marks :"<<m2<<endl;
}
};
class sports
{
protected:
int spmarks;
public:
void getsports()
{
cout<<"Enter Sports marks :";
cin>>spmarks;
}
void showsports()
{
cout<<"Sports Marks :"<<spmarks<<endl;
}
};
class result : public test, public sports
{
int total,per;
public:
void calculate()
{
total=m1+m2+spmarks;
per=total/3;
}
void showresult()
{
cout<<"Total Marks :"<<total<<endl;
cout<<"Percentage :"<<per;
}
};
void main()
{
clrscr();
result s;
s.getinfo();
s.getmarks();
s.getsports();
s.calculate();
s.showinfo();
s.showmarks();
s.showsports();
s.showresult();
getch();
}
OUTPUT:
Practical 9:
Using Virtual Function.
Algorithm:
Input: No input from user
Output: Output of virtual function in parent class and overridden function in child class is
displayed
Steps:
1. Start
2. Create parent class Person with virtual member function print()
3. Create class Student which inherits from class Person with overridden function print()
4. Create an object p1 and pointer *p of class person.
5. Assign p=&p1.
6. Call print function of person class.
7. Create an object of Student class s1.
8. Assign p=&s1.
9. Call print function of student class.
10. Stop
class person print() [Virtual function]
1. Start
2. print “Name of person : BOB”
3. Stop
class student print() [Overridden function]
1. Start
2. print “Name of student : TOM”
3. Stop
FlowChart:
START
p=&p1
Call print()
START START
p=&s1
Print “Name of
student :TOM"
Call print()
STOP
STOP
Print “Name of
/*Using VirtualPerson
Function*/
:BOB"
#include<iostream.h>
#include<conio.h>
class Person STOP
{
public:
virtual void print()
{
cout<<"Name of person :BOB"<<endl;
}
};
class Student : public Person
{
public:
void print()
{
cout<<"Name of student :TOM";
}
};
void main()
{
clrscr();
Person *p,p1;
p=&p1;
p->print();
Student s1;
p=&s1;
p->print();
getch();
}
OUTPUT:
Practical 10:
File Handling in C++.
Algorithm:
Pre-requisites: Country file and capital file should be created
Input: Country file name and capital file name
Output: Countries and their capitals are displayed
Steps:
1. Start
2. Declare 2 objects of the input file stream(ifstream); one for the country file and one for
the capital file
3. Take the country file name from the user and open it
4. If the file is not found display a suitable message to the user
5. Take the capital file name from the user and open it
6. If the file is not found display a suitable message to the user
7. Up: If the end of country file is not reached
a. Get a line of the country file into str1
b. Display str1
c. Get a line of the capital file into str2
d. Display str2
e. Go to Up
8. Close the capital file
9. Close the country file
10. Stop
FlowChart:
START
Take the country file name
from the user
Yes
If the
country Display country
file is not file is not found
found
No
A
B
C
C
Practical 11:
Creating a web page with tables and images, hyperlink feature and background
color.
HTML PROGRAM
<html>
<title>My Web Page</title>
<body bgcolor="pink">
<h2><marquee>welcome to my page</marquee></h2>
<br></br>
<table border="1">
<td colspan="2">Chemistry</td>
<td colspan="2">Physics</td></tr>
<tr><td>Theory</td>
<td>Pracs</td>
<td>Theory</td>
<td>Pracs</td>
<td>Theory</td>
<td>Pracs</td></tr>
<tr><td>80</td>
<td>60</td>
<td>50</td>
<td>80</td>
<td>70</td>
<td>59</td></tr>
<tr><td>50</td>
<td>60</td>
<td>35</td>
<td>47</td>
<td>75</td>
<td>46</td></tr>
</table>
<br></br>
<A href="http://www.vidyavalleynp.in/"><img src="C:\Users\Public\Pictures\Sample Pictures\
Chrysanthemum.jpg"
</body>
</html>
Practical 12:
Creating simple HTML page on the topic: Software Development Company. The
page must have atleast 3 paragraphs of text. The page must have an
appropriate title, background color and hyperlink to other pages. The
paragraphs must have text consisting of different colors.
<html>
<head>
<title>Binsoft.com</title>
</head>
<body bgcolor="#d6fa98">
<hr>
<p><font color="red">Welcome to Binsoft Pvt. Ltd. one of the most leading companies
in the sofware industries.One of the foremost companies in this industry with over 25 years of
Experience</font></p>
<hr>
<p><b>Email.</b> <a
href="mailto:support@binsoft.com">support@binsoft.com</a></p>
<p><b>Addr.</b> Binsoft Softwares Pvt. Ltd. Ganesh Arcade Floor 3,4 Pune,411011</p>
<hr>
</body>
</html>
# CODE
<html>
<head>
<title>Binsoft Clients</title>
</head>
<body bgcolor="#d6fa98">
<hr>
<ol>
<li>ABS Enginnerings</li>
<li>RD Techsoft</li>
<li>Techfins</li>
<li>FinMines</li>
<li>RS Logistics</li>
</ol>
<hr>
</body>
</html>
Practical 13:
Practical 15:
End Sub