You are on page 1of 30

S.NO.

1. 2. 3. 4. 5.

PROGRAM
Progarm to create a single file and then display its contents. Program to use multiple files in succession. Program to use multiple files simultaneously. Program for reading and writing class objects. Program to implement searching ina file that has records maintained through structures.

SIGN.

6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26.

Program to append data in a file. Program to insert data in a sorted file. Program to delete a record from a file. Program to modify data in a given file. Program to perform linear search. Program to perform binary search. Program to perform insertion in an array. Program to perform deletion in an array. Program to perform traversal in an array. Program to perform bubble sort in an array. Program to perform insertion sort in an array. Program to perform insertion in the beginning of a list. Program to perform insertion in the end of a list. Program to perform deletion from the beginning of list. Program to perform traversal in a list. Pushing in Stack-Array. Popping from Stack-Array. Pushing in linked-Stack. Popping from linked stack. Insertion in Array-Queue. Insertion in linked-Queue.

3. Program to use multiple files simultaneously.


#include<iostream.h> #include<fstream.h> #include<stdlib.h> Int main() { System (cls); //Assuming that the two files (stumarks and stunames) are already created. Ifstream filin1 , filin2; Filin1.open(stunames,ios::in); Filin2.open(stumarks,ios::in); Char line[80]; Cout<<The contents of stunames and stumarks are given below<<endl; Filin1.getline(line,80); Cout<<line<<\n; Filin2.getline(line,80); Cout<<line<<\n; Filin1.getline(line,80); Cout<<line<<\n; Filin2.getline(line,80); Cout<<line<<\n; Filin1.getline(line,80); Cout<<line<<\n; Filin2.getline(line,80); Cout<<line<<\n; Filin1.close(); Filin2.close(); Return 0; }

2. Program to use multiple files in succession.


#include<iostream.h> #include<fstream.h> #include<stdlib.h> Int main() { System (cls); //First create two files. Ofstream filout; Filout.open(Stunames,ios::out); Filout<<Devyani\n<<Monica Patrick\n<<Neil Banerjee\n; Filout.close(); Filout.open(stumarks,ios::out); Filout<<72.98\n<<72.17\n<<69.33\n; Filout.close(); //Read these files. Char line[80]; Ifstream filin; Filin.open(stunames,ios::in); Cout<<The contents of stuname 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; Fin.close(); Filin.close(); Filin.open(stumarks,ios::in);

1. Program to create a single line and then display its contents.


#include<iostream.h> #include<fstream.h> #include<stdlib.h> Int main() { System(cls); Ofstream fout(student,ios::out); Char name[30],ch; Float marks=0.0; //Loop yo get 5 records 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( i=0; i<5 ; i++) { Fin.get(name,30); Fin.get(ch); Fin<<marks;

25. Program to perform insertion in Array-Queue.


#include<iostream.h> #include<stdlib.h> #include<process.h> int insert_in_q(int queue[],int ele) { if(rear=size-1) return -1; else if(rear==-1) { front=rear=0; queue[rear]=ele; } else { rear++; queue[rear]=ele; } return 0; } void display(int queue[], int front, int rear) { if(front==-1) return; for(int i=front;i<rear;i++) cut<<queue[i]<<"<-\t"; cout<<queue[rear]<<endl;

24. Program to perform popping from a linked-stack.


#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<process.h> struct node{ int info; node*next; }*save, *newptr, *save, *ptr; node*Create_new_node(int) { ptr=new node; ptr->info=n; ptr->next=null; return ptr; } void push(node*np) { if(top==NULL) top=n; else { save=top; top=np; np->next=save; } }

23. Program to perform pushing in Linked-Stack.


#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<process.h> struct node{ int info; node*next; }*save, *newptr, *save, *ptr, *rear; node*Create_new_node(int) { ptr=new node; ptr->info=n; ptr->next=null; return ptr; } void push(node*np) { if(top==NULL) top=n; else { save=top; top=np; np->next=save; } }

22.Program to perform popping from Array-stack.


#include<iostream.h> #include<process.h> int push(int stack[], int&top , int ele) { if(top==size-1) return -1; else { 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)

21. Program to perform pushing in stack-Array.


#include<iostream.h> #include<stdlib.h> int push(int stack[], int&top , int ele) { if(top==size-1) return -1; else { top++; stack[top]=ele; } return 0; } void display(int stack[], int top) { cout<<stack[top]<<"<--"<<endl; for(int i=top-1; i>=0 ;i--) cout<<stack[i]<<endl; } const int size=50; int main() { int stack[size], Item ,top=-1 ,res; char ch='y'; system("cls"); while(ch=='y'||ch=='Y')

20. Program to perform traversal in a list.


#include<iostream.h> #include<process.h> struct node{ int info; node*next; }*save, *newptr, *save, *ptr, *rear; node*Create_new_node(int) { ptr[=new node; ptr->info=n; ptr->next=null; return ptr; } void insert(node* np) { if(start==null; start=rear=np; else { rear->next=np; rear=np; } } void traverse(node*np) { while(np!=NULL)

19. Program to perform deletion from beginning of the list.


#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<process.h> struct node{ int info; node*next; }*save, *newptr, *save, *ptr, *rear; node*Create_new_node(int) { ptr[=new node; ptr->info=n; ptr->next=null; return ptr; } void insert(node* np) { if(start==null; start=rear=np; else { rear->next=np; rear=np; } } void delnode()

18. Program to perform insertion in end.


#include<iostream.h> #include<process.h> struct node { int info; node*next; } *start, *newptr ,*save, *ptr, *rear; node*Create_New_Node(int n) { ptr=new Node; ptr->info=n; ptr->next=NULL; return ptr; } void insert_End(Node* np) { if(start==NULL) start=rear=np; else { rear->next=np; rear=np; } } } void Display(Node* np) { while(np!=NULL)

17. Program to perform insertion in the beginning of a list.


#include<iostream.h> #include<process.h> struct node { int info; node*next; } *start, *newptr ,*save, *ptr; node*Create_New_Node(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) {

16. Program to perform insertion sort in array.


#include<iostream.h> #include<limits.h> void Inssort(int ar[], int size) { int tmp, j; ar[0]=INT_MIN; for(int i=1; i<=size ; i++) { tmp=ar[i]; j=i-1; while(tmp<ar[j]) { ar[j+1]=ar[j]; j--; } ar[j+1]=tmp; cout<<"Array after pass-"<<i<<"-is-"; for(int k=0; k<=size; k++) cout<<ar[k]<<" "; } } int main() { int ar[50], item , N ,index; cout<<"Enter desired no. of elements in an array"; cin>>N;

15. Program to perform bubble sort in an array.


#include<iostream.h> #include<conio.h> void bubblesort(int ar[],int size) { int tmp, ctr=0; for(int i=0; i<size ;i++) { for(int j=0; j<(size-1)--i;j++) { if(ar[j]>ar[j+1]) { tmp=ar[j]; ar[j]=ar[j+1]; ar[j+1]=tmp; } } cout<<"Array after iteration is-"<<++ctr<<"-is-"; for(int k=0; k<size ;k++) cout<<ar[k]<<" "; cout<<endl; } } int main() { int ar[50], item , N ,index; cout<<"Enter desired no. of elements in an array";

14. Program to perform Traversal in an array.


#include<iostream.h> int main() { int ar[50], item , N , index; cout<<"Enter the no. of desired elements in array"; cin>>N; cout<<"\nEnter array elements..\n"; for(int i=0;i<N; i++) cin>>ar[i]; cout<<"\nArray widout doubled elements in an array is as follows...\n"; for(i=0; i<N ; i++) { ar[i]*=2; cout<<ar[i]<<" "; } cout<<endl; return 0; }

13. Program to perform deletion in an array.


#include<iostream.h> #include<process.h> { for(int i=0;i<=size-1;i++) { if(ar[i]==item) return i; } return -1; } int main() { int ar[50], item , N , index; cout<<"Enter the desired no. of elements"; cin>>N; cout<<"\nEnter array elements"<<endl; for(i=0; i<N; i++) cin>>ar[i]; char ch='y'; while(ch=='y'||ch=='Y') { cout<<"Enter element to be created"; cin>>item; if(N==0) { cout<<"Underflow!!\n";

12. Program to perform insertion in an array.


#include<iotream.h> #include<conio.h> Int isearch(int ar[] , int size , int item; { int pos; If(item<=ar[0]) Pos=0; else { For(i=0; i<=size-1 ; i++) { if(ar[i]<=item&&item<ar[i+1]) { pos=i+1;; break; } } if(i==size-1) pos=size; } return pos; } int main() { int ar[50],item ,N , index; cout<<"Enter the desired no. of elements in array";

11. Program to perform binary search in an array.


#include<iostream.h> #include<conio.h> Int Bsearch(int ar[] , int size , int item) { int beg=0 , last=size-1 , mid; Mid=(beg+last)/2; If(ar[mid]==item) Return mid; Else if(ar[mid]<=item) Beg=mid+1; Else Last=mid-1; } Int main() { Int ar[40], n , item , index; Cout<<Desired no. of elements in array; Cin>>n; Cout<<Enter elements of array; For(i=0 ; i<n ; i++) Cin<<ar[i]; Cout<<Enter the element to be searched for:; Cin>>item; Index=Bsearch(ar,n,item) If(index==-1) Cout<<Element does not exist; Else Cout<<Element found at index<<Index<<at position<<index+1; Return 0;

10. Program to perform linear search in an array.


#include<iostream.h> #include<conio.h> Int lsearch (int ar[], int size , int item) { For(i=0; i<=size-1; i++) { If(ar[i]==item) Return I; } Return -1; } Int main() { Intar[40], n , item , index; Cout<<Desired no. of elements in array; Cin>>n; Cout<<Enter elements of array; For(i=0 ; i<n ; i++) Cin<<ar[i]; Cout<<Enter the element to be searched for:; Cin>>item; Index=lsearch(ar,n,item) If(index==-1) Cout<<Element does not exist; Else Cout<<Element found at index<<Index<<at position<<index+1; Return 0; }

9.Program to modify data in a given file.


#include<iostream.h> #include<fstram.h> #include<stdio.h> #include<string.h> #include<stdlib.h> Class stu{ int rollno; char name[25]; char class[4]; float marks; char grade; public: void getdata() { Cout<<Rollno:; cin>>rollno; cout<<Cass:; cin>>class; 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;

8. Program to delete a record from a file.


#include<iostream.h> #include<fstream.h> #include<stdio.h> #include<string.h> Class stu{ int rollno; char name[25]; char class[4]; float marks; char grade; public: void getdata() { Cout<<Rollno:; cin>>rollno; cout<<Cass:; cin>>class; 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

7.Program to insert data in a sorted file.


#include<iostream.h> #include<fstream.h> #include<stdio.h> Class stu{ Int rollno; Char name[25]; Char class[4]; Float marks; Char grade; Public: void getdata() { Cout<<Rollno:; cin>>rollno; cout<<Cass:; cin>>class; 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;

6. Program to append data in a file.


#include<iostream.h> #include<fstram.h> Class stu{ int rollno; char name[25]; char class[4]; float marks; char grade; public: void getdata() { Cout<<Rollno:; cin>>rollno; cout<<Cass:; cin>>class; 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; }

5.Program to implement searching in a file the has records maintained through structures.
#include<iostream.h> #include<fstream.h> Struct stu{ Int rollno; Char name[25]; Char class[4]; Float marks; Char grade; }s1; Int main() { Int rn, char found=n; Ifstream fi(stu.dat,ios::in); Cout<<Enter roll no. to be searched for:; Cin>>rn; While(!fin.eof()) { Fi.read((char*)&s1,sizeof(s1); If(s1.rollno==rn) { Cout<<s1.name<<,rollno<<rn<<has<<s1.marks<<%marks and<<s1.grade<<grade.<<endl; Found=y; Break; } } If(found==n) Cout<<rollno not found in file!!<<endl;

26.Program to perform insertion in Linked queue.


#include<iostream.h> #include<process.h> struct node{ int info; node*next; }*save, *newptr, *save, *ptr, *rear; node*Create_new_node(int) { ptr[=new node; ptr->info=n; ptr->next=null; return ptr; } void insert_End(Node* np) { if(start==NULL) start=rear=np; else { rear->next=np; rear=np; } } } void Display(Node* np) {

4. Program for reading and writing class objects.


#include<iostream.h> #include<fstream.h> #include<stdlib.h> Class student { Char name[40]; Char grade; Float marks; Public: Void getdata(void); Void display(void); }; Void student::getdata(void) { Char ch; Cin.get(ch); Cout<<Enter name:; Cin.getline(name,40); Cout<<Enter grade:; Cin>>grade; Cout<<Enter marks:; Cin>>marks; Cout<<endl; } Void student::display(void) { Cout<<Name:<<name<<\t<<Grade:<<grade<<\t<<Marks:<<marks<<\t<<endl; } Int main()

You might also like