C++ Practical File
C++ Practical File
C++ Practical File
Program 1:
to search an
COMPUTER SCIENCE Program
element
of an array using
search. PRACTICAL FILE Binary
#include<iostream.h>
#include<process.h>
#include<conio.h>
void main()
cout<<A[i]<<" ";
binary_search(A,n,p);
getch(); }
while(L<=U)
{ mid=(L+U)/2; if(A[mid]==p)
break; }
else if(p<=A[mid])
U=mid-1;
else \
if(L>U)
{cout<<"\n\nUnsuccessful search.";
cin>>p;
goto lb;}
else exit(1);
}}
Program 2: WAP USING MULTIPLE INHERITANCE FOR THE CLASSES STUDENT, GAME AND PERSON.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class person{
void indata()
gets(name);
cin>>age;
void outdata();};
void person::outdata()
class game {
void input()
cin.get();cin.getline(game_name,20);
void output()
}
};
return 'A';
else if(Tmarks>80&&Tmarks<=90)
return 'B';
else if(Tmarks>70&&Tmarks<=80)
return 'C';
else if(Tmarks>60&&Tmarks<=70)
void enter()
indata();
input();
void display()
outdata();
cout<<"\n\nGrade = "<<calgrade();
} };
A.display();
getch();
#include<iostream.h>
#include<conio.h>
int A[50]; int n; int p; int subscript; /*Note: subscript and index are same.*/ cout<<"Enter the array size :
";
cout<<A[i]<<" ";
void linear_search ( int A[], int n, int p); //function declaration linear_search(A,n,p); getch();
void linear_search(int A[], int n, int p) { int count=0; int B[50]; int flag=0;
flag=count;
if(flag==0)
else
{ if(A[i]==p)
}}
}
Program 4: Wap using pointers to find the length of a string and print the reversed string.
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{ clrscr();
{ intermediate = *(str+j);
*(str+j)=*(str+i);
*(str+i)=intermediate;
cout<<str; getch();
#include<iostream.h>
#include<math.h>
#include<conio.h>
{ return pow(a,b);}
{ return pow(a,b); }
{ return pow(a,b); }
float calc(float a, int b)
{ return pow(a,b); }
void main()
clrscr();
int P,Q;
float R,S;
cout<<R<<"^"<<S<<" = "<<pow(R,S);
getch();
#include<iostream.h>
#include<conio.h>
void swap_using_pointers(int *, int *);
void main()
clrscr();
int a,b;
cout<<"\n\nEnter first integer : "; cin>>a; cout<<"\n\nEnter second integer : "; cin>>b;
swap_using_pointers(&a,&b);
cout<<"\n\nNow value of first integer = "<<a; cout<<"\n\nNow value of second integer = "<<b;
getch();
*b=temp;
}
Program 7: Program to sales of 5 salesman in 12 months and to print total sales made by each salesman.
#include<iostream>
int main()
{ int sales[2][6];
int i,j,total;
for(i=0;i<2;++i)
for(j=0;j<6;j++)
{ cout<<"\nMonth"<<j+1<<":";
cin>>sales[i][j];
total=total+sales[i][j]; }
cout<<endl;
return 0;}
Program 8:Program to sort an array in ascending order using Selection sort.
#include<iostream.h>
#include<conio.h>
cout<<"\n\nEnter the elements of array : \n\n"; for(int i=0; i<n; i++) cin>>A[i];
cout<<A[i]<<" ";
getch();
{int small; int k,count=0; for(int i=0; i<n; i++) { small=A[i]; count++;
if(A[j]<small)
{small=A[j];
A[j]=A[i];
A[i]=small;
cout<<A[k]<<" ";
}
Program 9: Wap to create a text file to input roll no. and marks of ten students and display them on
screen after reading from the text file using data file handling.
#include<fstream.h>
#include<conio.h>
void main()
{ clrscr();
int a[10];
float b[10];
cin>>a[i];
cin>>b[i]; }
ofstream student;
student.close();
i=0;
ifstream stude;
stude.open("stud.txt",ios::in);
stude.seekg(0);
while(!stude.eof())
{stude.getline(str[i],80);
cout<<str[i]<<"\n\n";
i++;
getch();
}
Program 10: Program to illustrate the working of a function an object.
#include<iostream>
class Distance
public:
feet=f; inches=i;
void printit(void)
Distance d3;
d3.feet=feet+d2.feet+(inches + d2.inches)/12;
d3.inches=(inches + d2.inches)%12;
return (d3);
};
int main()
Length1.getdata(17,6);
Length2.getdata(13,8);
total=Length1.sum(Length2);
cout<<"Length 1: ";
Length1.printit();
cout<<"Length 2: ";
Length2.printit();
total.printit();
return 0; }
#include<iostream>
#include<fstream>
#include<stdlib.h>
int main()
{ system("cls");
ofstream filout;
filout.open("stunames",ios::out);
filout.close();
filout.open("stumarks",ios::out);
filout<<"78.92\n"<<"72.17\n"<<"69.33\n";
filout.close();
char line[80];
ifstream filin;
filin.open("stunames",ios::in);
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.close();
filin.open("stumarks",ios::in);
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.close();
return 0; }
Program 12: Wap in c++ using pointers to find the smallest and the largest element in a dynamically
created array.
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
array=new[n];
while(i!=n)
smallest=array[0];
if(array[i]<=smallest)
if(array[i]>=largest) largest=array[i];
getch();
}
Program 13: Program to illustrate working of default argument. Calculate interest amount making use of
default argument.
#include<iostream>
#include<stdlib.h>
cout<<"\tRate: "<<rate;
}
int main()
system("cls");
cout<<"Case 1";
amount(2000);
cout<<"Case 2";
amount(2500,3,0.11);
cout<<"Case 4";
amount(2500,0.08);
return 0;
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int a,b;
cout<<"\n\nEnter first integer : "; cin>>a; cout<<"\n\nEnter second integer : "; cin>>b;
swap_using_pointers(&a,&b);
cout<<"\n\nNow value of first integer = "<<a; cout<<"\n\nNow value of second integer = "<<b;
getch();
*b=temp; }
Program 15: Get roll numbers and marks of the students of a class (get from the user) and stores these
details into a file called Marks.dat.
#include<iostream>
#include<fstream>
int main()
ofstream filout;
filout.open("marks.dat",ios::out);
char ans='y';
while(ans=='y' || ans=='Y')
cin>>rollno;
cin>>marks;
cin>>ans;
filout.close();
return 0;}
Program 16: Program to create a single file and then display its contents.
#include<iostream>
#include<fstream>
#include<stdlib.h>
int main()
{ system("cls");
cin.get(name,30);
cout<<"\t\tMarks: ";
cin>>marks;
cin.get(ch);
fout<<name<<"\n"<<marks<<"\n";
fout.close();
fin.seekg(0);
cout<<"\n";
for(int i=0;i<5;i++)
fin.get(name,30);
fin.get(ch);
fin>>marks;
fin.get(ch);
cout<<"\tMarks: "<<marks<<"\n";}
fin.close();
return 0;}
Program 17: Wap to create a text file to input roll no. and marks of five students and display them on
screen after reading from the text file using data file handling.
#include<fstream.h>
#include<conio.h>
void main()
clrscr();
cin>>a[i];
cin>>b[i];
student.close();
i=0;
char str[80][80];
ifstream stude;
stude.open("stud.txt",ios::in); stude.seekg(0);
while(!stude.eof())
getch(); }
class X
int codeno;
float price;
public:
codeno=i;
price=j;
++count;
void display(void)
cout<<"Price: "<<price<<"\n";
cout<<"count= "<<count<<endl;
};
int X::count=0;
int main()
X ob1,ob2;
ob1.getval(101,25.12);
ob2.getval(102,38.19);
X::discount();
X ob3;
ob3.getval(103,49.00);
X::discount();
ob1.display();
ob2.discount();
ob3.display();
return 0;}
Program 19: Read a text file data.xyz ana create a file that stores the text line by lime along with line
numbers.
#include<iostream>
#include<fstream>
#include<stdlib.h>
int main()
("out.txt",ios::app);
ifstream infile("da
char myline[75];
int lc=0;
exit(1);
while(1)
infile.getline(myline,75,'.');
if(infile.eof())
break;
lc++;
outfile<<lc<<":"<<myline<<"\n";
infile.close();
outfile.close();
cout<<"Output"<<lc<<"records"<<endl;
return 0;
Program 20: A file named marks.dat already stores some students' details like rollno and marks. Write a
program that reads more such details and appends them to this file. Make sure that previous contents
of the file are not lost.
#include<iostream>
#include<fstream>
#include<stdlib.h>
int main()
ofstream fout;
fout.open("marks.dat",ios::app);
char ans='y';
int rollno;
float marks;
system("cls");
while(ans=='y'||ans=='Y')
cin>>rollno;
cin>>marks;
fout<<rollno<<'\n'<<marks<<'\n';
cin>>ans;
fout.close();
return 0;
}
Program 21: Program for reading and writing class objects using read() and write() function.
#include<fstream.h>
#include<conio.h>
class Student
char name[40];
char grade;
float marks;
public:
void getdata();
void display();
};
char ch;
cin.get(ch);
cout<<"\n Enter name:";
cin.getline(name, 40);
cin>>grade;
cin>>marks;
cout<<endl;
cout<<"Name: "<<name<<"\t"
<<"Grade: "<<grade<<"\t"
<<"Marks: "<<marks<<"\t"<<endl;
int main()
clrscr();
if(!filin)
return 1;
for(int i=0;i<3;i++)
ob[i].getdata();
filin.write((char *) & ob[i], sizeof (ob[i]));
filin.seekg(0);
for(i=0;i<3;i++)
ob[i].display();
filin.close();
getch();
return 0;
}
The output to display the content of the file
#include<iostream>
#include<fstream>
#include<stdlib.h>
int main()
system("cls");
ofstream fout;
fout.open("Aschars",ios::app);
if(!fout)
return 1;
for(int i=33;i<128;i++)
fout.put((char)i);
fout.close();
ifstream fin;
fin.open("Aschars",ios::in);
fin.seekg(0);
for(int i=33;i<128;i++)
fin.get(ch);
cout.put((char)i);
if(!(i%8))
cout<<endl;
line++;
if(line>22)
system("Pause");
line=0;
return 0;
}
Program 23: Program for creating a text file character by character till semicolon is entered by user.
#include<fstream>
#include<stdio.h>
#include<conio.h>
int main()
char ch;
ofstream fout("textfille.txt");
do
ch=getche();
fout<<ch;
}while(ch!=';');
fout.close();
return 0;
}
Program 24: Program to write and read a structure using write() and read() function using a binary file.
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
struct customer
char name[51];
float balance;
};
int main()
system("cls");
customer savac;
strcpy(savac.name,"Tina Marshall");
savac.balance=21310.75;
ofstream fout;
fout.open("Saving",ios::out|ios::binary);
if(!fout)
return 1;
fout.write((char*)&savac,sizeof(customer));
fout.close();
ifstream fin;
fin.open("Saving",ios::in|ios::binary);
fin.read((char*)&savac,sizeof(customer));
cout<<savac.name;
fin.close();
return 0;
#include<iostream>
#include<fstream>
#include<stdlib.h>
int main()
{system("cls");
char ch;
ifstream fin;
fin.open("matks.dat",ios::in);
if(!fin){
return 1;}
while(fin)
{
fin.get(ch);
cout<<ch;
fin.close();
return 0;
#include<iostream.h>
#include<conio.h>
#include<process.h>
int pop(int[],int&);
int push(int[],int&,int);
void display(int[],int);
void main()
{ clrscr();
char m,ch;
int k,stack[size],item,top=-1,res;
do
<<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display" //Main Menu
<<"\n 4. Exit"
switch(k)
{ case 1: ch='y';
while(ch=='y'||ch=='Y')
cin>>item;
res=push(stack,top,item); if(res==-1)
{cout<<"\nOverflow !!!!";
exit(1); }
cin>>ch;
} break;
{ res=pop(stack,top);
if(res==-1)
cout<<"\nUnderflow !!!!";
exit(1);
else
break;
case 3: cout<<"\nThe resultant stack is : "; display(stack,top);
break;
case 4: exit(0);
break;
} // end of switch
} // end of main()
if(top==size-1)
top++;
stack[top]=el; return 0;
int ret;
ret=stack[top];
cout<<stack[top]<<"<--";
sql queries
Table DEPT
Table WORKER
To display the Wno, Name , Gender from the table WORKER in desending order of Wno.
To display the Name of all the female workers from the table WORKER.
To display the WNo and Name of those workers from the table WORKER who are born between
1987-01-01 and 1991-12-01.
Give the output of the following queries based on the tables Books and Issued :
Table Students
To insert a new row in the Student table with the following data:
9, Zaheer, 36, Computer, {12/03/97}, 230, M
Give the output of the following queries based on the tables Books and Issued :
Table Issued
To show Book name, Author name and Price of books of First Publ. publishers.
To display the names and price of all books from books in ascending order of their price.
To increase the piece of all books of EPB Publishers by 50.
To insert a new row in the table Issued having the following data F0003, 1
Give the output of the following queries based on the tables Books and Issued :
SELECT COUNT(*) FROM Books;