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

39.

PROGRAM TO INSERT DATA IN A STORED FILE

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class stu { int roll;
char name[25];
char clas[4];
float marks;
char grade;
public:
void getdata()
{
cout<<"Roll No. : ";cin>>roll;
cout<<"Name : ";cin>>name;
cout<<"Class : ";cin>>clas;
cout<<"Marks : ";cin>>marks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='F';
}
void putdata()
{
cout<<"Roll no.: "<<roll<<" Name:
"<<name<<" Marks: "<<marks<<" Grade: "<<endl;
}
int getrno()
{
return roll;
}
}s1,stud;
void main()
{
clrscr();
ifstream fi("stu.dat",ios::in|ios::binary);
ofstream fo("temp.dat",ios::out|ios::binary);
char last ='y';
cout<<"Enter Deatils of Student whose Record is to be
Inserted :\n";
s1.getdata();
while(!fi.eof())
{
fi.read((char*)&stud,sizeof(stud));
if(s1.getrno()<=stud.getrno())
{
fo.write((char*)&s1,sizeof(s1));
last='n';
break;
}
else
fo.write((char*)&stud,sizeof(stud));
}
if(last=='y')
fo.write((char*)&s1,sizeof(s1));
else if(!fi.eof())
{
while(!fi.eof())
{
fi.read((char*)&stud,sizeof(stud));
fo.write((char*)&stud,sizeof(stud));
}
}
fi.close();
fo.close();
remove("stu.dat");
rename("temp.dat","stu.dat");
fi.open("stu.dat",ios::in);
cout<<"File Now Contains : \n";
while(!fi.eof())
{
fi.read((char*)&stud,sizeof(stud));
if(fi.eof())break;
stud.putdata();
}
fi.close();
getch();
}

40. PROGRAM TO DELETE RECORD FROM FILE

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class stu { int roll;
char name[25];
char clas[4];
float marks;
char grade;
public:
void getdata()
{
cout<<"Roll No. : ";cin>>roll;
cout<<"Name : ";cin>>name;
cout<<"Class : ";cin>>clas;
cout<<"Marks : ";cin>>marks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='F';
}
void putdata()
{
cout<<"Roll no.: "<<roll<<" Name:
"<<name<<" Marks: "<<marks<<" Grade: "<<endl;
}
int getrno()
{
return roll;
}
}s1,stud;
void main()
{
clrscr();
ifstream fio("stu.dat",ios::in);
ofstream file("temp.dat",ios::out);
int rno; char found='f',confirm='n';
cout<<"Enter Deatils of Student whose Record is to
be :\n";
cin>>rno;
while(!fio.eof())
{
fio.read((char*)&s1,sizeof(s1));
if(s1.getrno()==rno)
{
s1.putdata();
found='t';
cout<<"Are you confirm: (y/n): \n";
cin>>confirm;
if(confirm=='n')
file.write((char*)&s1,sizeof(s1));
}
else
file.write((char*)&s1,sizeof(s1));
}
if(found=='f')
cout<<"Record not found!!!";
fio.close();
file.close();
remove("stu.dat");
rename("temp.dat","stu.dat");
fio.open("stu.dat",ios::in);
cout<<"File Now Contains : \n";
while(!fio.eof())
{
fio.read((char*)&stud,sizeof(stud));
if(fio.eof())break;
stud.putdata();
}
fio.close();
getch();
}
41. PROGRAM TO MODIFY DATA IN A GIVEN FILE

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
class stu { int roll;
char name[25];
char clas[4];
float marks;
char grade;
public:
void getdata()
{
cout<<"Roll No. : ";cin>>roll;
cout<<"Name : ";cin>>name;
cout<<"Class : ";cin>>clas;
cout<<"Marks : ";cin>>marks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='F';
}
void putdata()
{
cout<<"Roll no.: "<<roll<<" Name:
"<<name<<" Marks: "<<marks<<" Grade: "<<endl;
}
int getrno()
{
return roll;
}
void modify();
}s1,stud;
void stu::modify()
{
cout<<"rollno :"<<roll<<endl;
cout<<"name :"<<name<<" class: "<<clas<<"
marks"<<endl;
cout<<"Enter New Details :";
char nm[20]=" ",cl[4]=" ";
float mks;
cout<<"New Name :(Enter . to retain old one)";
cin>>nm;
cout<<"New class:(enter . to retain old one)";
cin>>cl;
cout<<"New marks:(enter-1 to retain old one)";
cin>>mks;
if(strcmp(nm,".")!=0)
strcpy(name,nm);
if(strcmp(cl,".")!=0)
strcpy(clas,cl);
if(mks!=-1)
{
marks=mks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='F';
}
}
void main()
{
clrscr();
fstream fio("stu.dat",ios::in|ios::out|ios::binary);
int rno;long pos;char found='f';
cout<<"\nEnter roll no of the student whose record is to
be modified :";
cin>>rno;
while(!fio.eof())
{
pos=fio.tellg();
fio.read((char*)&s1,sizeof(s1));
if(s1.getrno()==rno)
{
s1.modify();
fio.seekg(pos);
fio.write((char*)&s1,sizeof(s1));
found='t';
break;
}
}
if(found=='f')
cout<<"Record not found !!!!!";
fio.seekg(0);
cout<<"\nNow the file contains : ";
while(!fio.eof())
{
fio.read((char*)&sud,sizeof(stud));
stud.putdata();
}
fio.close();
getch();
}

42. PROGRAM TO READ A 2D ARRAY USING


POINTER ,CALCULATE ITS ROW-SUM AND COLUMN-
SUM AND DISPLAY THIS ARRY ALONGWITH ROW-
SUM AND COLUMN-SUM

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *val,*rsum,*csum;
int maxr,maxc,i,j;
cout<<"Enter dimensions (row column) : \n";
cin>>maxr>>maxc;
val=new int[maxr*maxc];
rsum=new int[maxr];
csum=new int[maxc];
for(i=0;i<maxr;i++)
{
cout<<"\nEnter elements of row "<<i+1<<":";
rsum[i]=0;
for(j=0;j<maxc;j++)
{
cin>>val[i*maxc+j];
rsum[i]+=val[i*maxc+j];
}
}
for(j=0;j<maxc;j++)
{
csum[j]=0;
for(i=0;i<maxr;i++)
{
csum[j]+=val[i*maxc+j];
}
}
cout<<"\nTe given array alongwith the rowsum and
column sum is:\n";
for(i=0;i<maxr;i++)
{
for(j=0;j<maxc;j++)
{
cout<<val[i*maxc+j]<<"\t";
}
cout<<rsum[i]<<endl;
}
for(j=0;j<maxc;j++)
{
cout<<csum[j]<<"\t";
}
cout<<endl;
getch();
}

43. PROGRAM TO EXCHANGE POSITIONS OF


STRINGS STORED IN ARRAY USING ARRAY OF
POINTERS

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *names[]={"Sachin","Kapil","Ajay","Sunil","Anil"};
int len=0;
len=strlen(names[1]);
cout<<"Originally string 2 is :
";cout.write(names[1],len).put('\n');
cout<<"and string 4 is :
";cout.write(names[3],len).put('\n');
char *t;
t=names[1];
names[1]=names[3];
names[3]=t;
len=strlen(names[1]);
cout<<"Exchanged string 2 is :
";cout.write(names[1],len).put('\n');
cout<<"and string 4 is :
";cout.write(names[3],len).put('\n');
getch();
}

44. PROGRAM TO DISPLAY DETAILS OF THE


SALESMAN WHO HAS MADE THE MAXIMUM TOTAL
SALES.

#include<iostream.h>
#include<string.h>
#include<conio.h>
class salesman { char name[11];
float q1sal,q2sal,q3sal,q4sal,totsal;
public:
salesman()
{
strcpy(name," ");
q1sal=q2sal=q3sal=q4sal=totsal=0;
}
void getdata(char *s,float i,float j,float k, float
l)
{
strcpy(name,s);
q1sal=i;q2sal=j;q3sal=k;q4sal=l;
}
void calctot()
{
totsal=q1sal+q2sal+q3sal+q4sal;
}
char *getname() {return name;}
float getq1() {return q1sal;}
float getq2() {return q2sal;}
float getq3() {return q3sal;}
float getq4() {return q4sal;}
float gettot() {return totsal;}
salesman *maxsal(salesman *s)
{
if(!s)s=this;
else
{
float f1,f2;
f1=s->gettot();
f2=this->gettot();
if(f1<f2)
s=this;
}
return s;
}
};
salesman *sp;
void printit(salesman *sp)
{
cout<<"\nSalesman with maximum sales\n";
cout<<"Name :";
char *ss=sp->getname();
cout.write(ss,11).put('\n');
cout<<"Total Sales :"<<sp->gettot()<<"\n";
}
void main()
{
clrscr();
salesman raman,sita,vedant,anubhav,bina;
float q1,q2,q3,q4;
sp=&raman;
cout<<"Enter sales in four quarters for raman :\n";
cin>>q1>>q2>>q3>>q4;
raman.getdata("raman",q1,q2,q3,q4);
raman.calctot();
sp=raman.maxsal(sp);
cout<<"Enter sales in four quarters for sita :\n";
cin>>q1>>q2>>q3>>q4;
sita.getdata("sita",q1,q2,q3,q4);
sita.calctot();
sp=sita.maxsal(sp);
cout<<"Enter sales in four quarters for vedant :\n";
cin>>q1>>q2>>q3>>q4;
vedant.getdata("vedant",q1,q2,q3,q4);
vedant.calctot();
sp=vedant.maxsal(sp);
cout<<"Enter sales in four quarters for anubhav :\n";
cin>>q1>>q2>>q3>>q4;
anubhav.getdata("anubhav",q1,q2,q3,q4);
anubhav.calctot();
sp=anubhav.maxsal(sp);
cout<<"Enter sales in four quarters for bina :\n";
cin>>q1>>q2>>q3>>q4;
bina.getdata("bina",q1,q2,q3,q4);
bina.calctot();
sp=bina.maxsal(sp);
printit(sp);
getch();
}
45. BINARY SEARCH IN ARRAY

#include<iostream.h>
#include<conio.h>
int bsearch(int[],int,int);
void main()
{
clrscr();
int ar[50],item,n,index;
cout<<"Enter desired array size (max. 50)...";
cin>>n;
cout<<"\nEnter array elements(must be sirted in asc
order)\n";
for(int i=0;i<n;i++)
cin>>ar[i];
cout<<"Enter element to be searched for : ";
cin>>item;
index=bsearch(ar,n,item);
if(index==-1)
cout<<"\nSorry Given element could not be found!!!";
else
cout<<"\nElement found at index :
"<<index<<",position:"<<index+1<<endl;
getch();
}
int bsearch(int ar[],int size,int item)
{
int beg,last,mid;
beg=0;last=size-1;
while(beg<=last)
{
mid=(beg+last)/2 ;
if(item==ar[mid])
return mid;
else if(item>ar[mid])
beg=mid+1;
else
last=mid-1;
}
return -1;
}
46. DELETION IN ARRAY

#include<iostream.h>
#include<conio.h>
#include<process.h>
int lsearch(int[],int,int);
void main()
{
clrscr();
int ar[50],item,n,index;
cout<<"Enter desired array size (max. 50)...";
cin>>n;
cout<<"\nEnter array elements(must be sorted in asc
order)\n";
for(int i=0;i<n;i++)
cin>>ar[i];
char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\nEnter element to be deleted : \n";
cin>>item;
if(n==0)
{
cout<<"Underflow!!!\n";
exit(1);
}
index=lsearch(ar,n,item);
if(index!=-1)
ar[index]=0;
else
cout<<"\nNo such element in array!!!!";
cout<<"\nThe array now is as shown below :\n";
cout<<"Zero(0) signifies deleted element.\n";
for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
cout<<"After this the emptied space will be shifted to
the end of array.";
for(i=index;i<n;i++)
{
ar[i]=ar[i+1];
}
n-=1;
cout<<"\nWant to delete more elements (y/n):";
cin>>ch;
}
cout<<"\nTHE FINAL ARRAY IS :\n";
for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
getch();
}
int lsearch(int ar[],int size,int item)
{
for(int i=0;i<size;i++)
{
if(ar[i]==item)
return i;
}
return -1;
}

47. WRITE A MENU DRIVEN PROGRAM IN C++ TO


DO THE FOLLOWING IN A MATRIX:

i) SUM OF BOTH DIAGONALS.


ii) DISPLAY UPPER HALF OF MATRIX.
iii) DISPLAY LOWER HALF OF MATRIX.
iv) TRANSPOSE OF MATRIX.

#include<iostream.h>
#include<conio.h>
#include<process.h>

void main()
{
clrscr();
int i, j, a[3][3], choice;
char ch='Y';
for(i=0; i<3; i++)
{ cout<<"Enter row "<<i+1<<" details : \n";
for(j=0; j<3; j++)
{
cout<<"Column "<<j+1<<" : ";
cin>>a[i][j];
}
}
while(ch=='y' || ch=='Y')
{
cout<<"\n\nMENU : \n";
cout<<"1. Sum of both diagonals\n";
cout<<"2. Display upper half of matrix\n";
cout<<"3. Display lower half of matrix\n";
cout<<"4. Transpose of matrix\n";
cout<<"5. Exit\n\n";
cout<<"Enter choice : ";
cin>>choice;

switch(choice)
{
case 1 : int sum=0;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
if(i==j || i+j==2)
sum+=a[i][j];
}
}
cout<<"Sum is "<<sum;
break;

case 2 : for(i=0; i<3; i++)


{
for(j=0; j<3; j++)
{
if(j>=i)
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
break;

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


{
for(j=0; j<3; j++)
{
if(j<=i)
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
break;

case 4 : for(j=0; j<3; j++)


{
for(i=0; i<3; i++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
break;

case 5 : exit(0);
}
cout<<"\n\nDo you want to continue(Y/N) : ";
cin>>ch;
}
getch();
}

You might also like