Computer Science (Part - I) Practical File Answer Sheet
Computer Science (Part - I) Practical File Answer Sheet
int main()
{
//clrscr();
int a, b;
//void swap(int &,int &);
cout<<"Enter two values:";
cin>>a>>b;
cout<<"\n Befor swapping: a= "<<a <<" b= "<<b;
swap(&a, &b);
cout<<"\n After swapping: a= "<<a <<" b= "<<b;
getch();
//return 0;
}
OUTPUT :-
( Write output….. )
Page 3 of 17
/* 3. Linear Search */
Write a program in C++, that first initializes an array of given 10 integer
numbers. The program must verify whether a given element belongs to 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”.
Enter the program and verify proper execution of the same on the computer.
Note: Write the below program code on proper printout of practical file sheet pdf)
#include<iostream>
using namespace std;
int main()
{
int a[20],n,x,i,flag=0;
cout<<"How many elements?";
cin>>n;
cout<<"\nEnter elements of the array\n";
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nEnter element to search:";
cin>>x;
for(i=0;i<n;++i)
{
if(a[i]==x)
{
flag=1;
break;
}
}
if(flag)
cout<<"\nElement is found at position "<<i+1;
else
cout<<"\nElement not found";
return 0;
}
Page 4 of 17
/* 4. Binary Search */
Write a program in C++, that first initializes an array of
given 10 integer numbers. The program must verify
whether a given element belongs to 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”.
Enter the program and verify proper execution of the same
on the computer.
Note: Write the below program code on proper printout of practical file sheet pdf)
#include<iostream>
using namespace std;
int main()
{
int search(int [],int,int);
int n,i,a[100],e,res;
cout<<"How Many Elements:";
cin>>n;
cout<<"\n Enter Elements of Array in Ascending order\n";
for(i=0;i<n;++i)
{
cin>>a[i];
}
cout<<"\nEnter element to search:";
cin>>e;
res=search(a,n,e);
if(res!=-1)
cout<<"\nElement found at position "<<res+1;
else
Page 5 of 17
while(f<=l)
{
m=(f+l)/2;
if(e==a[m])
return(m);
else
if(e>a[m])
f=m+1;
else
l=m-1;
}
return -1;
}
Output:
(attach extra blank pages also and write output as per program requirement )
Page 6 of 17
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
Line::~Line(void) {
cout << "Object is being deleted" << endl;
}
int main() {
Line line;
// set line length
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
Output:
(attach extra blank pages also and write output as per program requirement )
Page 8 of 17
#include<iostream>
#include<conio.h>
using namespace std;
class complex {
int a, b;
public:
void getvalue() {
cout << "Enter the value of Complex Numbers a,b:";
cin >> a>>b;
}
void display() {
cout << a << "+" << b << "i" << "\n";
}
};
int main() {
// clrscr();
complex obj1, obj2, result, result1;
obj1.getvalue();
obj2.getvalue();
// getch();
}
Output:
(attach extra blank pages also and write output as per program requirement )
Page 10 of 17
#include<iostream>
#include<conio.h>
using namespace std;
class emp {
public:
int eno;
char name[20], des[20];
void get() {
cout << "Enter the employee number:";
cin>>eno;
cout << "Enter the employee name:";
cin>>name;
cout << "Enter the designation:";
cin>>des;
}
};
void calculate() {
np = bp + hra + da - pf;
}
void display() {
cout << eno << "\t" << name << "\t" << des << "\t" << bp
<< "\t" << hra << "\t" << da << "\t" << pf << "\t" << np << "\n";
}
};
int main() {
int i, n;
char ch;
salary s[10];
cout << "Enter the number of employee:";
cin>>n;
for (i = 0; i < n; i++) {
s[i].get();
s[i].get1();
s[i].calculate();
}
cout << "\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for (i = 0; i < n; i++) {
s[i].display();
}
getch();
return 1;
}
Page 12 of 17
#include<iostream>
#include<conio.h>
using namespace std;
class Person{
public: virtual void print(){
cout<<"\n The Name of person is BOB";
}
};
int main()
{
//clrscr();
Person *p;
Person p1;
p=&p1;
p->print();
Student s1;
p= &s1;
p->print();
getch();
}
Page 13 of 17
int main(){
ifstream fcountry;
ifstream fcap;
char fnm1[20], fnm2[20], str1[20], str2[20];
//clrscr();
cout<<"\n enter country file name- ";
cin>>fnm1;
fcountry.open(fnm1) ;
if(!fcountry)
cout<<"\n Country file not found";
else{
cout<<"\n Enter capitl filename-";
cin>>fnm2;
fcap.open(fnm2);
if(!fcap)
cout<<"\n Capital file not found";
else{
Page 14 of 17
while(!fcountry.eof())
{
fcountry.getline(str1,20,'\n');
cout<<str1<<"\t";
fcap.getline(str2,20,'\n');
cout<<str2<<endl;
}
}
}
fcap.close();
fcountry.close();
getch();
}
Page 15 of 17