12th Computer Sci Practical Paper-1
12th Computer Sci Practical Paper-1
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;
Output
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.
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
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
//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
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
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
Experiment No. 7
Output
0
1
object is born
object is alive
1
2
3
5
8
13
21
34
55
object are destroyed....
-===============================================================================================================================================================================================================================================================================
Experiment No. 8
//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;
}
};
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
Experiment No. 9
//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);
};
Output
greatest number is 20
Experiment No. 10
//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