Sie sind auf Seite 1von 16

Program 9

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void rcSum(int [][50],int);
void diagSum(int [][50],int);
void addsub(int [][50],int[][50],int,int);
void trans(int [][50],int);
int i,j;
void main()
{
clrscr();
int a[50][50], s, ch;
int b[50][50];
char cont='n';
cout << "\nEnter size of matrix : ";
cin >> s;
for ( i=0;i<s;i++)
for ( j=0;j<s;j++)
{
cout << "Enter element at position (" << i << ',' << j << ") : ";
cin >> a[i][j];
}
do{
cout << "\n\nWhat operation do u want to do ";
cout << "\n1 : Row and column sum";
cout << "\n2 : Sum of diagonals";
cout << "\n3 : Add";
cout << "\n4 : Subtract";
cout << "\n5 : Transpose";
cout << "\nEnter choice : ";
cin >> ch;
cout << endl << endl;
switch (ch)
{
case 1:
rcSum(a,s);
break;
case 2:
diagSum(a,s);
break;
case 3:
cout << "Second matrix\n";
for (i=0;i<s;i++)
for ( j=0;j<s;j++)
{
cout <<"Enter no at position"<< i <<','<< j<< ": ";
cin >> b[i][j];
}
addsub(a,b,s,1);
break;

case 4:
int c[50][50];
cout << "Second matrix\n";
for (i=0;i<s;i++)
for (j=0;j<s;j++)
{
cout << "Enter no at position (" << i << ',' << j << ") : ";
cin >> c[i][j];
}
addsub(a,c,s,-1);
break;
case 5:
trans(a,s);
break;
default:
cout << "wrong choice";
}
cout << "\n\nDo you want to continue? (y/n) : ";
cont=getche();
}while (cont=='y'||cont=='Y');
}
void rcSum(int a[][50],int s)
{
int rowS[50], colS[50];
for ( i=0;i<50;i++)
{
rowS[i]=0;
colS[i]=0;
}
for (i=0;i<s;i++)
for ( j=0;j<s;j++)
{
colS[j]+=a[i][j];
rowS[i]+=a[i][j];
}
for (i=0;i<s;i++)
{
for (j=0;j<s;j++)
{
cout << setw(5) << a[i][j];
}
cout << setw(5) << rowS[i];
cout << endl;
}
for (i=0;i<s;i++)
{
cout << setw(5) << colS[i];
}
}
void diagSum(int a[][50], int s)
{
int lrds=0, rlds=0;
for (i=0;i<s;i++)
{

for (j=0;j<s;j++)
{
if (i==j)
{
cout << setw(5) << a[i][j];
lrds+=a[i][j];
}
else
cout << setw(5) << " ";
}
cout << endl;
}
cout << "\nLeft-to-right diagonal sum :" << lrds << endl;
for (i=0;i<s;i++)
{
for (j=0;j<s;j++)
{
if (i+j==s-1)
{
cout << setw(5) << a[i][j];
rlds+=a[i][j];
}
else
cout << setw(5) <<" ";
}
cout << endl;
}
cout << "\nRight-to-left diagonal sum : " << rlds<<endl;
}
void addsub(int a[][50],int b[][50],int s,int m)
{
int c[50][50];
for (i=0;i<s;i++)
for ( j=0;j<s;j++)
{
if (m==1)
c[i][j]=a[i][j]+b[i][j];
else if (m==-1)
c[i][j]=a[i][j]-b[i][j];
}
cout << endl;
for (i=0;i<s;i++)
{
for (j=0;j<s;j++)
cout << setw(5) << c[i][j];
cout << endl;
}
}
void trans(int a[][50],int s)
{
int b[50][50];
for ( i=0;i<s;i++)
for (j=0;j<s;j++)
if (i<j)

{
b[i][j]=a[j][i];
b[j][i]=a[i][j];
}
else if (i==j)
b[i][j]=a[i][j];
for (i=0;i<s;i++)
{
for (j=0;j<s;j++)
cout << setw(5) << b[i][j];
cout << endl;
}
}

Program10
#include <iostream.h>
#include <conio.h>
void bSort(int[],int);
void ins(int[],int &,int,int);
void del(int[],int &,int);
int bSearch(int[],int,int);
void merge(int[],int,int[],int,int[]);
void out(int[],int);
void main()
{
clrscr();

char cont='n';
int a[25],s,ch;
cout << "\nEnter size (1-25) : ";
cin >> s;
for (int i=0;i<s;i++)
{
cout << "Enter element #" << i+1 << " : ";
cin >> a[i];
}
do{
cout << "\n\nMenu";
cout << "\n1 : Bubble sort";
cout << "\n2 : Insert value at position";
cout << "\n3 : Delete value at position";
cout << "\n4 : Binary search";
cout << "\n5 : Merge";
cout << "\nEnter choice : ";
cin >> ch;
cout << "\n\n";
switch (ch)
{
case 1:
bSort(a,s);
cout << "Bubble sorting complete";
out(a,s);
break;
case 2:
int iVal,iPos;
if (s==25)
{
cout << "Error : Array full. Unable to insert values";
break;
}
cout << "Enter value to be inserted : ";
cin >> iVal;
cout << "Enter position of insertion : ";
cin >> iPos;
while (iPos>25)
{
cout << "\nError : Insertion position must be less than 26";
cout << "\nEnter position of insertion : ";
cin >> iPos;
}
if (iPos>s)
{
cout << "\nWarning : Insertion position exceeds given array size.";
cout << "\nValue will be inserted at end of array";
}
ins(a,s,iVal,iPos);
cout << "\nInsertion successful";
out(a,s);
break;
case 3:

int dPos;
cout << "Enter position of deletion : ";
cin >> dPos;
while (dPos>s)
{
cout << "\nError : Deletion position cannot be bigger than size";
cout << "\nEnter position of deletion : ";
cin >> dPos;
}
del(a,s,dPos);
cout << "\nDeletion successful";
out(a,s);
break;
case 4:
int bSVal,bSPos;
cout << "Enter search term : ";
cin >> bSVal;
cout << "\nResorting array";
bSort(a,s);
out(a,s);
bSPos=bSearch(a,s,bSVal);
cout << "\nBinary search complete";
if (bSPos>-1)
cout << "\nTerm found at position " << bSPos+1;
else
cout << "\nTerm not found";
break;
case 5:
int b[25],c[25],s2,s3;
cout << "Enter size of second array : ";
cin >> s2;
while (s+s2>25)
{
cout << "\nError : Resultant array must be less than 26";
cout << "\nEnter size of second array : ";
cin >> s2;
}
for (i=0;i<s2;i++)
{
cout << "Enter element #" << i+1 << " : ";
cin >> b[i];
}
merge(a,s,b,s2,c);
cout << "\nSorting first array";
cout << "\nSorting second array";
cout << "\nMerge successful";
s3=s+s2;
out(c,s3);
break;
default:
cout << "Error : Incorrect choice selected";
}
cout << "\nDo you want to continue? (y/n) : ";
cont=getche();

}while(cont=='y'||cont=='Y');
}
void bSort(int a[],int s)
{
int t;
for (int i=0;i<s;i++)
for (int j=0;j<(s-i-1);j++)
if (a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
void ins(int a[],int &s,int iVal,int iPos)
{
bSort(a,s);
if (iPos>s+1)
{
iPos=s+1;
}
for (int i=s-1;i>=iPos-1;i--)
a[i+1]=a[i];
a[iPos-1]=iVal;
s++;
}
void del(int a[],int &s,int dPos)
{
for (int i=dPos-1;i<s-1;i++)
a[i]=a[i+1];
s--;
}
int bSearch(int a[],int s,int val)
{
int f=0,l=s-1,m;
char flag='n';
while (f<=l&&flag=='n')
{
m=(f+l)/2;
if (a[m]==val)
{
flag='y';
break;
}
else if (a[m]>val)
l=m-1;
else if (a[m]<val)
f=m+1;
}
if (flag=='n')
m=-1;
return m;
}
void merge(int a[],int sa,int b[],int sb,int c[])

{
bSort(a,sa);
bSort(b,sb);
int i=0,j=0,k=0;
while (i<sa&&j<sb)
{
if (a[i]==b[j])
{
c[k++]=a[i++];
c[k++]=b[j++];
}
else if (a[i]<b[j])
c[k++]=a[i++];
else if (a[i]>b[j])
c[k++]=b[j++];
}
while (i<sa)
c[k++]=a[i++];
while (j<sb)
c[k++]=b[j++];
}
void out(int a[],int s)
{
cout << endl;
for (int i=0;i<s;i++)
cout << a[i] << endl;
}

Program 11
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class floppybox
{
int id;
char name[40];
int size;
public:
void getd()
{
cout<<"enter name, size, id\n";
cin>>name;
cin>>size;
cin>>id;
}
void showd()
{
cout<<"\nname - "<<name;
cout<<"\nsize ="<<size;
cout<<"\nid ="<<id;

}
int retid()
{
return id;
}
char* retstr()
{
return name;
}
};
char ans,ans2,str[40];
int s,n;
fstream f1;
floppybox fb;
void create()
{
f1.open("file.dat",ios::out|ios::binary);
do{
fb.getd();
f1.write((char*)&fb,sizeof(fb));
cout<<"do u want to continue\n";
cin>>ans;
}while(ans=='Y'||ans=='y');
f1.close();
}
void read()
{
f1.open("file.dat",ios::in|ios::binary);
while(f1.read((char*)&fb,sizeof(fb)))
{
fb.showd();
}
f1.close();
}
int total()
{
int c=0;
f1.open("file.dat",ios::in|ios::binary);
while(f1.read((char*)&fb,sizeof(fb)))
{
c++;
}
f1.close();
return c;
}
void flopid(int n)
{
f1.open("file.dat",ios::in|ios::binary);
while(f1.read((char*)&fb,sizeof(fb)))
{
if(n==fb.retid())
fb.showd();
}
f1.close();
}
void flopnm(char str[40])

{
f1.open("file.dat",ios::in|ios::binary);
while(f1.read((char*)&fb,sizeof(fb)))
{
if(strcmpi(str,fb.retstr())==0)
fb.showd();
}
f1.close();
}
void main()
{
clrscr();
int choice;
do
{ create();
cout<<"\nwhat do u want to do\n1.read the file\n2.find total number of records\n3.search and
display a parricular record\n";
cin>>choice;
switch(choice)
{
case 1:read();
break;
case 2:cout<<"\ntotal number of records are "<<total();
break;
case 3: int s;
cout<<"\non what basis do u want to search 1.id OR 2.name\n";
cin>>s;
if(s==1)
{
cout<<" enter search id\n";
cin>>n;
flopid(n);
}
else if(s==2)
{
cout<<"enter search name\n";
cin>>str;
flopnm(str);
}
break;
}//switch
cout<<"\ndo u want to continue y or n\n";
cin>>ans2;
}while(ans2=='y'||ans2=='Y');
getch();
}

Program12
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class book
{
int id;
char name[20];
int num;
public:
void getd()
{
cout<<"enter name, number of pages, id\n";
cin>>name;
cin>>num;
cin>>id;
}
void showd()
{
cout<<"\nname - "<<name;
cout<<"\nnumber of pages ="<<num;
cout<<"\nid ="<<id;
}
};
book b1,b2;
fstream f1,f2;
char ans;
void create()
{
f1.open("file.dat",ios::out|ios::binary);
do{
b1.getd();

f1.write((char*)&b1,sizeof(b1));
cout<<"do u want to continue\n";
cin>>ans;
}while(ans=='Y'||ans=='y');
f1.close();
}
void read()
{
f1.open("file.dat",ios::in|ios::binary);
while(f1.read((char*)&b1,sizeof(b1)))
{
b1.showd();
}
f1.close();
}
void edit()
{
int n1;
cout<<"enter record number to edit\n";
cin>>n1;
cout<<"enter the new details\n";
b1.getd();
f1.open("file.dat",ios::ate|ios::out|ios::binary);
f1.seekp((n1-1)*sizeof(b1),ios::beg);
f1.write((char*)&b1,sizeof(b1));
f1.close();
read();
}
void insert()
{
int n2,r=0;
cout<<"enter the position where u want to insert record\n";
cin>>n2;
cout<<"enter new record";
b2.getd();
f1.open("file.dat",ios::in|ios::binary);
f2.open("new.dat",ios::out|ios::binary);
f1.seekg(0);
while(f1.read((char*)&b1,sizeof(b1)))
{
r++;
if(r==n2)
f2.write((char*)&b2,sizeof(b2));
f2.write((char*)&b1,sizeof(b1));
}
f2.close();
f1.close();
remove("file.dat");
rename("new.dat","file.dat");
read();
}
void insertbkn()
{
int bno;
cout<<"enter the booknumber";

cin>>bno;
int no=0;
f1.open("file.dat",ios::in|ios::binary);
f2.open("new.dat",ios::out|ios::binary);
f1.seekg(0);
while(f1.read((char*)&b1,sizeof(b1)))
{
no++;
if(bno==no+1)
{
cout<<"enter new record to insert\n";
b2.getd();
f2.write((char*)&b2,sizeof(b2));
}
f2.write((char*)&b1,sizeof(b1));
}
f2.close();
f1.close();
remove("file.dat");
rename("new.dat","file.dat");
read();
}
void main()
{
clrscr();
int choice;
create();
do{
cout<<"\nwhat do u want to do\n1.read the file\n2.edit a record\n3.insert a record at nth
position\n4.insert a record after a book number\n";
cin>>choice;
switch(choice)
{
case 1: read();
break;
case 2: edit();
break;
case 3: insert();
break;
case 4: insertbkn();
break;
default : cout<<"\nwrong choice\n";
}
cout<<"\ndo u want to continue y or n\n";
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}

Program 13
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class employee
{
int eno;
char ename[40];
double salary;
public:
void getd()
{
cout<<"enter eno, name, salary of employee\n";
cin>>eno;
gets(ename);
cin>>salary;
}
void showd()
{
cout<<"name - "<<ename;
cout<<"\nemployee nnumber - "<<eno;
cout<<"\nsalary = "<<salary;
}
};
employee e1,e2;
fstream f1,f2;
char ans,ans2;
void create()
{
f1.open("file.dat",ios::out|ios::binary);
do{
e1.getd();
f1.write((char*)&e1,sizeof(e1));
cout<<"do u want to continue\n";
cin>>ans;
}while(ans=='Y'||ans=='y');
f1.close();
}
void read()
{
f1.open("file.dat",ios::in|ios::binary);

while(f1.read((char*)&e1,sizeof(e1)))
{
e1.showd();
}
f1.close();
}
int count()
{
int c=0;
f1.open("file.dat",ios::in|ios::binary);
while(f1.read((char*)&e1,sizeof(e1)))
{
c++;
}
f1.close();
return c;
}
void del()
{
int n,r=0;
cout<<"enter the record number u want to delete\n";
cin>>n;
f1.open("file.dat",ios::in|ios::binary);
f2.open("new.dat",ios::out|ios::binary);
f1.seekg(0);
while(f1.read((char*)&e1,sizeof(e1)))
{
r++;
if(r!=n)
f2.write((char*)&e1,sizeof(e1));
}
f2.close();
f1.close();
remove("file.dat");
rename("new.dat","file.dat");
read();
}
void dellast()
{
f1.open("file.dat",ios::in|ios::binary);
f2.open("new.dat",ios::out|ios::binary);
f1.seekg(0,ios::end);
int last=f1.tellg();
int s=sizeof(e1);
int rec=last/s;
int r=0;
cout<<"\ntotal number of records are "<<rec;
f1.seekg(0);
while(f1.read((char*)&e1,sizeof(e1)))
{
r++;
if(r!=rec)
f2.write((char*)&e1,sizeof(e1));
}
f2.close();

f1.close();
remove("file.dat");
rename("new.dat","file.dat");
read();
}
void main()
{
clrscr();
int choice;
create();
do{
cout<<"what do u want to do\n1.read the file\n2.count total number of records\n3.delete
record at nth position\n4.delete last record\n";
cin>>choice;
switch(choice)
{
case 1: read();
break;
case 2: cout<<"\ntotal number of records are = "<<count();
break;
case 3: del();
break;
case 4: dellast();
break;
default : cout<<"\nwrong choice\n";
}
cout<<"\ndo u want to continue y or n\n";
cin>>ans2;
}while(ans2=='y'||ans2=='Y');
getch();
}

Das könnte Ihnen auch gefallen