CS Practical File
CS Practical File
#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 1st int number : "; cin>>P;
cout<<"Enter 2nd number : "; cin>>Q;
cout<<"Enter 1st real number : "; cin>>R;
cout<<"Enter 2nd real number : "; cin>>S;
cout<<P<<"^"<<Q<<" = "<<pow(P,Q)<<"\n\n";
cout<<P<<"^"<<R<<" = "<<pow(P,R)<<"\n\n";
cout<<R<<"^"<<Q<<" = "<<pow(R,Q)<<"\n\n";
cout<<R<<"^"<<S<<" = "<<pow(R,S);
getch();
}
OUTPUT:
// WAP USING FUNCTION OVERLOADING TO CALCULATE AREA OF CIRCLE, SQUARE AND RECTANGLE.
#include<iostream.h>
#include<math.h>
#include<conio.h>
float area(float p)
{
return 3.14*p*p;
}
float area(int p)
{
return p*p;
}
float area(float p,float q)
{
return p*q;
}
void main()
{
clrscr();
int p,q,ch;
float a,b,r;
char choice;
do{
cout<<"\n\nChoose from the following : ";cout<<"\n\n1. Area of square ";cout<<"\n\n2. Area of circle ";
cout<<"\n\n3. Area of rectangle ";cout<<"\n\nEnter your choice : "; cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\nEnter side of square : ";
cin>>p;
cout<<"\n\narea of square is : "<<area(p);
break;
case 2:
case 3:
}
cout<<"\n\nWant to choose again : ";
cin>>choice;
}
while(choice=='y'||choice=='Y');
getch();
}
OUTPUT
/* Program to define the classes PERSON, GAME and STUDENT & to access the essential data using multiple
inheritance.*/
#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() // since the function contains loop so it is not made inline
{
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;
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();
// indata() of class person called here
cout<<"\n\nEnter the roll number: ";
cin>>rollno;
input();
// input() of class game called here
cout<<"\n\nEnter total marks (out of 100) : ";
cin>>Tmarks;
}
void display()
{
outdata();
cout<<"\n\nRoll number : "<<rollno;
output();
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{
clrscr();
student A;
A.enter();
A.display();
getch();
}
OUTPUT:
// WAP TO CREATE A MENU DRIVEN PROGRAM TO IMPLEMENT THE FOLLOWING METHODS OF SEARCHING.
A)LINEAR SEARCH
B) BINARY SEARCH
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int Linear(int [],int,int);
int Binary(int [],int,int);
int main()
{
clrscr();
int a[50], key,size,pos,i,j;
char ch;
cout<<" Enter the desired size of array(max.50)";
cin>>size;
do
{
cout<<"......MENU......"<<endl;
cout<<"1: LINEAR SEARCH<<endl;
cout<<"2: BINARY SEARCH"<<endl;
cout<<"3: EXIT"<<endl;
cout<<".Enter Your Choice."<<endl;
cin>>i;
switch(i)
{
case 1:
clrscr();
cout<<"......LINEAR SEARCH......"<<endl;
cout<<"Enter "<<size<<" elements"<<endl;
for(j=0;j<size;j++)
{
cin>>a[j];
}
cout<<"Enter Element to be searched"<<endl;
cin>>key;
pos=Linear(a,size,key);
if(pos==-1)
cout<<"\n Sorry!! Element is not present is array"<<endl;
else
cout<<"\n Element found at index:"<<pos<<" position "<<pos+1<<endl;
break;
case 2:
clrscr();
cout<<"......BINARY SEARCH......"<<endl;
cout<<"Enter "<<size<<" elements"<<endl;
for(j=0;j<size;j++)
{
cin>>a[j];
}
cout<<"Enter Element to be searched"<<endl;
cin>>key;
pos=Binary(a,size,key);
if(pos==-1)
cout<<"\n Sorry!! Element is not present is array"<<endl;
else
OUTPUT
// WAP TO CREATE A MENU DRIVEN PROGRAM TO IMPLEMENT THE FOLLOWING METHODS OF SORTING.
//A)INSERTION SORT
B) SELECTION SORT
C) BUBBLE SORT
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void Selection(int [],int);
void Bubble(int [],int);
void insertion(int [],int);
int main()
{
int a[50], key,size,pos,i,j;
char ch;
do
{
cout<<"......MENU......"<<endl;
cout<<"1: INSERTION SORT"<<endl;
cout<<"2: BUBBLE SORT"<<endl;
cout<<"3: SELECTION SORT"<<endl;
cout<<"4: EXIT"<<endl;
cout<<".Enter Your Choice."<<endl;
cin>>i;
switch(i)
{
case 1: system("cls");
cout<<"......INSERTION SORT......"<<endl;
cout<<" Enter the desired size of array(max.50)";
cin>>size;
cout<<"Enter "<<size<<" elements"<<endl;
for(j=1;j<=size;j++)
{
cin>>a[j];
}
insertion(a,size);
cout<<"\n\n The Sorted Array is: \n\n";
for(j=1;j<=size;j++)
cout<<a[j]<<" ";
cout<<endl;
break;
case 2:system("cls");
cout<<"......BUBBLE SORT......"<<endl;
cout<<" Enter the desired size of array(max.50)";
cin>>size;
cout<<"Enter "<<size<<" elements"<<endl;
for(j=0;j<size;j++)
{
cin>>a[j];
}
Bubble(a,size);
cout<<"The Sorted Array is"<<endl;
for(j=0;j<size;j++)
cout<<a[j]<<" ";
cout<<endl;
break;
case 3: system("cls");
cout<<"......SELECTION SORT......"<<endl;
cout<<" Enter the desired size of array(max.50)";
cin>>size;
cout<<"Enter "<<size<<" elements"<<endl;
for(j=0;j<size;j++)
{
cin>>a[j];
}
Selection(a,size);
for(j=0;j<size;j++)
cout<<a[j]<<" ";
cout<<endl;
break;
case 4: exit(0);
break;
default: cout<<"Wrong Choice"<<endl;
break;
}
cout<<"do you want to continue"<<endl;
cin>>ch;
} while(ch=='Y'||ch=='y');
return 0;
}
void Bubble(int a[],int size)
{
int tmp,ctr=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<(size-1)-i;j++)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
cout<<"Array after iteration:: "<<++ctr<<" --is:"<<endl;
for(int k=0;k<size;k++)
cout<<a[k]<<" ";
cout<<endl;
}
}
void Selection(int a[],int size)
{
int small,pos,tmp;
for(int i=0;i<size;i++)
{
small=a[i];
pos=i;
for(int j=i+1;j<size;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
tmp=a[i];
a[i]=a[pos];
a[pos]=tmp;
cout<<"\n Array after pass: "<<i+1<<" is::";
for(int k=0;k<size;k++)
{
cout<<a[k]<<" ";
}
cout<<endl;
}
}
void insertion(int a[],int size)
{
int tmp,j;
a[0]=-1;
for(int i=1;i<=size;i++)
{
tmp=a[i];
j=i-1;
while(tmp<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=tmp;
cout<<"Array after pass::"<<i<<"::is";
for(int k=1;k<=size;k++)
cout<<a[k]<<" ";
cout<<endl;
}
}
OUTPUT
/*Write menu driven program to show following operations in a 2-d array (using user defined function)Menu
1. Adding two 2-d arrays
2. Subtracting two 2-d arrays
3. Multiplying two 2-d arrays
4. Check whether two 2-d arrays are equivalent or not
5. Display upper triangular matrix
6. Display lower triangular matrix
7. Display and find sum of diagonal elements of a 2-d array
8. Display and find the row-wise sum of a 2-d array
9. Display and find the column-wise sum of a 2-d array
10. Quit */
#include<iostream.h>
#include<process.h>
#include<conio.h>
int a[80][80],b[80][80];
// 2-D matrices
int i=0, j=0;
int m, n, p, q;
// Matrix 1 : m= no. of Rows , n=no. of Columns Matrix 2 : p= no. of Rows , q=no. of Columns
int mc, oc, sum=0;
// Function Declarations
void add(int a[80][80],int b[80][80]);
void subtract(int a[80][80],int b[80][80]);
void multiply(int a[80][80],int b[80][80]);
void multiply1(int b[80][80],int a[80][80]);
void insert_mat(int a[80][80],int b[80][80]);
void main() // main body
{
clrscr();
int ch,op; char choice;
do{
cout<<"\n\nChoose from the following menu : ";
cout<<"\n\n1. Add two 2-D arrays ";
cout<<"\n\n2. Subtract two 2-D arrays ";
cout<<"\n\n3. Multiply two 2-D arrays " ;
cout<<"\n\n4. Exit ";
cout<<"\n\n\n\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1 :
insert_mat(a,b);
if(m==p)
if(n==q)
add(a,b);
else
{
cout<<"\n\nNumber of columns of matrices are not same;
}
else
cout<<"\n\nNumber of rows of matrices are not same. ";
break;
case 2 :
insert_mat(a,b);
if(m==p)
if(n==q)
{
case 4 : exit(0);
}
cout<<"\n\n\nWant to choose again => "; cin>>choice;
}while(choice=='y'||choice=='Y');
getch();
}
/* WAP in C++ which accepts an integer array and its size and assigns the elements into a twodimensional array of
integers in the following format: */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],b[20][20],len,i,j,k;
cout<<"Enter the length of the array";
cin>>len;
cout<<"Enter"<<len<<"elements";
for(i=0;i<len;i++)
cin>>a[i];
for( k=0;k<=len;k++)
for( j=0;j<len-k;j++)
b[k][j]=a[j];
cout<<endl<<"Array you entered is:";
for(i=0;i<len;i++)
cout<<a[i]<<" ";
cout<<endl<<"two dimensional array is:"<<endl;
for(i=0;i<len;i++)
{
for(j=0;j<len;j++)
cout<<b[i][j]<<" ";
cout<<endl;
}
getch();
}
/* Write menu driven program to show following operations in a 1-d array (using user defined function)1. Inserting an
element at ith position 2. Deleting an element from an array 3. Quit*/
#include<iostream.h>
#include<limits.h>
#include<process.h>
#include<conio.h>
/*--------------------------------------------------------------Function declaration -------------------------------------------------------*/
case 1: insert_element(A,n);
break;
case 2: delete_element(A,n);break;
case 3: exit(0); break;
default : cout<<"\n\nPlease enter desired keyword : ";
}
cout<<"\n\nWant to choose from menu again : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
getch();
} // end of main
n--;
}
}
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct student
{
int rollno;
float marks[3];
char name[25];
}
stu[5],temp;
void bubble(student []);
void main()
{
int i;
for(i=0;i<5;i++)
{
cout<<endl<<"Enter name of the student"<<i;
gets(stu[i].name);
cout<<"Enter marks for three subject";
for(int j=0;j<3;j++)
cin>>stu[i].marks[j];
cout<<endl<<"Enter the roll number of";puts(stu[i].name);
cin>>stu[i].rollno;
}
bubble(stu);
cout<<"SORTED DETAILS ARE";
for(i=0;i<5;i++)
{
cout<<endl<<"Name of the student"<<i;
puts(stu[i].name);
cout<<"marks for three subject";
for(int j=0;j<3;j++)
cout<<stu[i].marks[j];
cout<<endl<<"Enter the roll number of";puts(stu[i].name);
cout<<stu[i].rollno;
}
getch();
}
void bubble(student a[])
{
int j;
for(int i=0;i<5;i++)
{
for(j=0;j<4-i;j++)
{
if(a[j].rollno>a[j+1].rollno)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
OUTPUT
/* Create a array storing these details of 5 employees .print the array. Create a menu that displays the following options
1. Query
2. Addname
Option 1 displays the total salary of the employee after taking the employee number from the user .(basic+hra)
option 2 adds the second name of all employees at the end of first name. Print the new array also.*/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class employee
{
int empno;
char name[25];
float hra,basicsal;
public:
void get();
void display();
void query();
void addname();
int eno()
{
return empno;
}
}ob[5];
int i=0;
void employee::query()
{
cout<<"Total Salary of";
puts(name);cout<<"is"<<basicsal+hra<<endl;
}
void employee::addname()
{
char n[50];
cout<<"Enter Second Name of"; puts(name);
gets(n);
strcat(name,n);
cout<<"Changed Name of employee "<<empno<<" is";puts(name);
}
void employee::get()
{
cout<<"\tEMPLOYEE"<<++i<<endl;
cout<<"\Enter the employee no:"<<endl;
cin>>empno;
cout<<"Enter name:"<<endl;
gets(name);
cout<<"Enter basic salary"<<endl;
cin>>basicsal;
cout<<"Enter hra:"<<endl;
cin>>hra;
}
void main()
{
clrscr();
char ans;
for(int i=0;i<5;i++)
ob[i].get();
int ch;
do{
cout<<"MENU"<<endl;
cout<<"\n1:QUERY \n 2: ADDNAME";
cin>>ch;
int k=0,pos=0;
cout<<"Enter the employee number where you want to make changes";
cin>>k;
for(i=1;i<=5;i++)
if(ob[i].eno()==k)
pos=i;
switch(ch)
{
case 1: ob[pos].query();
break;
case 2: ob[pos].addname();
break;
default: cout<<"Wrong choice";
}
cout<<"Do you want to continue(Y/N)";
cin>>ans;
}
while(ans=='y'||ans=='Y');
getch();
}
/* Write functions which perform the following task on the string (without using standard library functions): A) string
length b) comparison of two strings c) concat of two strings*/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void findlength()
{
char str[30];
int l=0;
cout<<"\n Enter the string (size<=30) ";
gets(str);
while(str[l]!='\0')
{
l++;
}
cout<<"\n Length Of the given String is: "<<l<<endl;
}
void compare()
{
char str1[30], str2[30];
int l1=0,l2=0,i=0,flag=0;
cout<<"\n Enter the string1 (size<=30) ";
gets(str1);
while(str1[l1]!='\0')
{
l1++;
}
cout<<"\n Enter the string2 (size<=30) ";
gets(str2);
while(str2[l2]!='\0')
{
l2++;
}
if(l2!=l1)
{
cout<<"\n Strings are not Equal ";
}
else
{
for(i=0;i<l1;i++)
{
if(str1[i]!=str2[i])
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<"\n Strings are not Equal ";
}
else
{
cout<<"\n Strings are Equal ";
}
}
}
void concat()
{
char str1[30], str2[30];
int l1=0,l2=0,i=0,flag=0;
cout<<"\n Enter the string1 (size<=30) ";
gets(str1);
while(str1[l1]!='\0')
{
l1++;
}
cout<<"\n Enter the string2 (size<=30) ";
gets(str2);
while(str2[l2]!='\0')
{
l2++;
}
for(i=0;i<l2;i++)
{
str1[l1+i]=str2[i];
}
str1[l1+l2]='\0';
cout<<"\n The concatenated String is: ";
puts(str1);
}
void main()
{
clrscr();
char ch,ans;
do
{
cout<<"Enter your choice \n \t1.Find length of string\n\t"
"2.Compare two Strings \n\t3.Concatenate two strings\n\t4.Exit \n";
cin>>ch;
switch(ch)
{
case '1':
findlength();
break;
case '2':
compare();
break;
case '3':
concat();
break;
default:cout<<"Wrong choice";
}
cout<<"Do You want to continue"<<endl;
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}
OUTPUT
/* 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();
}
/* WAP using pointers to find the smallest/ 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];
}
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();
}
/*WAP that maintains information about books(name,no,price) using classes and store it in file. The program
should provide the user with the following options.A) add a record B) search record
C) modify record*/
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class book
{
char bname[30];
int bno;
float price;
public:
void input()
{
cout<<"\nEnter Book Name: ";
gets(bname);
cout<<"Enter BOOK No.: ";
cin>>bno;
cout<<"Enter Price";
cin>>price;
}
void setprice()
{
cout<<"\nEnter Price";
cin>>price;
}
void display()
{
cout<<"\nBook Name: "<<bname<<"\tBook No.: "<<bno<<"\tPrice: "<<price<<"\t";
}
int getbno()
{
return bno;
}
};
void main()
{
clrscr();
book b;
int n,i,j;
fstream ofile,afile;
char ch,ch1;
do
{
cout<<"\n\t1.Add records\n\t2.Search Records\n\t3.Modify Records\n\t4.Exit";
cin>>ch;
switch(ch)
{
case '1' :
ofile.open("bk.dat",ios::out|ios::binary);
case '2' :
case '3' :
cout<<"\nEnter Book No. to be modified ";
int bn1,flag1=0,r=0;
cin>>bn1;
afile.open("bk.dat",ios::in|ios::out|ios::binary);
while(afile)
{
afile.read((char *)&b,sizeof(book));
if(!afile)
break;
if (bn1==b.getbno())
{
b.setprice();
afile.seekp(r*sizeof(b),ios::beg);
afile.write((char *)&b,sizeof(book));
flag1=1;
break;
}
r++;
}
if(flag1==0)
cout<<"\n No record Found";
afile.close();
break;
cin>>ch1;
}while(ch1=='Y'||ch1=='y');
getch();
}
/* WAP to maintain a telephone directory using data file(binary) which provides the user with following
options a) append record b) search record*/
# include <fstream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
class telephone
{
char name[30];
char address[50];
double tno;
public :
void input()
{
cout<<"\n Enter the name ";
gets(name);
cout << "\n Enter address ";
gets(address);
cout<<"\n Enter the telephone number ";
cin>>tno;
}
void show()
{
cout << "\n Name "<<name;
cout << "\n Address "<<address;
}
double rt_tno()
{
return tno;
}
}tele;
// Function to append the records in file
void append()
{
ofstream tfile;
telephone tele;
tfile.open("tele.dat", ios :: app);
int n,i;
cout<< "Enter how many customers ";
cin>>n;
for (i =0; i<n ;i++)
{
tele.input();
tfile.write((char *)& tele,sizeof(tele));
}
tfile.close();
}
// Function to search a record in the file
void display()
{
ifstream tfile;
tfile.open("tele.dat",ios :: binary);
int no,flag;
flag = 0;
cout<< "\n Enter telephone number to be searched ";
cin>>no;
while(tfile)
{
tfile.read((char *)&tele , sizeof(tele));
if(!tfile)
break;
if (tele.rt_tno() == no)
{
tele.show();
flag = 1;
}
}
if (flag == 0)
cout<< "\n Record does not exist ";
}
void main()
{
clrscr();
int ch;
char ch1;
do
{
cout << "1. For append record ";
cout <<"\n2. For search ";
cout << "\n3. For exit";
cout<<"Enter your choice: ";
cin >> ch;
switch (ch)
{
case 1: append();
break;
case 2: display();
break;
case 3 : exit(0);
}
cout<<"\n\t DO U want to continue <Y/N>: ";
cin>>ch1;
}while(tolower(ch1)!='n');
getch();
}
/* WAP to maintain a record of sports(name,no,fees) in a file(binary) and provide the user with options to add,search and delete
record from file.*/
// Filename: \\PracticalList\P_16.CPP
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class Sports{
char sname[30];
int sno;
float fees;
public:
void input()
{
cout<<"\nEnter sports Name: ";
gets(sname);
cout<<"Enter sports No.: ";
cin>>sno;
cout<<"Enter Fees: ";
cin>>fees;
}
void display()
{
cout<<"\nSports Name: "<<sname<<"\tSports No.: "<<sno<<"\tFees: "<<fees<<"\t";
}
int getsno()
{
return sno;
}
};
void main()
{
clrscr();
Sports s;
int n,i,j;
fstream ofile,afile;
char ch,ch1;
do
{
cout<<"\n\t1.Add records\n\t2.Search Records\n\t3.Delete Records\n\t4.Exit\n";
cout << "Enter your choice... ";
cin>>ch;
switch(ch)
{
case '1' :
case '2' :
ofile.open("Sport.dat",ios::out|ios::binary);
cout<<"\nEnter no. of records to be Entered: ";
cin>>n;
for(i=0;i<n;i++)
{
s.input();
ofile.write((char*)&s,sizeof(Sports));
}
ofile.close();
break;
cout<<"\nEnter Sports No. to be searched: ";
int sn,flag=0;
cin>>sn;
afile.open("Sport.dat",ios::in);
while(afile)
{
afile.read((char *)&s,sizeof(Sports));
if(!afile)
break;
cout << s.getsno();
if (sn==s.getsno())
{
s.display();
flag=1;
break;
}
}
if(flag==0)
cout<<"\n No record Found";
afile.close();
break;
case '3' :
cout<<"\nEnter Sports No. to be Deleted ";
int sn1,flag1=0;
cin>>sn1;
afile.open("Sport.dat",ios::in|ios::binary);
ofile.open("TSport.dat",ios::out|ios::binary);
while(afile)
{
afile.read((char *)&s,sizeof(Sports));
if(!afile)
break;
if (sn1==s.getsno())
{
flag1=1;
}
else
{
ofile.write((char *)&s,sizeof(Sports));
}
}
if(flag1==0)
cout<<"\n No record Found";
afile.close();
ofile.close();
afile.open("TSport.dat",ios::in|ios::binary);
ofile.open("Sport.dat",ios::out|ios::binary);
while(afile)
{
afile.read((char *)&s,sizeof(Sports));
ofile.write((char *)&s,sizeof(Sports));
}
afile.close();
ofile.close();
break;
case '4' : exit(0);
}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
}while(ch1=='Y'||ch1=='y');
getch();
}
/* Given a binary file sports.dat containing records of the following structure type
STRUCT SPORTS { CHAR EVENT[20]; CHAR PARTICIPANT[10][30]; }
WAP to read this file and create another file called athletic.dat copying only those records where event name is athletic*/
#include<iostream>
#include<fstream>
#include<string.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
struct sports {
char event[20];
char participants[10][30];
int no_of_participants;
} s[20], s2[20];
void copy(fstream &ob);
int i=0;
int main()
{
char choice;
fstream ob("sports.dat",ios::binary|ios::in|ios::out);
do
{
if(i>0)
cin.get();
cout<<"\n\nEnter the name of Event : ";
cin.getline(s[i].event,20);
cout<<"\n\nEnter the total number of participants in Event "<<s[i].event<<" : ";
cin>>s[i].no_of_participants;
cout<<"\n\nEnter the name of Praticipants : \n";
cin.get();
for(int j=0; j<s[i].no_of_participants; j++)
cin.getline(s[i].participants[j],30);
ob.write((char*)&s[i], sizeof(sports));
cout<<"\n\n\nWant to Enter Details of Another Event (Y/N) : ";
cin>>choice;
i++;
}while(choice=='y'||choice=='Y');
cout<<"\n\n\n\n\n*********************************************************\n\n";
copy(ob);
cout<<"\n\n*****************************************************************************\n\n\n";
getch();
}
void copy(fstream &o)
{
sports s[20];
o.seekg(0);
ofstream file;
file.open("athletic.dat",ios::binary);
file.seekp(0);
int j;
int c=0;
while(o)
{
o.read((char*)&s[c], sizeof(sports));
if(strcmp("athletic",s[c].event)==0)
{
file.write((char*)&s[c], sizeof(sports));
break;
}
c++;
}
o.close();
file.close();
sports sp;
ifstream oo;
oo.open("athletic.dat",ios::binary);
while(oo)
{
oo.read((char*)&sp, sizeof(sports));
}
cout<<"\n\nThe Records of file are : \n\n";
cout<<"\n\nEvent = "<<sp.event;
cout<<"\n\n\n\nThe Participants are : \n\n";
for(int i=0; i<sp.no_of_participants; i++)
{
cout<<sp.participants[i]<<"\n\n";
}
oo.close();
}
/*WAP to perform the given operations on linked list.a)insertion b) deletion c)traversal d) search*/
#include<iostream>
#include<stdlib.h>
#include<process.h>
using namespace std;
struct node
{
int info;
node *next;
}*start,*newptr,*save,*ptr;
node *create(int);
void insert_beg(node*);
void insert_end(node*);
void display(node*);
void del_beg();
void del_end();
void del(int);
int search(int);
int main()
{
start=NULL;
int ele,mn;
char ch='n';
do
{
system("cls");
cout<<"1: Insert a node"<<endl;
cout<<"2: Delete a node"<<endl;
cout<<"3: Traverse Linked list"<<endl;
cout<<"4: Search Element"<<endl;
cout<<"5: Exit"<<endl;
cout<<"Enter your choice"<<endl;
cin>>mn;
switch(mn)
{
case 1: int m;
system("cls");
cout<<"-----Insertion-----"<<endl;
cout<<"1: Insertion At Beginning"<<endl;
cout<<"2: Insertion At End"<<endl;
cout<<"Enter your choice"<<endl;
cin>>m;
cout<<"Enter information to be inserted"<<endl;
cin>>ele;
newptr=create(ele);
switch(m)
{
case 1: node *e;
insert_beg(newptr);
display(start);
break;
case 2: insert_end(newptr);
display(start);
break;
default: cout<<"wrong choice";
break;
}
break;
case 2:int n;
system("cls");
cout<<"-----Deletion-----"<<endl;
cout<<"1: Deletion At Beginning"<<endl;
cout<<"2: Deletion At End"<<endl;
cout<<"3: Deletion of an element"<<endl;
cout<<"Enter your choice"<<endl;
cin>>n;
switch(n)
{
case 1:del_beg();
display(start);
break;
case 2: del_end();
display(start);
break;
case 3:system("cls");
cout<<"Enter element to be deleted";
cin>>ele;
del(ele);
display(start);
break;
default: cout<<"wrong choice";
break;
}
break;
case 3: display(start);
break;
case 4: system("cls");
int ele,pos;
cout<<"enter the element to be searched"<<endl;
cin>>ele;
pos=search(ele);
if(pos==-1)
cout<<"element not found"<<endl;
else
{
cout<<"element found at "<<pos<<endl;
}
break;
case 5: exit(0);
default: cout<<"Wrong choice";
break;
}
cout<<"Do you want to continue";
cin>>ch;
}while(ch=='y'||ch=='Y');
}
node * create(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void insert_beg(node *np)
{
if(start==NULL)
start=np;
else
{
save=start;
start=np;
np->next=save;
}
}
void display(node *np)
{
if(np==NULL)
{
cout<<"empty list"<<endl;
}
else{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
cout<<"\n";
}
}
void insert_end(node* n)
{
node *e;
if(start==NULL)
start=n;
else
{
e=start;
while(e->next!=NULL)
{
e=e->next;
}
e->next=n;
}
}
void del_beg()
{
if(start==NULL)
{
cout<<"No element is present in the list"<<endl;
}
else
{
ptr=start;
start=start->next;
delete ptr;
}
}
void del_end()
{
node *e;
if(start=NULL)
cout<<"No element is present in the list"<<endl;
else
{
ptr=start;
while((ptr->next)->next!=NULL)
{
ptr=ptr->next;
}
e=ptr->next;
ptr->next=NULL;
delete e;
}
}
void del(int er)
{
node *e,*f;
e=start;
if(e->info==er)
{
start=e->next;
delete e;
}
else
{
while(e->next->info!=er)
{
e=e->next;
}
f=e->next;
e->next=f->next;
delete f;
}
}
int search(int e)
{
ptr=start;
int counter=1;
while (ptr!=NULL && ptr->info!=e){
ptr = ptr->next;
counter++;
}
if (ptr != NULL && ptr->info==e) {
return counter;
}
}
{
top++;
stack[top]=ele;
}
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)
{
if(top==-1)
return;
cout<<stack[top]<<"<--"<<endl;
for(int i=top-1;i>=0;i--)
{
cout<<stack[i]<<endl;
}
}
return ptr;
}
void push(node *np)
{
if(top==NULL)
top=np;
else
{
save=top;
top=np;
np->next=save;
}
}
int pop()
{
int r=-1;
if(top==NULL)
cout<<"--UNDERFLOW--"<<endl;
else
{
ptr=top;
r=top->info;
top=top->next;
delete ptr;
}
return r;
}
void display(node *np)
{
if(np==NULL)
cout<<"EMPTY STACK"<<endl;
while(np!=NULL)
{
cout<<np->info<<"-->";
np=np->next;
}
cout<<"\n";
}
OUTPUT
}
else
{
rear++;
queue[rear]=ele;
}
return 0;
}
int dequeue(int queue[])
{
int ret;
if(front==-1)
return -1;
else
{
ret=queue[front];
if(front==rear)
front=rear=-1;
else
front++;
}
return ret;
}
void display(int queue[],int front,int rear)
{
if(front==-1)
return;
for(int i=front;i<rear;i++)
{
cout<<queue[i]<<"<-";
}
cout<<queue[rear]<<endl;
}
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void enqueue(node *np)
{
if(front==NULL)
{
front=rear=np;
}
else
{
rear->next=np;
rear=np;
}
}
int dequeue()
{
int r=-1;
if(front==NULL)
return r;
else
{
ptr=front;
front=front->next;
r=ptr->info;
delete ptr;
}
return r;
}
void display(node *np)
{
while(np!=NULL)
{
cout<<np->info<<"-->";
np=np->next;
}
}
return -1;
else if(rear==-1)
front=rear=0;
else if (rear==size-1)
rear=0;
else
rear++;
queue[rear]=e;
return 0;
}
int dequeue(int queue[])
{
int ret;
if(front==-1)
return -1;
else
{
ret=queue[front];
if(front==rear)
rear=front=-1;
else if(front==size-1)
front=0;
else
front++;
}
return ret;
}
void display(int queue[],int front,int rear)
{
int i=0;
if(front==-1)
cout<<"empty queue"<<endl;
else if(rear>=front)
{
for(i=0;i<front;i++)
cout<<"-";
cout<<">>>";
for(i=front;i<rear;i++)
cout<<queue[i]<<"<-";
cout<<queue[rear]<<"<<<"<<endl;
}
else
{
for(i=0;i<rear;i++)
cout<<queue[i]<<"<-";
cout<<queue[rear]<<"<<<";
for(;i<front;i++)
cout<<"-";
cout<<">>>";
for(i=front;i<size;i++)
cout<<queue[i]<<"<-";
cout<<"circular";
}
}
Output