1. PROGRAM TO INSERT AN ELEMENT IN AN ARRAY
Program to insert an element in a sorted array at an appropriate position .
#include<iostream.h>
void insertar(int a[ ], int num, int val) //n is
the number of elements in the array
{ int i=num-1;
while (i>=0 && a[i]>val)
{
a[i+1]=a[i]; // shift the element one position towards right
i--;
}
a[i+1]=val; //insertion of item at appropriate place
}
void main()
{ int a[20]={1,2,3,5,7,12,15,19,24},count=9;
int i=0;
cout<<“Original array is:\n”;
for(i=0;i<count;i++)
cout<<a[i]<<”, “;
insertar(a,count++,
10);
cout<<”\n Array after inserting 10 is:\n”;
for(i=0;
i<count; i++)
cout<<a[i]<<”, “;
}
2. Program to delete an integer element at position k in an
array having n elements.
#include<iostream.h>
void deletar(int a[ ],
int n, int item) //n is the number of elements already present in the array
{int i=0;
while(i<n
&& a[i]<item)
i++;
if
(a[i]==item) // given item is found
{while (i<n)
{a[i]=a[i+1]; // shift the
(i+1)th element one position towards left
i++;
}
cout<<”\n Given item
is successfully deleted”;
}
else
cout<<”\n Given item is
not found in the array”;
n--;
}
void main()
{int
a[10]={2,4,5,7,8,11,12,15},n=8;
int i=0;
cout<<“Original array
is :\n”;
for(i=0;i<n;i++)
cout<<a[i]<<”,
“;
delete_item(a,n,11);
cout<<”\nArray after
deleting 11 is:\n”;
for(i=0; i<n; i++)
cout<<a[i]<<”, “;
}
3. Program to traverse all the elements of an array
#include<iostream.h>
void main()
{ int ar[20],i,n;
cout<<”\n enter the number of elements
of the array :” ;
cin>>n; //number
of elements in the array
if((n<=20) //
checking the length of array should not exceed its maximum capacity
{
cout<<”\n enter the elements of the array : ”;
for(i=0;i<n;i++) // loop to accept n
elements of the array
cin>>ar[i];
cin>>ar[i];
cout<<”\n elements of the array
are :”;
for(i=0;i<n;i++)
cout<<endl<<ar[i];
}
else
cout<<”\n
array may have upto 20 elements”;
}
4. PROGRAM TO ADD TWO MATRICES
#include<iostream.h>
#include<conio.h>
void main()
{
int a[2][3],b[2][3],c[2][3],d,e,f;
cout<< "First matrix.\n";
for (d=1;d<=2;d++)
{
for
(e=1;e<=3;e++)
{ cout<<
"Enter the number: ";
cin>>
a[d][e]; }
}
cout<< "\n
Second matrix.\n";
for (d=1;d<=2;d++)
{
for
(e=1;e<=3;e++)
{ cout<<
"Enter the number: ";
cin>>
b[d][e]; }
}
cout<< "\nSum of matrices : \n";
for (d=1;d<=2;d++)
{
for
(e=1;e<=3;e++)
{
cout<< a[d][e]+b[d][e]
<< '\t';
}
cout<< endl;
}
getch();
}
5. PROGRAM TO FIND DIFFERENCE OF TWO MATRICES.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[2][3],b[2][3],c[2][3],d,e,f;
cout<< "First matrix.\n";
for (d=1;d<=2;d++)
{
for
(e=1;e<=3;e++)
{ cout<<
"Enter the number: ";
cin>>
a[d][e]; }
}
cout<< "\n
Second matrix.\n";
for (d=1;d<=2;d++)
{
for
(e=1;e<=3;e++)
{ cout<<
"Enter the number: ";
cin>>
b[d][e]; }
}
cout<< "\n
Difference of matrices(a-b) : \n";
for (d=1;d<=2;d++)
{
for
(e=1;e<=3;e++)
{
cout<<
a[d][e]+b[d][e] << '\t';
}
cout<< endl;
}
getch();
}
6. Program to find the transpose of the matrix
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],i,j;
cout<<"Enter the matrix"<<endl;
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"matrix is"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==0||i==1&&j==2)
{int t=a[i][j];
a[i][j]=a[j][i];
a[j][i]=t;
}
}
}
cout<<"transpose of the matrix
is"<<endl;
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
getch();
}
7. Program to sort a list of numbers using Bubble Sort
// Bubble Sorting
#include<iostream.h>
void main()
{
int a[20];
inti,j,n,t;
cout<<"\n Enter the number of elements(>20) : ";
cin>>n;
if(n>20)
cout<<"\n List size cannot exceed 20 ";
else
{
cout<<"\n Enter the elements : \n ";
// accepting the list of elements
for(i=0;i<n;i++)
cin>>a[i];
// Sorting
for(i=0;i<n;i++)
{ cout<<"\n Pass : "<<i+1;
for(j=0;j<(n-i);j++)
{
if(a[j+1]<a[j])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
cout<<"\n";
for(int k=0;k<n;k++)
cout<<"\t"<<a[k];
}
cout<<"\n The sorted list is : ";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
cout<<"\n Have a nice day ";
}
}
Programme to search ofr a word in a file.
#include<fstream.h>
#include<conio.h>
int strcmp(char s[], char f[])
{ int i,j;
for(i=0;s[i]!='\0';i++);
for(j=0;f[j]!='\0';j++);
if(i==j)
{
for(i=0;i<=j;i++)
if(s[i]!=f[i])
return 1;
return 0;
}
else
return 1;
}
void main()
{ clrscr();
int i,j;
char sname[20],fname[20];
ifstream f1("sfile.txt",ios::in);
int c=0;
cout<<"\n Enter name to be searched ";
cin>>sname;
while(f1)
{ f1>>sname;
cout<<" : "<<sname;
if(strcmp(sname,fname)==0)
{ cout<<"name found";
c++;
}
}
if(c==0)
cout<<"\n Name not found ";
f1.close();
}
Q: Program to accept three names and count number of times character 'a' appears in the file.
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char n[20];
cout<<endl;
ofstream fin;
fin.open("random.txt", ios::app);
for(int i=0;i<3;i++)
{
cin>>n;
fin<<n<<" ";
}
fin.close();
char cc;
int cnt=0;
ifstream fi("random.txt");
while(fi)
{ fi>>cc;
if(cc=='a')
cnt++;
}
cout<<"\n a appears "<<cnt <<" number of times";
fi.close();
}
// Bubble Sorting
#include<iostream.h>
void main()
{
int a[20];
inti,j,n,t;
cout<<"\n Enter the number of elements(>20) : ";
cin>>n;
if(n>20)
cout<<"\n List size cannot exceed 20 ";
else
{
cout<<"\n Enter the elements : \n ";
// accepting the list of elements
for(i=0;i<n;i++)
cin>>a[i];
// Sorting
for(i=0;i<n;i++)
{ cout<<"\n Pass : "<<i+1;
for(j=0;j<(n-i);j++)
{
if(a[j+1]<a[j])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
cout<<"\n";
for(int k=0;k<n;k++)
cout<<"\t"<<a[k];
}
cout<<"\n The sorted list is : ";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
cout<<"\n Have a nice day ";
}
}
8: Program to delete an element from a given list of numbers
#include<iostream.h>
#include<conio.h>
void deletar(int a[ ], int n, int item) //n is the number of elements in the array
{ int i=0;
while(i<n && a[i]<item)
i++;
if (a[i]==item) // given item is found
{ while (i<n)
{ a[i]=a[i+1]; // shift the (i+1)th element one position towards left
i++;
}
cout<<"\n Given item is successfully deleted";
n--;
}
else
cout<<"\n Given item is not found in the array";
}
void main()
{ clrscr();
int a[10]={2,4,5,7,8,11,12,15},n=8;
int i=0;
cout<<"Original array is :\n";
for(i=0;i<n;i++)
cout<<a[i]<<" , ";
deletar(a,n,11);
cout<<"\n Array after deleting 11 is:\n";
for(i=0; i<n; i++)
cout<<a[i]<<", ";
}
9. Program to insert an element at appropriate position in a sorted array.
#include<iostream.h>
void insertar(int a[ ], int num, int val) //n is the number of elements in the array
{ int i=num-1;
while (i>=0 && a[i]>val)
{
a[i+1]=a[i]; // shift the element one position towards right
i--;
}
a[i+1]=val; //insertion of item at appropriate place
}
void main()
{
int a[20]={1,2,3,5,7,12,15,19,24},count=9;
int i=0;
cout<<"Original array is:\n";
for(i=0;i<count;i++)
cout<<a[i]<<",";
insertar(a,count++, 10);
cout<<"\n Array after inserting 10 is:\n";
for(i=0; i<count; i++)
cout<<a[i]<<", ";
}
10. Program to sort a list of numbers using selection sort.
Program to calculate salary of employees
#include<iostream.h>
#include<conio.h>
class employ
{
int eno,el;
char ename[40];
float gross,sal;
char salary();
float calcsal(int sal,int el)
{
float grossal= sal+(sal*0.3)+(sal*0.4)-(sal*el)/30;
return (grossal);
}
public:
void getdata()
{
float gross;
cout<<"enter the details";
}
void display()
{
cout<<ename<<sal<<el<<eno;
cout<<gross;
}
};
void main()
{
clrscr();
employ r1;
r1.getdata();
r1.display();
getch();
}
Program to count number of times a word appears in a file.
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{clrscr();
int i=0;
char ch[15],c[10];
ifstream obj("villager.txt");
cout<<"enter the word to be searched";cin>>c;
while(!obj.eof())
{
obj>>ch;
if(strcmp(ch,c)==0)
{ i++ ;}
}
cout<<"number of times "<<c<<" appears on file is";
cout<<i;
obj.close();
getch();
}
Program to assign grades to student
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class stud
{
int rno,age;
public:
void input();
void disp();
int retroll();
};
void stud::input()
{
cout<<"ENTER THE ROLL NO OF STUDENT";
cin>>rno;
cout<<"ENTER THE AGE OF STUDENT";
cin>>age;
}
void stud::disp()
{
cout<<"THE ROLL NO OF STUDENT IS"<<rno;
cout<<"THE AGE OF STUDENT IS"<<age;
}
int stud::retroll()
{
return rno;
}
void main()
{
clrscr();
int a,b;
char ch='y';
do
{
cout<<"ENTER 1. TO ACCEPT RECORD,2. TO DISPLAY RECORDS,3.TO SEARCH RECORD";
cin>>a;
switch(a)
{
case 1:{stud s;
s.input();
ofstream ofile("sample.dat",ios::binary);
ofile.write((char*)&s,sizeof(s));
ofile.close();
break;
}
case 2:{stud s1;
ifstream infile("sample.dat",ios::binary);
while(infile)
{
infile.read((char*)&s1,sizeof(s1));
s1.disp();
}
infile.close();
break;
}
case 3:{stud s2;
int t,g=0;
cout<<"ENTER THE ROLL NO TO SEARCH";
cin>>t;
ifstream infile("sample.dat",ios::binary);
while(infile)
{infile.read((char*)&s2,sizeof(s2));
if(s2.retroll()==t)
{cout<<"record found";
s2.disp();
}
else
g++;
}
if(g)
cout<<"record not found";
infile.close();
break;
}
default :{cout<<"invalid choice";}
}
cout<<"enter y or Y to continue";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch();
}
Program to accept details of a student and calculate the percentage.
#include<iostream.h>
#include<conio.h >
class student
{ int rollno;
char name[20];
float marks[5],percent;
float calcpercent();
public:
void input();
void display();
}s[3];
float student::calcpercent()
{int i;
float total=0,p;
for(i=1;i<=5;i++)
total+=marks[i];
percent=total/5;
return(p);
}
void student::input()
{ int i=0;
cout<<"rollno,name,marks"<<endl;
cin>>rollno>>name;
for(i=0;i<=5;i++)
{cin>>marks[5];
percent=calcpercent();
}
void student::display();
{int i;
cout<<"rollno. "<<rollno<<endl;
cout<<"name "<<name<<endl;
for(i=0;i<=5;i++)
cout<<marks[i]<<" ";
cout<<endl<<percent;
}
void main();
{clrscr();
int i;
for(i=0;i<=3;i++)
{s[i].input();
s[i].display();
}
getch();
}
/*write a program to accept minimum 5 names from the user and write these names
onto a file name"FILE .TXT"*/
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<process.h>
void main()
{
clrscr();
char name[10];int i;
ofstream fin("DST.TXT",ios::in);
for(i=0;i<5;i++)
{
cout<<"\n\t enter the 5 names of the student";
cin>>name;
fin<<name<<endl;
}
fin.close();
ifstream f2("DST.TXT",ios::out);
cout<<"\n\t the names are "<<endl;
while (!f2.eof())
{
f2>>name;
cout<<name;
}
f2.close();
getch();
}
#include<iostream.h>
void selection_sort(int
a[ ], int size)
{ int i, j, pos, min;
for(i=0;i<size-1;i++)
{
min=a[i]; // initialize min
with ith element of unsorted array
pos=i; // pos saves index value of the
smallest element
//loop for selecting the smallest
element form unsorted array
for(j=i+1; j<size; j++)
{if(a[j]<min)
{
min=a[j]; // overwriting min
with least value found
pos=j; //pos saves the index value of smallest
element
}
}
// swapping
the next smallest value with ith value to its appropriate location
a[pos]=a[i];
a[i]=min
}
}
void main(
)
{ int a[10],n,i;
cout<<”\n Enter the number of elements
in the list : “;
cin>>n;
cout<<”\n Enter values in the array is
:\n”;
for(i=0;i<n ;i++)
cout<<a[i]<<”, “;
select_sort(a,n);
cout<<”\n The sorted array is:\n”;
for(i=0; i<n; i++)
cout<<a[i]<<”, “;
}
Output is
Enter the number of
elements in the list : 5
Enter values in the array
is :
4
12
8
7
6
The sorted array is:
4
6
7
8
12
11. Program to sort a list of numbers using insertion sort.
#include<iostream.h>
void insertsort( int ar[]))
{ intj,k,tmp,min;
for(j=1; j<n-1;j++)
{ min=ar[j];
for( k=0;k<j;k++)
{ if(ar[k]>ar[j])
{ tmp=ar[j];
for(pos=j;pos>k;pos++) //
loop for shifting the elements
ar[pos]=ar[pos-1];
}
}
}
}
void main()
{int num[20];
cout<<”\n Enter the number of elements in the
array (max 20) : ” ;
cin>>n;
if(n<=20)
{ cout<<”\n Enter the elements of the
array :
\n ”;
for(i=0;i<n;i++)
cin>>num[i];
insert(num);
cout<<”\n The sorted list is : “;
for(i=0;i<n;
i++)
cout<<”\n”<<num[i];
}
Output :
Enter the number of elements in the
array (max 20) :
6
Enter the elements of the array :
23
12
34
5
6
20
The sorted list is :
5
6
12
20
23
34
12.Program to sort a list using insertion sort and showing the status of list after every pass.
13. Program to implement merge sort
14. Program to declare a class student in C++.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
int r;
char name[20],ad[40];
public:
void input();
void display();
};
void student::input()
{
cout<<"Enter details";
cin>>r>>name>>ad;
}
void student::display()
{
cout<<r<<"\t"<<endl;
cout<<name<<endl;
cout<<ad<<endl;
}
void main()
{
clrscr();
student s;
s.input();
s.display();
getch();
}
15. Program to read data of employees and insert it in a file
#include "iostream.h"
void main()
{
int a[10],i,j,pos,k,tmp;
cout<<"\n Enter the number of elements : ";
int n;
cin>>n;
cout<<"\n Enter the elements of array : ";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{ if(a[i]<a[j])
{ pos=i;
tmp=a[i];
while( pos>j)
{
a[pos]=a[pos-1];
pos--;
}
a[j]=tmp;
}
}
cout<<"After
pass : "<<i+1<<" - ";
for(int
m=0;m<n;m++) cout<<"\t"<<a[m];
cout<<endl;
}
cout<<"\n the sorted array is : \n ";
for(i=0;i<n;i++)
cout<<"\t
"<<a[i];
cout<<"\n _____________________";
}
13. Program to implement merge sort
#include<iostream.h>
void Mergesort(int
a[ ], int m, int b[n], int c[ ])// m is size of array a and n is the size of
array b
{ int
i=0; // i points to the smallest element of the array a which is at index 0
int
j=0;// j points to the smallest element of the array b which is at the index
m-1 since b is
int k=0; // k is index counter for array C
while((i<m)&&(j<n))
{ if(a[i]<b[j])
c[k++]=a[i++]; // copy value from a into c and then
increment i and k
else
c[k++]=b[j++]; // copy value b into array c and then increment
j and k
}
while(i<m)
//copy all remaining
elements of array a, if not copied
c[k++]=a[i++];
while(j>=0)
//copy all remaining
elements of array b, if not copied
c[k++]=b[j--];
}
void main()
{ int array1[10], array2[10],
marray[20];
int a1,b1,c1, i,j,k;
// accept
values of array a and array b;
cout<<”\n enter the number of
elements if first array array1: ”;
cin>>a1;
cout<<”\n enter elements of array1 in
ascending order : ”;
for(int i=0;i<a1;i++)
cin>>a[i];
cout<<”\n enter the number of
elements of second array b”;
cin>>b1;
cout<<”\n enter elements of array2
in ascending array : ”;
for(int i=0;i<b1;i++)
cin>>b[i];
mergesot(array1, a1, array2, a2, marray);
cout<<”the
merged array is :\n”;
for(int
i=0; i<(a1+a2); i++)
cout<<marray[i]<<”,
“;
}14. Program to declare a class student in C++.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
int r;
char name[20],ad[40];
public:
void input();
void display();
};
void student::input()
{
cout<<"Enter details";
cin>>r>>name>>ad;
}
void student::display()
{
cout<<r<<"\t"<<endl;
cout<<name<<endl;
cout<<ad<<endl;
}
void main()
{
clrscr();
student s;
s.input();
s.display();
getch();
}
15. Program to read data of employees and insert it in a file
#include<fstream.h>
#inlude<conio.h>
#include<stdio.h>
class Employee()
{
int empid;
char
empname[10];
char
empdesg[10];
public:
void input()
{
while(ch=='Y'||ch=='y')
{
cout<<"
Enter the Employee name\n";
gets(empname);
cout<<"
Enter the Employee id\n";
cin>>empid;
cout<<"
Enter the Employee disignation\n";
gets(empdesg);
cout<<"
Do you want to continue?(Y\y\N\n)\n";
cin>>ch;
}
}
};
void main()
{
clrscr();
Employee obj;
ifstream fin;
fin.open("Employee.dat",ios::in);
while(!fin.eof)
{
fin.write((char*)&obj,size
of(obj);
}
}
16. Program to illustrate destructors in C++
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
int rollno;
char name[20];
public:
student()
{
rollno=5;
strcpy(name,"aman rana");
}
~student()
{
cout<<"destructor program is over";
}
};
void main()
{
clrscr();
student s;
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
int rollno;
char name[20];
public:
student()
{
rollno=5;
strcpy(name,"aman rana");
}
~student()
{
cout<<"destructor program is over";
}
};
void main()
{
clrscr();
student s;
getch();
}
Program to calculate salary of employees
#include<iostream.h>
#include<conio.h>
class employ
{
int eno,el;
char ename[40];
float gross,sal;
char salary();
float calcsal(int sal,int el)
{
float grossal= sal+(sal*0.3)+(sal*0.4)-(sal*el)/30;
return (grossal);
}
public:
void getdata()
{
float gross;
cout<<"enter the details";
}
void display()
{
cout<<ename<<sal<<el<<eno;
cout<<gross;
}
};
void main()
{
clrscr();
employ r1;
r1.getdata();
r1.display();
getch();
}
Program to count number of times a word appears in a file.
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{clrscr();
int i=0;
char ch[15],c[10];
ifstream obj("villager.txt");
cout<<"enter the word to be searched";cin>>c;
while(!obj.eof())
{
obj>>ch;
if(strcmp(ch,c)==0)
{ i++ ;}
}
cout<<"number of times "<<c<<" appears on file is";
cout<<i;
obj.close();
getch();
}
Program to assign grades to student
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class stud
{
int rno,age;
public:
void input();
void disp();
int retroll();
};
void stud::input()
{
cout<<"ENTER THE ROLL NO OF STUDENT";
cin>>rno;
cout<<"ENTER THE AGE OF STUDENT";
cin>>age;
}
void stud::disp()
{
cout<<"THE ROLL NO OF STUDENT IS"<<rno;
cout<<"THE AGE OF STUDENT IS"<<age;
}
int stud::retroll()
{
return rno;
}
void main()
{
clrscr();
int a,b;
char ch='y';
do
{
cout<<"ENTER 1. TO ACCEPT RECORD,2. TO DISPLAY RECORDS,3.TO SEARCH RECORD";
cin>>a;
switch(a)
{
case 1:{stud s;
s.input();
ofstream ofile("sample.dat",ios::binary);
ofile.write((char*)&s,sizeof(s));
ofile.close();
break;
}
case 2:{stud s1;
ifstream infile("sample.dat",ios::binary);
while(infile)
{
infile.read((char*)&s1,sizeof(s1));
s1.disp();
}
infile.close();
break;
}
case 3:{stud s2;
int t,g=0;
cout<<"ENTER THE ROLL NO TO SEARCH";
cin>>t;
ifstream infile("sample.dat",ios::binary);
while(infile)
{infile.read((char*)&s2,sizeof(s2));
if(s2.retroll()==t)
{cout<<"record found";
s2.disp();
}
else
g++;
}
if(g)
cout<<"record not found";
infile.close();
break;
}
default :{cout<<"invalid choice";}
}
cout<<"enter y or Y to continue";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch();
}
Program to accept details of a student and calculate the percentage.
#include<iostream.h>
#include<conio.h >
class student
{ int rollno;
char name[20];
float marks[5],percent;
float calcpercent();
public:
void input();
void display();
}s[3];
float student::calcpercent()
{int i;
float total=0,p;
for(i=1;i<=5;i++)
total+=marks[i];
percent=total/5;
return(p);
}
void student::input()
{ int i=0;
cout<<"rollno,name,marks"<<endl;
cin>>rollno>>name;
for(i=0;i<=5;i++)
{cin>>marks[5];
percent=calcpercent();
}
void student::display();
{int i;
cout<<"rollno. "<<rollno<<endl;
cout<<"name "<<name<<endl;
for(i=0;i<=5;i++)
cout<<marks[i]<<" ";
cout<<endl<<percent;
}
void main();
{clrscr();
int i;
for(i=0;i<=3;i++)
{s[i].input();
s[i].display();
}
getch();
}
/*write a program to accept minimum 5 names from the user and write these names
onto a file name"FILE .TXT"*/
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<process.h>
void main()
{
clrscr();
char name[10];int i;
ofstream fin("DST.TXT",ios::in);
for(i=0;i<5;i++)
{
cout<<"\n\t enter the 5 names of the student";
cin>>name;
fin<<name<<endl;
}
fin.close();
ifstream f2("DST.TXT",ios::out);
cout<<"\n\t the names are "<<endl;
while (!f2.eof())
{
f2>>name;
cout<<name;
}
f2.close();
getch();
}
Programme to search ofr a word in a file.
#include<fstream.h>
#include<conio.h>
int strcmp(char s[], char f[])
{ int i,j;
for(i=0;s[i]!='\0';i++);
for(j=0;f[j]!='\0';j++);
if(i==j)
{
for(i=0;i<=j;i++)
if(s[i]!=f[i])
return 1;
return 0;
}
else
return 1;
}
void main()
{ clrscr();
int i,j;
char sname[20],fname[20];
ifstream f1("sfile.txt",ios::in);
int c=0;
cout<<"\n Enter name to be searched ";
cin>>sname;
while(f1)
{ f1>>sname;
cout<<" : "<<sname;
if(strcmp(sname,fname)==0)
{ cout<<"name found";
c++;
}
}
if(c==0)
cout<<"\n Name not found ";
f1.close();
}
Q: Program to accept three names and count number of times character 'a' appears in the file.
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char n[20];
cout<<endl;
ofstream fin;
fin.open("random.txt", ios::app);
for(int i=0;i<3;i++)
{
cin>>n;
fin<<n<<" ";
}
fin.close();
char cc;
int cnt=0;
ifstream fi("random.txt");
while(fi)
{ fi>>cc;
if(cc=='a')
cnt++;
}
cout<<"\n a appears "<<cnt <<" number of times";
fi.close();
}
No comments:
Post a Comment