[go: up one dir, main page]

0% found this document useful (0 votes)
65 views47 pages

Anuj C++ Practical File

The document contains an index and listing of various C++ programs submitted by a student named AnuJ, including programs involving binary search, linear search, copy constructors, pointers, queues, file input/output, and SQL commands. It provides the code and output for each listed program to demonstrate concepts in C++ programming.

Uploaded by

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

Anuj C++ Practical File

The document contains an index and listing of various C++ programs submitted by a student named AnuJ, including programs involving binary search, linear search, copy constructors, pointers, queues, file input/output, and SQL commands. It provides the code and output for each listed program to demonstrate concepts in C++ programming.

Uploaded by

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

Practical file

Computer Science
(C++)

Submitted By :- ANUJ
Class - XII A
Roll no. –
INDEX
 PROGRAMS:
 Binary Search
 Linear Search
 Copy Constructor
 Parameterised Constructor
 To calculate area of a triangle using class (Nesting)
 Pointer
 Linear Queue
 Circular Queue
 To read content from a Binary File
 To write content in the Binary File
 To find absolute value of a number
 To print area of rectangle and circle
 To calculate sum using function overloading
 Using same function to print different Data Types
 To swap two numbers using class
 . Program That Defines a Class String and
Overload Program
 Program to Print Numbers From 1 to n Without
Using Loops
 Travel plan [using class]
 Publication [using class]
 Merge two arrays

SQL COMMANDS:
 Show database
 Create database
 To insert data and use of SELECT command
 Creating and printing the table in descending
order
 Alter the table with ADD command
 Alter the table with DROP command
 LIKE command
 SELECT-ORDER BY command
 CREATE VIEW command
PROGRAMS
1. Binary Search:
#include<conio.h>

#include<iostream.h>

int Binary_Search(int a[], int N, int data)

int first,last,mid;

int found=0;

first=0;

last=N-1;

while(first<=last && found==0)

mid=int((first+last)/2);

if(a[mid]==data)

found=1;

if(a[mid]<data)

first=mid+1;

if(a[mid]>data)

last=mid-1;

return(found);

int main()
{

clrscr();

int x[10],data, i;

cout<<"\n Enter 10 numbers in ascending order:";

for(i=0;i<10;i++)

cin>>x[i];

cout<<"\n Enter No. to be searched:";

cin>>data;

int res=Binary_Search(x,10,data);

if(res==1)

cout<<"\n found";

else

cout<<"\n not found";

getch();

return 0;

/*

Enter 10 numbers in ascending order:1

4 7 9 11 14 16 19 20 23

Enter No. to be searched:11

found
*/

2. Linear Search
#include<conio.h>

#include<iostream.h>

int Linear_Search (int x[],int N,int data)

int i, found=0;

for(i=0;i<N;i++)

if(x[i]==data)

found=1;

return(found);

int main()

clrscr();

int x[10],data, i;

cout<<"\n Enter 10 numbers:";

for(i=0;i<10;i++)

cin>>x[i];

}
cout<<"\n Enter No. to be searched:";

cin>>data;

int res=Linear_Search(x,10,data);

if(res==1)

cout<<"\n found";

else

cout<<"\n not found";

getch();

return 0;

/* Enter 10 numbers:1

2 3 4 5 6 7 8 9 10

Enter No. to be searched:5

found */

3. Copy Constructor
#include<conio.h>

#include<iostream.h>

#include<string.h>

class student

int roll;

char name[30];
public:

student()

cout<<"\n Constructor:";

roll=10;

strcpy(name,"Rahul");

student(student &s)

cout<<"\n Copy constructor:";

roll=s.roll;

strcpy(name,s.name);

void input_void()

cout<<"\n Enter roll no:";

cin>>roll;

cout<<"\n Enter name:";

cin>>name;

void show_data()

cout<<"\n Roll no:";

cout<<roll;

cout<<"\n Name:";
cout<<name;

} };

int main()

student s;

s.show_data();

cout<<"\n";

student A(s);

A.show_data();

getch();

return 0;

/*

Constructor:

Roll no:10

Name:Rahul

Copy constructor:

Roll no:10

Name:Rahul */

4. Parametrised Constructor
#include<conio.h>

#include<iostream.h>
class read_constructor

int x;

public:

read_constructor(int a)

x=a;

void read_data()

cin>>x;

void show_data()

cout<<"Value of x:"<<x;

};

int main()

read_constructor obj(10);

obj.show_data();

getch();

return 0;

}
/*

Value of x:10

*/

5. Nesting
#include<conio.h>

#include<iostream.h>

class area

int b, h;

double ar;

public:

void input_data()

cout<<"\n Enter base:";

cin>>b;

cout<<"\n Enter height:";

cin>>h;

void calculate()

input_data();

ar=0.5*b*h;
}

void output()

calculate();

cout<<"\n Area of triangle:"<<ar;

} };

int main()

clrscr();

area A;

A.output();

getch();

return 0;

} /*

Enter base:10

Enter height:5

Area of triangle:25 */

6. Pointers
#include<conio.h>

#include<iostream.h>

int* read()

int x;
x=45;

return(&x);

int main()

clrscr();

int *res;

res=read();

cout<<"\n Value at res:"<<res;

getch();

return 0;

/*

Value at res:0x8fc7ffee

*/

7. Linear Queue
#include<conio.h>

#include<iostream.h>

#include<iomanip.h>

//function prototype

void add(int &front, int &rear, int x[], int N, int value);

void deletion(int &front, int &rear);


void display(int front, int rear, int x[]);

void main()

{ clrscr();

int x[100],front,rear,choice,value;

front=-1;

rear=-1;

do

cout<<"\n Queue Menu";

cout<<"\n 1. Addition";

cout<<"\n 2. Deletion";

cout<<"\n 3. Display";

cout<<"\n 4. Exit";

cout<<"\n Enter your choice";

cin>>choice;

switch(choice)

case 1:

cout<<"\n Enter value";

cin>>value;

add(front,rear,x,100,value);

break;

case 2:

deletion(front,rear);
break;

case 3:

display(front,rear,x);

getch();

break;

case 4:

break;

default:

cout<<" wrong choice";

getch();

}while(choice!=4);

getch();

return ;

void add(int &front, int &rear, int x[], int N, int value)

if(rear==-1)

front=0;

rear=0;

x[rear]=value;

else
{ if(rear>=(N-1))

cout<<"full";

else

rear=rear+1;

x[rear]=value;

} } }

void deletion (int &front, int &rear)

if(front==-1)

cout<<"Empty";

else

if(front==rear)

front=-1;

rear=-1; }

else

{ front=front+1; }

return;

void display (int front, int rear, int x[])

int i;
if(front==-1)

cout<<"Empty";

else

for(i=front;i<=rear;i++)

cout<<setw(4)<<x[i];

return;

}/*

3. Display

4. Exit

Enter your choice 1

Enter value 8

Queue Menu

1. Addition

2. Deletion

3. Display

4. Exit

Enter your choice 3

2 5 8

Queue Menu

1. Addition

2. Deletion

3. Display
4. Exit

Enter your choice 2

Queue Menu

1. Addition

2. Deletion

3. Display

4. Exit

Enter your choice 3

5 8

Queue Menu

1. Addition

2. Deletion

3. Display

4. Exit

Enter your choice 1

Enter value 9

Queue Menu

1. Addition

2. Deletion

3. Display

4. Exit
Enter your choice 3

5 8 9

Queue Menu

1. Addition

2. Deletion

3. Display

4. Exit

Enter your choice 4 */

8. Circular Queue
#include<conio.h>

#include<iostream.h>

#include<iomanip.h>

//Function prototype

void push(int x[], int N, int &M, int data);

void pop(int &m);

void display(int x[], int &m);

void main()

clrscr();

int x[100],m,data,choice;

m=0;

do
{

cout<<"\n STACK MENU";

cout<<"\n 1. Push";

cout<<"\n 2. Pop";

cout<<"\n 3. Display";

cout<<"\n 4. Exit";

cout<<"\n Enter choice:";

cin>>choice;

switch(choice)

case 1:

cout<<" Enter value:";

cin>>data;

push(x,100,m,data);

break;

case 2:

pop(m);

break;

case 3:

display(x,m);

getch();

break;

case 4:

break;
default:

cout<<"\n Wrong choice";

getch();

}while(choice!=4);

void push(int x[], int N, int &M, int data)

if(M>=N)

cout<<"STACK full";

else

x[M]=data;

M=M+1;

return;

void pop(int &m)

if(m<1)

cout<<"\n STACK Empty";

else
m=m-1;

return;

void display(int x[],int &m)

int i;

if(m<1)

cout<<"\n STACK Empty";

else

for(i=0;i<m;i++)

cout<<setw(6)<<x[i];

return;

} /*

2. Pop

3. Display

4. Exit

Enter choice:1

Enter value:4

STACK MENU

1. Push

2. Pop

3. Display
4. Exit

Enter choice:1

Enter value:6

STACK MENU

1. Push

2. Pop

3. Display

4. Exit

Enter choice:1

Enter value:8

STACK MENU

1. Push

2. Pop

3. Display

4. Exit

Enter choice:3

2 4 6 8

STACK MENU

1. Push

2. Pop

3. Display

4. Exit
Enter choice:2

STACK MENU

1. Push

2. Pop

3. Display

4. Exit

Enter choice:3

2 4 6

STACK MENU

1. Push

2. Pop

3. Display

4. Exit

Enter choice:4

*/

9. Read Binary File


#include<fstream.h>

#include<conio.h>

#include<iostream.h>

#include<string.h>

struct student{
int roll;

char name[30];

char address[80];

};

int main()

clrscr();

ifstream obj("student.dat");

student s;

cout<<"\n Reading student.dat file";

while(obj.read((char*)&s, sizeof(student)))

cout<<"\nRoll no:"<<s.roll;

cout<<"\n Name:"<<s.name;

cout<<"\n Address:"<<s.address;

obj.close();

getch();

return 0;

} /*

Reading student.dat file

Roll no:1

Name:ashish
Address:delhi

*/

10. Write in Binary File


#include<fstream.h>

#include<conio.h>

#include<iostream.h>

struct student

int roll;

char name[30];

char address[80];

};

int main()

{clrscr();

ifstream fin("student.dat");

ofstream fout("temp.dat");

int troll;

student s;

cout<<"\n Enter roll no to modify";

cin>>troll;

while(fin.read((char*)&s, sizeof(student)))

{ if(troll==s.roll)
{ cout<<"\n Enter New name:";

cin>>s.name;

cout<<"\n Enter Address:";

cin>>s.address; }

fout.write((char*)&s, sizeof(student));

fin.close();

fout.close();

//remove("student.dat");

//rename("temp.dat","student.dat");

getch();

return 0;

}/*

Enter roll no to modify 1

Enter New name:AG

Enter Address:anand vihar

*/

11. TO FIND ABSOLUTE VALUE OF A NUMBER.

#include <iostream.h>

using namespace std;

int absolute(int);

float absolute(float);
int main()

{ int a = -5;

float b = 5.5;

cout << "Absolute value of " << a << " = " << absolute(a) << endl;

cout << "Absolute value of " << b << " = " << absolute(b);

return 0;

int absolute(int var) {

if (var < 0)

var = -var;

return var;}

float absolute(float var){

if (var < 0.0)

var = -var;

return var;}

12. TO PRINT AREA OF RECTANGLE AND CIRCLE.

#include<iostream.h>

#include<conio.h>

class CalculateArea

{ public:

void Area(int r)

{ cout<<"\n\tArea of Circle is : "<<3.14*r*r; }

void Area(int l,int b)


{ cout<<"\n\tArea of Rectangle is : "<<l*b; }

void Area(float l,int b)

{ cout<<"\n\tArea of Rectangle is : "<<l*b }

void Area(int l,float b)

{ cout<<"\n\tArea of Rectangle is : "<<l*b; }

};

void main()

{ CalculateArea C;

C.Area(5);

C.Area(5,3);

C.Area(7,2.1f);

C.Area(4.7f,2); }

13. TO CALCULATE SUM USING FUNCTION


OVERLOADING.

#include<iostream.h>

#include<conio.h>

class Addition

{ public:

void sum(int a, int b)

{ cout<<a+b; }

void sum(int a, int b, int c)

{ cout<<a+b+c; }

};
void main()

{ clrscr();

Addition obj;

obj.sum(10, 20);

cout<<endl;

obj.sum(10, 20, 30);

14. USING THE SAME FUNCTION TO PRINT


DIFFERENT DATA TYPES.

#include <iostream>

using namespace std;

class printData

{ public: void print(int i)

{ cout << "Printing int: " << i << endl; }

void print(double f)

{ cout << "Printing float: " << f << endl; }

void print(char* c)

{ cout << "Printing character: " << c << endl; }

};

int main(void) {

printData pd;

pd.print(5);

pd.print(500.263);
pd.print("Hello C++");

return 0; }
15. To swap two numbers using class
#include<iostream.h>
#include<conio.h>

class swap
{
int a,b;
public:
void getdata();
void swapv();
void display();
};

void swap::getdata()
{ cout<<“Enter two numbers:”;
cin>>a>>b; }

void swap::swapv()
{ a=a+b;
b=a-b;
a=a-b; }

void swap::display()
{ cout<<“a=”<<a<<“tb=”<<b; }

main()
{
clrscr();
swap s;

s.getdata();
cout<<“nBefore swap:n”;
s.display();

s.swapv();
cout<<“nnAfter swap:n”;
s.display();

getch();
return 0;
}
16. Program That Defines a Class String and Overload
[Operator to Compare Two Strings]
#include<iostream>
#include<stdio.h> //used
for gets()
#include<string.h> //used for strcmp()
using namespace std;
class String
{
char str[20];
public:
void
getdata() //function to read the string
{

gets(str);
}
//operator
function to overload comparison operator and compare two strings
int operator
==(String s)
{

if(!strcmp(str,s.str))
return
1;
return
0;
}
};
main()
{
String s1,s2;

cout<<“Enter first string:”;


s1.getdata();

cout<<“Enter second string:”;


s2.getdata();

if(s1==s2) //here
the operator function will be called
{ cout<<“nStrigs are Equaln”; }
else
{ cout<<“nStrings are Not Equaln”; }

return 0;
}

17. Program to Print Numbers From 1 to n Without Using


Loops
#include<iostream>
using namespace std;
class Num
{
public:
static int i;
Num()
{
cout<<i++<<” “;
}
};
int Num::i=1;
int main()
{
int n;
cout<<“Enter value on n:”;
cin>>n;
Num obj[n];
return 0; }

18. Travel plan [using class]


include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
class travelplan
{long plancode;
char place[10];
int noofpeople;
int noofbus;
public:
travelplan();

void newplan();
void showplan();
}travel;
void travelplan::travelplan()
{cout<<"enter the plancode"<<endl;
cin>>plancode;
cout<<endl;
cout<<"enter the place"<<endl;
gets(place);
cout<<endl;
cout<<"enter the no of travellers"<<endl;
cin>>noofpeople;
cout<<endl;
}
void travelplan::newplan()
{if(noofpeople<20)
{noofbus=1;}
else if((noofpeople>=20)&&(noofpeople<40))
{noofbus=2;}
else if(noofpeople>=40)
{noofbus=3;}
}
void travelplan::showplan()
{cout<<"Plan code : "<<plancode<<endl;
cout<<"Place : ";
puts(place);
cout<<endl;
cout<<"No of travellers : "<<noofpeople<<endl;
cout<<"No of bus : "<<noofbus<<endl;
}
void main()
{travel.newplan();
travel.showplan();
getch();
}
19. Publication [using class]
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
class publication
{
char title[20];
float price;
public:
void getdata()
{
cout<<"Enter Title"<<endl;
gets(title);
cout<<"Enter Price"<<endl;
cin>>price;
}
void putdata()
{
cout<<"Title"<<endl;
puts(title);
cout<<"Price"<<endl;
cout<<price;
}};
class book : protected publication
{
int pagecount;
public:
void putdata()
{
publication :: putdata();
cout<<endl<<"Pg Count"<<endl;
cout<<pagecount;
}
void getdata()
{
publication::getdata();
cout<<"Pg Count"<<endl;
cin>>pagecount;
}
};
class tape : protected publication
{
float time;
public:
void getdata()
{
publication :: getdata();
cout<<"Enter Time"<<endl;
cin>>time;
}
void putdata()
{
publication :: putdata();
cout<<endl<<"Time "<<endl;
cout<<time;
}
};
void main()
{
clrscr();
book b;
tape t;
int ch;
x:
{
cout<<endl<<"1. BOOK 2:Tape 3:Exit "<<endl;
cin>>ch;
switch(ch)
{
case 1:b.getdata();
b.putdata();
goto x;
case 2:t.getdata();
t.putdata();
goto x;
case 3:exit(0);
}
}
getch();
}
20. Merge two arrays
#include<iostream.h>
#include<conio.h>
int c[100],i,j,k;
class merged
{
int na,nb,a[50],b[50];
public:
void merge();
void display();
void input();
}g;
void merged::merge()
{
int j=0;
int i=na-1;
while((i>0)&&(j<nb))
{
if (b[j]<a[i])
{
c[k]=a[i];
i--;
k++;
}
if (b[j]>a[i])
{
c[k]=b[j];
j++;
k++;
}
if (b[j]==a[i])
{
c[k]=b[j];
j++;
k++;
c[k]=a[i];
i--;
k++;
}
}
if(i==-1)
{
while(j<nb)
{
c[k]=b[j];
j++;
k++;
}
}
if(j==nb)
{
while(i>=0)
{
c[k]=a[i];
i--;
k++;
}}
}
void merged::display()
{
for(i=0;i<na+nb;i++)
cout<<c[i];
}
void merged::input()
{
cout<<"Enter A Lim"<<endl;
cin>>na;
cout<<"Enter B Limit"<<endl;
cin>>nb;
cout<<"Enter array A"<<endl;
for(i=0;i<na;i++)
cin>>a[i];
cout<<"Enter Array B"<<endl;
for(j=0;j<nb;j++)
cin>>b[j];
}
void main()
{
clrscr();
g.input();
g.merge();
g.display(); }
SQL
COMMANDS
Show databases;

Create database;
Create and desc table
Insert & select

Alter table- add


Alter table drop

Select-Like
Select- order by

Create view

You might also like