0% found this document useful (0 votes)
4 views

12th Computer Sci Practical Paper-1

The document contains a series of C++ programming experiments demonstrating various programming concepts such as call by value, call by reference, linear search, binary search, bubble sort, pointer arithmetic, object-oriented programming (OOP) with constructors and destructors, single inheritance, friend functions, and function overloading. Each experiment includes code snippets, descriptions of the tasks, and expected outputs. The programs cover fundamental programming techniques and OOP principles in C++.

Uploaded by

yashdolse01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

12th Computer Sci Practical Paper-1

The document contains a series of C++ programming experiments demonstrating various programming concepts such as call by value, call by reference, linear search, binary search, bubble sort, pointer arithmetic, object-oriented programming (OOP) with constructors and destructors, single inheritance, friend functions, and function overloading. Each experiment includes code snippets, descriptions of the tasks, and expected outputs. The programs cover fundamental programming techniques and OOP principles in C++.

Uploaded by

yashdolse01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

-===============================================================================================================================================================================================================================================================================

Experiment No. 1

A) Write a program in C++ that exchange data (Call by Value) using SWAP function
i.e. void swap(int, int)to interchange the given two numbers.

// call by value
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
inta,b;
void swap(int,int);
cout<<"enter the value of two numbers:";
cin>>a>>b;

cout<<"before swapping A="<<a<<"b="<<b<<endl;


swap( a, b);
cout<<"after swapping A="<<a<<"b="<<b<<endl;
getch();
}
void swap(intx,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}

Output

enter the value of two numbers:55


66
before swapping A=55b=66
after swapping A=55 b=66
=================================================================================================================================================================================================================

Experiment No. 2

A) Write a program in C++ that exchange data (Call by Reference) using SWAP
function void swap(int *, int *)to interchange the given two numbers.

//Program to Demonstrate call by reference//


#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
inta,b;
void swap(int*x,int*y);
cout<<"enter the value of two numbers:";
cin>>a>>b;

cout<<"before A="<<a<<"b="<<b<<endl;
swap(& a,& b);
cout<<"after A="<<a<<"b="<<b<<endl;
getch();
}
void swap(int*x,int*y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

Output

enter the value of two numbers:55


66
before A=55b=66
after A=66b=55
-===============================================================================================================================================================================================================================================================================

Experiment No. 3

A) Write a program in C++, that first initializes an array of given 10 integer numbers.
The program must verify whether a given element belongs this array or not, using
LINEAR SEARCH technique. The element (to be search) is to be entered at the
time of execution. If the number is found, the program should print” The Number is
Found” otherwise it should print “The number is not Found”.

//LINEAR SEARCH//
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
intnum[10],n;
cout<<"enter the 10 numbers for the array."<<endl;
for(int i=0;i<10;i++)
cin>>num[i];
cout<<"enter the number for linear search:";
cin>>n;
for(i=0;i<=10;i++)
{
if(num[i]==n)
{
cout<<"the number is present search successful at position"<<i+1<<endl;
break;
}
}
if (i==10)
cout<<"the number is absent search unsuccessful"<<endl;
getch();

Output

enter the 10 numbers for the array.


1
2
3
4
5
6
78
8
9
10
enter the number for linear search:78
the number is present search successful at position7
Experiment No. 4

A) Write a program in C++, that first initializes an array of given 10


sortedintegernumbers. The program must verify whether a given element belongs
this array or not, using BINARY SEARCH technique. The element (to be search)
is to be entered at the time of execution. If the number is found, the program should
print” The Number is Found” otherwise it should print “The number is not Found”.

//Binary search//
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],n,i,x,beg,end,mid;
cout<<"enter number of array elements"<<endl;
cin>>n;
beg=0;
end=n-1;
mid=(beg+end)/2;
cout<<"enter the elements"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"enter element to be search";
cin>>x;
while(a[mid]!=x&&beg<=end)
{
if(a[mid]>x)
end=mid-1;

beg=mid+1;
mid=(beg+end)/2;
}
if(a[mid]==x)
cout<<"element is present at position"<<mid+1;
else
cout<<"search unsuccessfull";
getch();
}

Output

enter number of array elements


5
enter the elements
10 20 30 40 50
enter element to be search 30
element is present at position3
-===============================================================================================================================================================================================================================================================================

Experiment No. 5

A) Write a program in C++, that first initializes an array of given 10 integer 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.

//Bubble sort//
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
inti,j,t,sort[10];
cout<<"enter the array element";
for(i=0; i<10;i++)
{
cin>>sort[i];
}
for(i=0;i<10;i++)
{
for(j=10;j>0;j--)
{
if(sort[j-1]>sort[j])
{
t=sort[j-1];
sort[j-1]=sort[j];
sort[j]=t;
}
}
}
cout<<"sorted array is-"<<endl;
for(i=0;i<10;i++)
{
cout<<sort[i]<<endl;
}
getch();
}

Output

enter the array element10


2 5 3 8 25 14 57 25 147 10
sorted array is-
2
3
5
8
10
14
25
25
57
147
Experiment No. 6

A) Write a program in C++, that first initializes an array of given 5 integer numbers.
The program should perform summation of array elements using POINTER.

//sum of array
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
intnum[5],i,*p,sum=0;
cout<<"enter array elements:=\n";
for(i=0;i<5;i++)
cin>>num[i];
p=num;
for(i=100;i<105;i++,p++)
{
sum=sum+*p;
}
cout<<"the sum of array is "<<sum;
getch();
}

Output

enter array elements:=


2
3
1
4
5
the sum of array is 15
-===============================================================================================================================================================================================================================================================================

Experiment No. 7

A) Write a program in C++,using OOP to create theclass fib with CONSTRUCTOR


AND DE-CONSTRUCTOR and one member function voidgenfib().The program
must print the message “OBJECT IS BORN” when the OBJECT is create i.e. Class
Fib F. The program must display “OBJECTSARE DESTROYED….” when the class
de- constructor is called for the object when it reaches the end of its scope.
//Constructor and destructor//
#include<iostream.h>
#include<conio.h>
class fib
{
private:
int f1,f2,f3;//data members
public:
fib();//constructor member function
~fib()//destructor member function
{
cout<<"object are destroyed....";
}
voidgenfib();
};
//memberfunctions are define outside the class
fib::fib()
{
f1=0;
f2=1;
f3=0;
cout<<f1<<endl<<f2<<endl;
cout<<"object is born"<<endl;
}
void fib ::genfib()
{
cout<<"object is alive"<<endl;
while(f3<55)
{
f3=f1+f2;
cout<<f3<<endl;
f1=f2;
f2=f3;
}
}
void main()
{
clrscr();
class fib f;//create object and call constructor when object f is born
f.genfib();
getch();
}//destructor will be called here

Output

0
1
object is born
object is alive
1
2
3
5
8
13
21
34
55
object are destroyed....
-===============================================================================================================================================================================================================================================================================

Experiment No. 8

A) Write a program in C++, using OOP to demonstrating the implementation of


SINGLE INHERITANCE, having two class viz. student and result.

//Demonstrating inheritance//
#include<iostream.h>
#include<conio.h>

class student
{
private:
floatrollno;
public:
floatp,c,m,b,e;
voidgetdata()
{
cout<<"enter the marks of 5 subjects:-";
cin>>p>>c>>m>>b>>e;
}

};

class result : public student


{
private:
floattotal,per;
public:
voiddisdata();
};

void result::disdata()
{
getdata();
total=p+c+m+b+e;
per=total/500*100;
cout<<"total::"<<total<<"percentage::"<<per;
}
void main()
{
clrscr();
class result zl;
zl.disdata();
getch();
}

Output

enter the marks of 5 subjects:-


95
96
94
93
92
total::470
percentage::94
-===============================================================================================================================================================================================================================================================================

Experiment No. 9

A) Write a program in C++, using OOP to demonstrating the implementation of


FRIEND, having two class viz. XYX and ABC.

//friend function
#include<iostream.h>
#include<conio.h>
classabc;//forward declaration
class xyz
{
private:
int x;
public:
voidsetvalue(int i)
{
x=i;
}
friend void max(xyz,abc);
};

classabc
{
private:
int a;
public:
voidsetvalue(int i)
{
a=i;
}
friend void max(xyz,abc);
};

void max(xyz m,abc n)


{
if(m.x>n.a)
cout<<"greatest number is"<<m.x<<endl;
else
cout<<"greatest number is"<<n.a<<endl;
}
void main()
{
clrscr();
xyz T1;
T1.setvalue(10);
abc T2;
T2.setvalue(20);
max(T1,T2);
getch();
}

Output

greatest number is 20
Experiment No. 10

A) Write a program in C++, using OOP to demonstrating the implementation of


FUNCTION OVERLOADING having two classes.

//functional overloading//
#include<iostream.h>
#include<conio.h>
class ex
{
public:
voidcalc(intx,int y);
voidcalc(float a,float b);
};
void ex::calc(intx,int y)
{
int total;
total=x+y;
cout<<"\ntotal="<<total;
}
void ex::calc(float a,float b)
{
float sub;
sub=a-b;
cout<<"\nsub="<<sub;
}
void main()
{
exob;
clrscr();
intx,y;
floata,b;
cout<<"enter the value for x and y";
cin>>x>>y;
cout<<"enter the value for a and b";
cin>>a>>b;
ob.calc(a,b);
getch();
}

Output

enter the value for x and y


12
10
enter the value for a and b
14
10

You might also like