Nothing Special   »   [go: up one dir, main page]

C++ Practical File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 51

C++ Programs

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()

{ clrscr(); int A[50], n,p;

cout<<"Enter the Size of array : "; cin>>n;

cout<<"\n\nEnter the elements : \n\n"; for(int i=0; i<n; i++) cin>>A[i];

cout<<"\n\nArray formed is : "; for(i=0; i<n; i++)

cout<<A[i]<<" ";

cout<<"\n\nEnter the element to be searched : "; cin>>p;

void binary_search(int A[], int n, int p); //declaration

binary_search(A,n,p);

getch(); }

void binary_search(int A[], int n, int p)

{ int L,U,mid; char ch;

lb: L=0; U=n-1;

while(L<=U)

{ mid=(L+U)/2; if(A[mid]==p)

{ cout<<"\n\nElement "<<p<<" found. Search Successful."; cout<<"\n\nSubscript = "<<mid<<"


\n\nPosition = "<<mid+1;

break; }

else if(p<=A[mid])

U=mid-1;
else \

=mid+1; }//end of while loop

if(L>U)

{cout<<"\n\nUnsuccessful search.";

cout<<"\n\n\n\nWant to search again. : "; cin>>ch; if(ch=='y'||ch=='Y')

{cout<<"\n\n\n\nEnter the element to be searched : ";

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{

char name[21]; int age; public:

void indata()

{cout<<"\n\nEnter the name of Student: " ;

gets(name);

cout<<"\n\nEnter the age : ";

cin>>age;

void outdata();};

void person::outdata()

cout<<"\n\n"; for(int i=0; i<79; i++) cout<<"-";

cout<<"\n\nName of the student is: "<<name; cout<<"\n\nAge of the student is : "<<age;

class game {

char game_name[20]; public:

void input()

cout<<"\n\nEnter the game name : ";

cin.get();cin.getline(game_name,20);

void output()

cout<<"\n\nGame opted by the student is : "<<game_name;

}
};

class student: public person, public game

{ float Tmarks; // Multiple inheritance int rollno; public:

char calgrade() {if(Tmarks>90)

return 'A';

else if(Tmarks>80&&Tmarks<=90)

return 'B';

else if(Tmarks>70&&Tmarks<=80)

return 'C';

else if(Tmarks>60&&Tmarks<=70)

return 'D'; else return 'E';

void enter()

indata();

cout<<"\n\nEnter the roll number: "; cin>>rollno;

input();

cout<<"\n\nEnter total marks (out of 100) : "; cin>>Tmarks;

void display()

outdata();

cout<<"\n\nRoll number : "<<rollno;

cout<<"\n\nTotal marks are : "<<Tmarks;

cout<<"\n\nGrade = "<<calgrade();
} };

void main() { clrscr(); student A; A.enter();

cout<<\n\nstudent details are : \n\n;

A.display();

getch();

Program 3: Program to search an element of an array using Linear search method.

#include<iostream.h>

#include<conio.h>

void main() { clrscr();

int A[50]; int n; int p; int subscript; /*Note: subscript and index are same.*/ cout<<"Enter the array size :
";

cin>>n; // n=size upto which user wants to insert values in array

cout<<"\n\nEnter elements of array : \n"; for(int i=0; i<n; i++) cin>>A[i];

cout<<"\n\nThe array formed = ";

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

cout<<A[i]<<" ";

cout<<"\n\nEnter the element to be searched : "; cin>>p; // p=element to be searched

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;

for(int i=0; i<n; i++)


{ flag=count; if( A[i]==p)

B[count]=i; count++; //Linear Search..

flag=count;

if(flag==0)

cout<<"\n\nRequested element not found.";

else

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

{ if(A[i]==p)

{ cout<<"\n\nElement "<<p<<" is found";

cout<<"\n\nSubscript = "<<i<<"\n\nPosition = "<<i+1<<"\n\n\n";

}}

}
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();

char *str= new char[256];

cout<<"\n\nEnter the string : "; cin.getline(str,256);

cout<<"\n\nThe string length => "<<strlen(str);

cout<<"\n\nThe reverse string is : ";


char intermediate;

for(int j=strlen(str)-1,i=0; i<=strlen(str)/2; j--,i++)

{ intermediate = *(str+j);

*(str+j)=*(str+i);

*(str+i)=intermediate;

cout<<str; getch();

Program 5: WAP USING FUNCTION OVERLOADING TO CALCULATE A^B.

#include<iostream.h>

#include<math.h>

#include<conio.h>

int calc(int a, int b)

{ return pow(a,b);}

float calc(float a, float b)

{ return pow(a,b); }

float calc(int a, float 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<<"Enter int P : "; cin>>P;

cout<<"Enter int Q : "; cin>>Q;

cout<<"Enter real R : "; cin>>R;

cout<<"Enter real S : "; cin>>S;

cout<<P<<"^"<<Q<<" = "<<pow(P,Q)<<"\n\n"; // Using pow from math.h


cout<<P<<"^"<<R<<" = "<<pow(P,R)<<"\n\n"; cout<<R<<"^"<<Q<<" = "<<pow(R,Q)<<"\n\n";

cout<<R<<"^"<<S<<" = "<<pow(R,S);

getch();

Program 6: Wap using pointers to swap two integers.

#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();

void swap_using_pointers(int *a,int *b)

int temp; temp=*a; *a=*b;

*b=temp;

}
Program 7: Program to sales of 5 salesman in 12 months and to print total sales made by each salesman.

#include<iostream>

using namespace std;

int main()

{ int sales[2][6];

int i,j,total;

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

{ total=0;cout<<"Enter sales for salesman"<<i+1<<":\n";

for(j=0;j<6;j++)

{ cout<<"\nMonth"<<j+1<<":";

cin>>sales[i][j];

total=total+sales[i][j]; }

cout<<endl;

cout<<"TOtal sales of salesman"<<i+1<<" = "<<total<<endl;

return 0;}
Program 8:Program to sort an array in ascending order using Selection sort.

#include<iostream.h>

#include<conio.h>

void main() {clrscr(); int A[80],n;

lb: cout<<"Enter the size of array : "; cin>>n; if(n<=0||n>80)

{cout<<"\n\nEnter size of array less than or equal to 80."; goto lb;

cout<<"\n\nEnter the elements of array : \n\n"; for(int i=0; i<n; i++) cin>>A[i];

void selection_sort(int A[],int n); selection_sort(A,n);

cout<<"\n\n\n\n\nSorted array is : \n\n"; for(i=0; i<n; i++)

cout<<A[i]<<" ";
getch();

void selection_sort(int A[], int n) // Function to sort using selection sort

{int small; int k,count=0; for(int i=0; i<n; i++) { small=A[i]; count++;

for(int j=i+1; j<n; j++)

if(A[j]<small)

{small=A[j];

A[j]=A[i];

A[i]=small;

cout<<"\n\nArray after iteration "<<count<<" is :\n\n"; for(k=0; k<n; k++)

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];

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


{ cout<<"\n\nEnter the roll no. of student "<<i+1<<" : ";

cin>>a[i];

cout<<"\n\tEnter the marks of student "<<i+1<<" => ";

cin>>b[i]; }

ofstream student;

student.open("stud.txt"); for(i=0; i<10; i++)

{ student<<"\n\nMarks of roll no. "<<a[i]<<" => "<<b[i]; }

student.close();

i=0;

cout<<\n\nStudent details are :\n\n; char str[80][80];

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>

using namespace std;

class Distance

int feet, inches;

public:

void getdata(int f, int i)

feet=f; inches=i;

void printit(void)

cout<<feet<<" feet "<<inches<<" inches \n";

Distance sum(Distance d2)


{

Distance d3;

d3.feet=feet+d2.feet+(inches + d2.inches)/12;

d3.inches=(inches + d2.inches)%12;

return (d3);

};

int main()

Distance Length1, Length2, total;

Length1.getdata(17,6);

Length2.getdata(13,8);

total=Length1.sum(Length2);

cout<<"Length 1: ";

Length1.printit();

cout<<"Length 2: ";

Length2.printit();

cout<<"Total Length: ";

total.printit();

return 0; }

Program 11: Program to use multiple files in succession.

#include<iostream>
#include<fstream>

#include<stdlib.h>

using namespace std;

int main()

{ system("cls");

ofstream filout;

filout.open("stunames",ios::out);

filout<<"Devyani\n"<<"Monica Patrick\n"<<"Neil Banerjee\n";

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);

cout<<"The contents of stunames file are given below\n";

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);

cout<<"\nThe contents of stumarks file are given below\n";

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();

int *array, smallest, largest, n;

cout<<"\n\nEnter the number of elements : "; cin>>n;

array=new[n];

cout<<"\n\nEnter the elements : \n\n"; for(int i=0; i<n; i++) cin>>array[i];


i=0;

cout<<"\n\nThe array formed is : \n\n";

while(i!=n)

cout<<array[i]<<" "; i++;

smallest=array[0];

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

if(array[i]<=smallest)

smallest=array[i]; //Comparing the elements

largest=array[0]; for(i=0; i<n; i++)

if(array[i]>=largest) largest=array[i];

cout<<"\n\nThe smallest element is : "<<smallest<<"\n\nThe largest element is : "<<largest;

getch();

}
Program 13: Program to illustrate working of default argument. Calculate interest amount making use of
default argument.

#include<iostream>

#include<stdlib.h>

using namespace std;

void amount(float princ, int time=2,float rate=0.08);

void amount(float princ,int time,float rate)

cout<<"\nPrinciple Amount: Rs."<<princ;

cout<<"\tTime: "<<time<<" years";

cout<<"\tRate: "<<rate;

cout<<"\nInterest Amount: "<<(princ*rate*time)<<endl;

}
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;

Program 14: Wap using pointers to swap two integers.

#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();

void swap_using_pointers(int *a,int *b)

int temp; temp=*a; *a=*b;

*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>

using namespace std;

int main()

ofstream filout;

filout.open("marks.dat",ios::out);

char ans='y';

int rollno; float marks;

while(ans=='y' || ans=='Y')

cout<<"\n Enter Rollno.: ";

cin>>rollno;

cout<<"\n Enter marks: ";

cin>>marks;

filout<< rollno <<'\n' << marks <<endl;


cout<<"\n Want to enter more record?(y/n)...";

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>

using namespace std;

int main()

{ system("cls");

ofstream fout("student", ios::out);

char name[30], ch;

float marks =0.0;


for(int i=0;i<5;i++)

cout<<"Student "<<(i+1)<<":\tName: ";

cin.get(name,30);

cout<<"\t\tMarks: ";

cin>>marks;

cin.get(ch);

fout<<name<<"\n"<<marks<<"\n";

fout.close();

ifstream fin("student", ios::in);

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<<"Student Name: "<<name;

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();

int a[5]; float b[5];

for(int i=0; i<5; i++)

cout<<"\n\nEnter the roll no. of student "<<i+1<<" : ";

cin>>a[i];

cout<<"\n\tEnter the marks of student "<<i+1<<" => ";

cin>>b[i];

ofstream student; student.open("stud.txt");

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

student<<"\n\nMarks of roll no. "<<a[i]<<" => "<<b[i];

student.close();
i=0;

cout<<\n\nStudent details are :\n\n;

char str[80][80];

ifstream stude;

stude.open("stud.txt",ios::in); stude.seekg(0);

while(!stude.eof())

stude.getline(str[i],80); //reading from stud.txt


cout<<str[i]<<"\n\n"; i++;

getch(); }

Program 18: Program to keep count objects using static members.


#include<iostream>

using namespace std;

class X

int codeno;

float price;

static int count;

public:

void getval(int i,float j)

codeno=i;

price=j;

++count;

void display(void)

cout<<"Code no: "<<codeno<<'\t';

cout<<"Price: "<<price<<"\n";

static void discount(void)

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>

using namespace std;

int main()

("out.txt",ios::app);

ifstream infile("da

char myline[75];

int lc=0;

ofstream outfile ta.xyz",ios::in);


if(!infile)

cerr<<"Failed to open input file\n";

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>

using namespace std;

int main()

ofstream fout;

fout.open("marks.dat",ios::app);

char ans='y';

int rollno;

float marks;

system("cls");

while(ans=='y'||ans=='Y')

{cout<<"\n Enter Rollno.:";

cin>>rollno;

cout<<"\n Enter Marks:";

cin>>marks;

fout<<rollno<<'\n'<<marks<<'\n';

cout<<"\n Want to enter more records?(y/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();

};

void Student :: getdata()

char ch;

cin.get(ch);
cout<<"\n Enter name:";

cin.getline(name, 40);

cout<<"\n Enter grade: ";

cin>>grade;

cout<<"\n Enter Marks: ";

cin>>marks;

cout<<endl;

void Student :: display()

cout<<"Name: "<<name<<"\t"

<<"Grade: "<<grade<<"\t"

<<"Marks: "<<marks<<"\t"<<endl;

int main()

clrscr();

Student ob[3]; //declare array of 3 objects

fstream filin; //input and output file

filin.open("Stu.dat", ios :: in | ios :: out);

if(!filin)

cout<<"\n File not found! \n";

return 1;

cout<<"\nEnter details for 3 students: \n";

for(int i=0;i<3;i++)

ob[i].getdata();
filin.write((char *) & ob[i], sizeof (ob[i]));

filin.seekg(0);

cout<<"\n The contents of file.... \n";

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

filin.read((char *) & ob[i], sizeof (ob[i]));

ob[i].display();

filin.close();

getch();

return 0;

}
The output to display the content of the file

Program 22: Program to create a file using put().

#include<iostream>

#include<fstream>

#include<stdlib.h>

using namespace std;

int main()

system("cls");

ofstream fout;

fout.open("Aschars",ios::app);

if(!fout)

cout<<" The file cannot be opened !!\n";

return 1;

char ch; int line=0;

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<<" "<<i<<" = ";

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>

using namespace std;

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>

using namespace std;

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)

{cout<<"File can\'t be opened \n";

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;

cout<<"has the balance amout of Rs."<<savac.balance<<"\n";

fin.close();

return 0;

Program 25: Program to display contents of a file using get() function.

#include<iostream>

#include<fstream>

#include<stdlib.h>

using namespace std;

int main()

{system("cls");

char ch;

ifstream fin;

fin.open("matks.dat",ios::in);

if(!fin){

cout<<"Cannot open file!!\n";

return 1;}

while(fin)

{
fin.get(ch);

cout<<ch;

fin.close();

return 0;

Program 26: Wap in c++ to implement stack as an array.

#include<iostream.h>

#include<conio.h>

#include<process.h>

int pop(int[],int&);

int push(int[],int&,int);

void display(int[],int);

const int size=50;

void main()

{ clrscr();

char m,ch;

int k,stack[size],item,top=-1,res;

do

{ cout<<"\nChoose from the following : \n\n"

<<"\n 1. Push"

<<"\n 2. Pop"
<<"\n 3. Display" //Main Menu

<<"\n 4. Exit"

<<"\n\nEnter your choice : "; cin>>k;

switch(k)

{ case 1: ch='y';

while(ch=='y'||ch=='Y')

{ cout<<"\nEnter the element : ";

cin>>item;

res=push(stack,top,item); if(res==-1)
{cout<<"\nOverflow !!!!";

exit(1); }

cout<<"\nThe stack formed is : \n\n"; display(stack,top);

cout<<"\n\n\nWant to enter again ?: ";

cin>>ch;

} break;

case 2: ch='y'; while(ch=='y'||ch=='Y')

{ res=pop(stack,top);

if(res==-1)

cout<<"\nUnderflow !!!!";

exit(1);

else

cout<<"\nThe deleted Element is : "<<res<<endl;

cout<<"\nThe resultant stack is : \n\n";


display(stack,top); } cout<<"\nWant to delete again ? : ";
cin>>ch; }

break;
case 3: cout<<"\nThe resultant stack is : "; display(stack,top);
break;

case 4: exit(0);

break;

default: cout<<"\nPlease enter desired keyword : ";

} // end of switch

cout<<"\n\nChoose from the menu again ? : "; cin>>m;

}while(m=='y'||m=='Y'); // end of do-while loop getch();

} // end of main()

int push(int stack[],int &top,int el)

if(top==size-1)

return -1; else

top++;

stack[top]=el; return 0;

int pop(int stack[],int &top)

int ret;

if(top==-1) return -1; else


{

ret=stack[top];

top--; return ret;

void display(int stack[],int top)

cout<<stack[top]<<"<--";

for(int i=top-1;i>=0;i--) cout<<stack[i]<<"<";

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.

To count and display Male workers who have joined after1986-01-01.

Give the output of the following queries based on the tables Books and Issued :

SELECT COUNT(*), DCODE FROM WORKER GROUP BY DCODE HAVING COUNT(*)>1;

SELECT DISTINCT DEPARTMENT FROM DEPT;


SELECT MAX(DOJ), MIN(DOB) FROM WORKER:

Table Students

To show all the information about the students of History department.

To list the names of female students who are in Hindi Department.


To list the names of all students with their date of admission in ascending order.

To display students Name, Fee, Age for male Students only.

To count the number of student with Age<23.

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 :

Select COUNT(distinct department) from Student;

Select MAX(Age) from Student where Sex=F;

Select AVG(Fee) from Student;

Select AVG(Fee) from Student;


Table Books

Table Issued

To show Book name, Author name and Price of books of First Publ. publishers.

To list the names from books of Text type.

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 count the number of distinct Publishers.

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;

SELECT MAX(Price) FROM Books WHERE Quantity>=15;

SELECT Book_Name, Author_Name FROM Books WHERE Price >= 400;

SELECT Book_Name, Author_Name FROM Books WHERE Prublishers = EPB;

You might also like