Sei sulla pagina 1di 111

EX NO:

DATE:

Implementation of matrix vector multiplication with Static data, default argument & friend functions
AIM
To write a C++ program for friend function, default arguments and static function.

ALGORITHM STEP 1: Start the program STEP 2: The header files are declared. STEP 3: A class called sample is declared. STEP 4: setvalue() is used to assign values for a and b STEP 5: mean() is declared as friend. STEP 6. Using mean() the average of 2 numbers is calculated. STEP 7: In main() object for sample class is declared. STEP 8: All functions are called using the object. STEP 9: Stop the program.

PROGRAM CODING
#include<iostream.h> #include<conio.h> class vector; class matrix { static int m[3][3]; public: void getmatrix(void); void dismatrix(void); friend void multiply(matrix &,vector &); }; int matrix::m[3][3]; class vector { static int v[10]; public: void getvector(int n=3); void disvector(void); friend void multiply(matrix &,vector &); }; int vector::v[10]; void vector::getvector(int n) 2

{ int i; cout<<"\nEnter elements for vector one by one:\n"; for(i=0;i<n;i++) cin>>v[i]; } void vector::disvector() { int i; cout<<"\nThe vector elements are:\n"; for(i=0;i<3;i++) cout<<v[i]<<"\t"; } void matrix::getmatrix() { int i,j; cout<<"\nEnter the matrix:\n"; for(i=0;i<3;i++) for(j=0;j<3;j++) cin>>m[i][j]; } void matrix::dismatrix() {

int i,j; cout<<"\n Entered matrix is: \n"; for(i=0;i<3;i++) { for(j=0;j<3;j++) cout<<m[i][j]<<"\t"; cout<<"\n"; } } void multiply(matrix &m1,vector &v1) { int ans[3],i,j; cout<<"\nThe result matrix : \n"; for(i=0;i<3;i++) { ans[i]=0; for(j=0;j<3;j++) ans[i]+=m1.m[i][j]*v1.v[j]; cout<<ans[i]<<"\t"; } } void main() {

matrix m1; vector v1; clrscr(); m1.getmatrix(); m1.dismatrix(); v1.getvector(); v1.disvector(); multiply(m1,v1); getch(); }

OUTPUT
Enter the matrix: 1 2 3 4 5 6 7 8 9 Entered matrix is: 1 4 7 5 8 2 6 9 3

Enter elements for vector one by one: 2 4 6 The vector elements are: 2 4 6

The result matrix: 28 64 100 6

RESULT:
Thus the c++ program for friend functions, default arguments and static function was written and executed successfully.

EX NO:

DATE:

Implementation of Complex numbers using operator overloading

AIM
To write a c++ program for performing implementation of Complex numbers using operator overloading.

ALGORITHM STEP 1: Start the program. STEP 2: The header files are declared. STEP 3: A class complex is declared. STEP 4: Constructor complex is used to initialize the values for variables real and image. STEP 5: Getdata() is used to get values for both real and imag part. STEP 6: putdata() is used to display the real and imag part STEP 7: The operator +,-,/,* are overloaded. STEP 8: In main program,the object for class complex is declared and using it all the
functions are called.

STEP 9: Stop the program.

PROGRAM CODING
#include<iostream.h> #include<conio.h> #include<process.h> class complex { float x,y; public: complex() {} complex(float real,float imag) { x=real; y=imag; } complex operator + (complex); complex operator - (complex); complex operator * (complex); complex operator / (complex); void getdata(); void show(); }; void complex::getdata() 9

{ cout<<"\nEnter real part:"; cin>>x; cout<<"\nEnter imag part:"; cin>>y; } void complex::show() { cout<<"\nReal part:"<<x; cout<<"\nImag part:"<<y; } complex complex :: operator +(complex c) { complex temp; temp.x=x+c.x; temp.y=y+c.y; return(temp); } complex complex :: operator -(complex c) {

complex temp;

10

temp.x=x-c.x; temp.y=y-c.y;

return(temp); } complex complex :: operator *(complex c) { complex temp; temp.x = ((x * c.x) - (y * c.y)); temp.y = ((x * c.y) + (y * c.y)); return(temp); } complex complex :: operator /(complex c) { complex temp; temp.x=((x * c.x) + (y * c.y))/((x * c.x) + (y * c.y)); temp.y=((y * c.x) - (x * c.y))/((x * c.x) + (y * c.y)); return(temp); } void main() { clrscr();

11

complex c1,c2,c3; int op; char ch ,y,Y; do { c1.getdata(); c2.getdata(); cout<<"\n******** MENU *********"<< endl; cout<< "\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit"<< endl; cout<< "\nEnter Your Choice : "; cin>>op; switch(op) { case 1: cout<< "\nAddition of Two complex Nos. "; c3 = c1 + c2; c3.show(); break; case 2: cout<< "\nSubtraction of Two complex Nos. "; c3 = c1 - c2; c3.show(); break;

12

case 3: cout<<" \nMultiplication of Two complex Nos."; c3 = c1 * c2; c3.show(); break; case 4: cout<< "\ndivision of Two complex Nos. "; c3 = c1 / c2; c3.show(); break; case 5: exit(0); } cout<< " \nDo you want to continue(Y/y)"; cin>>ch; } while(ch=='y'||ch=='Y'); getch(); }

13

OUTPUT
Enter real part:1 Enter imag part:3 Enter real part:4 Enter imag part:6 ********MENU******** 1.Addition 2.Subtraction 3.Multiplication 4.Division .Exit Enter your choice:1 Addition of 2 complex no's Real part:5 Imag part:9 Do u want to continue(Y/y)y Enter real part:47 Enter imag part:9 Enter real part:43 Enter imag part:7 ********MENU******** 1.Addition 2.Subtraction 14

3.Multiplication 4.Division .Exit Enter your choice:2 Subraction of 2 complex no's Real part:4 Imag part:2 Do u want to continue(Y/y) Enter real part:3 Enter imag part:6 Enter real part:43 Enter imag part:11 ********MENU******** 1.Addition 2.Subtraction 3.Multiplication 4.Division .Exit Enter your choice:3 Multiplication of 2 complex no's Real part:63 Imag part:99 Do u want to continue(Y/y)y

15

Enter real part:10 Enter imag part:60 Enter real part:37 Enter imag part:77 ********MENU******** 1.Addition 2.Subtraction 3.Multiplication 4.Division .Exit Enter your choice:4 Division of 2 complex no;s Real part:1 Imag part:0.290581 Do u want to continue(Y/y)

16

RESULT:
Thus the c++ program for performing implementation of complex numbers using operator overloading was written and executed successfully.

17

EX NO:

DATE: Program to explain Constructor, Destructor, Copy constructor and Assignment operator overloading

AIM
To write a C++ program to explain the constructor ,destructor ,copy constructor and assignment operator overloading.

ALGORITHM STEP 1: Start the program STEP 2: The header files are declared. STEP 3: A class matrix is declared. STEP 4: A constructor is declared and the values for row,col and m is assigned to be null STEP 5: Using matrix() the memory is allocated. STEP 6: getmatrix() is used to get input. STEP 7: showmatrix() is used to display matrix. STEP 8: Functions for assignment operator overloading and copy constructor is written. STEP 9: In main function object is declared for class matrix and all functions is called using it. STEP 10: Stop the program.

18

PROGRAM CODING
#include<iostream.h> #include<conio.h> class matrix { int **m; int row, col; public: matrix() { row=col=0; m=NULL; } matrix(int r ,int c); ~matrix(); void getmatrix(); void showmatrix(); matrix(matrix &m2); //copy constructor matrix& operator=(matrix &m2); }; matrix::~matrix()

{ 19

for(int i=0;i<row;i++) delete m[i]; delete m; } matrix::matrix(int r ,int c) { row = r; col = c; m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col]; } matrix::matrix(matrix &m2) { cout<<\nCopy constructor invoked\n; row = m2.row; col = m2.col; m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col]; for(i=0;i<row;i++)

20

for(int j=0;j<row;j++) m[i][j]=m2.m[i][j]; } matrix& matrix::operator=(matrix &m2) { cout<<\nAssignment Operator Overloading\n; row = m2.row; col = m2.col; m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col]; for(i=0;i<row;i++) for(int j=0;j<row;j++) m[i][j]=m2.m[i][j]; return *this; } void matrix::getmatrix() { for(int i=0;i<row;i++) for(int j=0; j<col; j++) cin>>m[i][j];

21

} void matrix::showmatrix() { for(int i=0;i<row;i++) { for(int j=0;j<col;j++) cout<<\t<<m[i][j]; cout<<\n; } } void main() { int r,c; clrscr(); cout<<\nEnter rows and cols of the matrix\n; cin>>r>>c; matrix m1(r,c); cout<<\nEnter the matrix elements one by one; m1.getmatrix(); cout<<\nEntered matrix is\n; m1.showmatrix();

//invoking copy constructor

22

matrix m2=m1; cout<<\nResult of copy constructor is\n; m2.showmatrix(); matrix m3; m3=m1; cout<<\nResult of assignment operator overloading\n; m3.showmatrix(); getch(); }

23

OUTPUT
Enter rows and cols of the matrix.. 2 2 Enter the matrix elements one by one..1 2 3 4 Entered matrix is.. 1 3 2 4

Copy constructor invoked... Result of copy constructor is.. 1 3 2 4

Assignment Operator Overloading.. Result of assignment operator overloading.. 1 3 2 4

24

RESULT
Thus the program for explaining constructor, destructor, copy constructor and operator overloading was written and executed successfully.

25

EX NO:

DATE: Overloading new and delete operator using ::new and ::delete

AIM
To write a C++ program to overload NEW and DELETE operator using ::NEW and ::DELETE

ALGORITHM STEP 1: Start the program STEP 2: Header files are declared STEP 3: Class vector is declared STEP 4: read() is used to find the sum of all elements. STEP 5: sum() is used to find the sum of all elements. STEP 6: max() is used to find the maximum of all numbers. STEP 7: Function for invoking operator new and delete is written. STEP 8: In main() object is declared and all functions are called. STEP 9: Stop the program.

26

PROGRAM CODING
#include<iostream.h> #include<conio.h> #include<stdlib.h> class vector { private: int *array; public: void *operator new(size_t size) { vector *v; cout<<\nOperator new invoked; v=::new vector; v->array = new int[size]; if(!v) { cout<<Unable to allocate memory; exit(0); } return v; } void operator delete(void* v) { cout<<\nOperator delete invoked; vector *vp; vp = (vector*) v; delete (int *) vp->array; ::delete v; 27

} void read(int); int max(int); int sum(int); }; void vector::read(int s) { for(int i=0; i<s; i++) { cout<<\nEnter element <<i+1<<:; cin>>array[i]; } } int vector::sum(int s) { int tot=0; for(int i=0; i<s; i++) tot+=array[i]; return tot; } int vector::max(int s) { int max=0; for(int i=0;i<s;i++) if(array[i]>max) max = array[i]; return max; }

28

void main() { int s; clrscr(); cout<<\nEnter how many elements; cin >> s; vector *vec = new vector; cout<<Enter vector data\n; vec->read(s); cout<<\nThe maximum value is <<vec->max(s); cout<<\nThe sum is<< vec->sum(s); delete vec; getch(); }

29

OUTPUT:
Enter how many elements..3 Operator new invoked..Enter vector data.. Enter element 1:5 Enter element 2:12 Enter element 3:7 The maximum value is..12 The sum is..24 Operator delete invoked..

30

RESULT:
Thus the c++ program to overload new and delete operator using ::NEW and ::DELETE operator was written and executed successfully.

31

EX NO: Single linked list using template

DATE:

AIM
To write a c++ program to create a single linked list using template.

ALGORITHM STEP 1: Start the program. STEP 2: The header files are declared. STEP 3: Structure node is declared. STEP 4: A template is declared. STEP 5: A Class list is declared. STEP 6: makeempty() is used to delete all the elements in linked list. STEP 7: insert() is used to insert element in linked list. STEP 8: remove() is used to delete an element in linked list. STEP 9: display() is used to display all the elements. STEP 10: In main() object for class list is declared and using it all
. functions are called appropriate to the case

STEP 11: Stop the program.

32

PROGRAM CODING:
#include<iostream.h> #include<conio.h> #include<stdlib.h> template <class type> struct node { type data; node* next; }; template<class type> class list { public: list(); int length(void) const; void makeempty(void); void insert(void); void remove(void); void display(void); private: node<type>* linklist; int count; 33

}; template <class type> void list<type>::display(void) { node<type>* cur = linklist; cout<<\nThe linked list is\n; while(cur!=NULL) { cout<<cur->data<<->; cur=cur->next; } cout<<NULL\n; } template<class type> list<type>::list() { count=0; linklist=NULL; } template<class type> int list<type>::length(void) const { return count;

34

} template <class type> void list<type>::makeempty(void) { node<type>* temp; while(linklist !=NULL) { temp=linklist; linklist=linklist->next; delete temp; } count=0; cout<<\nNow List is empty; } template <class type> void list<type>::insert(void) { node<type>* temp; type item; cout<<\nEnter the item to insert; cin>>item; temp=new node<type>; temp->data = item;

35

temp->next = linklist; linklist=temp; count++; } template<class type> void list<type>::remove(void) { node<type>* cur = linklist; type item; cout<<\nEnter the item to remove; cin>>item; node<type>* temp; if(item==linklist->data) { temp = cur; linklist=linklist->next; } else { while(!(item==(cur->next->data))) cur=cur->next; temp = cur->next; cur->next = (cur->next)->next;

36

} delete temp; count; } void main() { int ch; list<int> list1; clrscr(); while(1) { cout<<\n Single Linked List Menu\n; cout<<\n 1.Insert \n2.Delete\n 3.Empty\n 4.Exit\n; cout<<\nEnter your Choice ; cin>>ch; switch(ch) { case 1: list1.insert(); list1.display(); break; case 2: if(list1.length()>0)

37

{ list1.remove(); list1.display(); } else cout<<\nList Empty; break; case 3: list1.makeempty(); break; case 4: exit(0); default: cout<<\nInvalid Choice\n; } } }

38

OUTPUT:
The linked list is.. 10->NULL Single Linked List - Menu 1.Insert 2.Delete 3.Empty 4.Exit Enter your Choice..1 Enter the item to insert..20 The linked list is.. 20->10->NULL Single Linked List - Menu 1.Insert 2.Delete 3.Empty 4.Exit

Enter your Choice..2 Enter the item to remove...20 The linked list is.. 10->NULL Single Linked List - Menu 39

1.Insert 2.Delete 3.Empty 4.Exit Enter your Choice..3 Now List is empty.. Single Linked List - Menu 1.Insert 2.Delete 3.Empty 4.Exit Enter your Choice..

40

RESULT:
Thus the c++ program to create a single linked list using templates was written and eecuted successfully. 41

EX NO: Insertion sort using template AIM

DATE:

To perform a c++ program to perform insertion sort using template.

ALGORITHM STEP 1: Start the program. STEP 2: header files are declared. STEP 3: A template is declared. STEP 4: In class insertion get() is used to get input. STEP 5: sort() is used to sort all the elements in ascending order. STEP 6: display() is used to display the sorted array. STEP 7: In main() object is declared for class insertion and it is used to call all the functions. STEP 8: Stop the program.

42

PROGRAM CODING
#include<iostream.h> #include<iomanip.h> #include<conio.h> template <class t> class insertion { t a[25]; public: void get(int); void sort(int); void display(int); }; template <class t> void insertion<t>::get(int n) { int i; cout<<\nEnter the array elements:; for(i=0; i<n;i++) cin>>a[i]; } template <class t> void insertion <t>::display(int n) 43

{ int i; cout<<\n The sorted array is\n; for(i=0;i<n;i++) cout<<a[i]<<setw(10); } template <class t> void insertion <t>::sort(int n) { int i,j; t temp; for(i=1;i<n;i++) { j=i; while(j>=1) { if(a[j]<a[j-1]) { temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; } j;

44

} } } void main() { int n; insertion<int> i1; insertion<float> i2; clrscr(); cout<<\n Insertion Sort on Integer Values; cout<<\n Enter the size of array:\n; cin>>n; i1.get(n); i1.sort(n); i1.display(n); cout<<\n Insertion Sort on Float Values; cout<<\n Enter the size of array:\n; cin>>n; i2.get(n); i2.sort(n); i2.display(n); getch();

45

OUTPUT
Insertion Sort on Integer Values.. Enter the size of array: 3 Enter the array elements:6 4 9 The sorted array is... 4 6 9

Insertion Sort on Float Values... Enter the size of array: 3 Enter the array elements:5.4 9.7 5.1 The sorted array is... 5.1 5.4 9.7

46

RESULT:
Thus the c++ program to perform insertion sort using template was written and executed successfully.

47

EX NO: Bubble Sort using Templates AIM


To write a c++ program to perform bubble sort using templates.

DATE:

ALGORITHM STEP 1: Start the program. STEP 2: The header files are declared. STEP 3: A template is declared. STEP 4: In class insertion get() is used to get input from user. STEP 5: sort() is used to sort all the elemens of array. STEP 6: display() is used to display the sorted array. STEP 7: In main() object for class bubble is created and all functions are called using it. STEP 8: Stop the program.

48

PROGRAM CODING
#include<iostream.h> #include<iomanip.h> #include<conio.h> template <class t> class bubble { t a[25]; public: void get(int); void sort(int); void display(int); }; template <class t> void bubble <t>::get(int n) { int i; cout<<\nEnter the array elements:; for(i=0; i<n;i++) cin>>a[i]; } template <class t> void bubble <t>::display(int n) 49

{ int i; cout<<\n The sorted array is\n; for(i=0;i<n;i++) cout<<a[i]<<setw(10); } template <class t> void bubble <t>::sort(int n) { int i,j; t temp; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } }

50

} void main() { int n; bubble<int> b1; bubble<float> b2; clrscr(); cout<<\n Bubble Sort on Integer Values; cout<<\n Enter the size of array:\n; cin>>n; b1.get(n); b1.sort(n); b1.display(n); cout<<\n Bubble Sort on Float Values; cout<<\n Enter the size of array:\n; cin>>n; b2.get(n); b2.sort(n); b2.display(n); getch(); }

51

OUTPUT:
Bubble Sort on Integer Values.. Enter the size of array: 3 Enter the array elements:6 3 9 The sorted array is.. 3 6 9

Bubble Sort on Float Values.. Enter the size of array: 4 Enter the array elements:2.4 6.9 1.6 4.3 The sorted array is.. 1.6 2.4 4.3 6.9

52

RESULT:
Thus the c++ program to perform bubble sort using template was written and executed successfully.

53

EX NO: Quick sort using templates AIM

DATE:

To write a c++ program to perform quick sort using templates.

ALGORITHM STEP 1: Start the program. STEP 2: The header files are declared. STEP 3: A class quick is declared. STEP 4: A template is also declared. STEP 5: get() is used to get array elements as input. STEP 6: sort() is used to sort all the elements of array. STEP 7: partition() is used to divide the array into sorted and unsorted one. STEP 8: put() is used to display the sorted elements. STEP 9: In main() object for class quick is declared and all functions are called using it. STEP 10: Stop the program.

54

PROGRAM CODING
#include<iostream.h> #include<conio.h> template <class w> class quick { w a[50]; int n; public: void get(); void sort(int,int); int partition(int,int); void put(); }; template <class w> void quick <w>::get() { int i; cout<<\n Enter the no of terms:; cin>>n; cout<<\n Enter the values:\n; for(i=1;i<=n;i++) cin>>a[i]; 55

sort(1,n); } template <class w> void quick <w>::sort(int p,int q) { int j; if(p<q) { j=partition(p,q+1); sort(p,j-1); sort(j+1,q); } } template <class w> int quick <w>::partition(int m,int p) { int i,j,t; w v; v=a[m]; i=m;j=p; do { do

56

i++; while(a[i]<v); do j; while(a[j]>v); if(i<j) { t=a[i]; a[i]=a[j]; a[j]=t; } }while(i<j); a[m]=a[j]; a[j]=v; return j; } template <class w> void quick<w>::put() { int i; for(i=1;i<=n;i++) cout<<a[i]<< ; }

57

void main() { clrscr(); quick<int>q1; quick<float>q2; cout<<\n\t\t QUICK SORT USING TEMPLATES; cout<<\n\t\t ~~~~~ ~~~~ ~~~~~ ~~~~~~~~~; q1.get(); cout<<\n\n Sorted array of integer values:-\n; q1.put(); q2.get(); cout<<\n\n Sorted array of floating values:-\n; q2.put(); getch(); }

58

OUTPUT
QUICK SORT USING TEMPLATES ~~~~~ ~~~~ ~~~~~ ~~~~~~~~~ Enter the no of terms:3 Enter the values: 4 5 2 Sorted array of integer values:245 Enter the no of terms:3 Enter the values: 2.4 1.2 5.6 Sorted array of floating values:1.2 2.4 5.6

59

RESULT
Thus the c++ program to do quick sort using templates was written and executed successfully. 60

EX NO: Merge sort using templates AIM


To write a c++ program to perform merge sort using templates.

DATE:

ALGORITHM STEP 1: Start the program STEP 2: The header files are declared. STEP 3: A class sort is declared. STEP 4: A template is also declared. STEP 5: get() is used to get input from user. STEP 6: merge() is used to divide the array into parts for sorting. STEP 7: mergesort() is used to sort all the elements. STEP 8: display() is used to display all the elements. STEP 9: In main() object is declared and using it all the functions are called STEP 10: Stop the program.

61

PROGRAM CODING
#include<iostream.h> #include<conio.h> #include<iomanip.h> template <class t> class sort { t a[10]; public: void get(int); void merge(int,int); void mergesort(int,int,int); void display(int); }; template <class t> void sort <t>::get(int n) { int i; cout<<\n\n Enter the array elements:; for(i=1;i<=n;i++) cin>>a[i]; } template <class t> 62

void sort <t>::display(int n) { int i; cout<<\n The sorted array is\n; for(i=1;i<=n;i++) cout<<a[i]<<setw(5); } template <class t> void sort <t>::merge(int low,int high) { int mid; if(low<high) { mid=(low+high)/2; merge(low,mid); merge(mid+1,high); mergesort(low,mid,high); } } template <class t> void sort<t>::mergesort(int low,int mid,int high) { t b[10];

63

int h,i,j,k; h=low; i=low; j=mid+1; while((h<=mid)&&(j<=high)) { if(a[h]<=a[j]) { b[i]=a[h]; h=h+1; } else { b[i]=a[j]; j=j+1; } i=i+1; } if(h>mid) { for(k=j;k<=high;k++) { b[i]=a[k];

64

i=i+1; } } else { for(k=h;k<=mid;k++) { b[i]=a[k]; i=i+1; } } for(k=low;k<=high;k++) a[k]=b[k]; } void main() { int n; clrscr(); cout<<\n\t\t MERGE SORT USING TEMPLATES; cout<<\n\t\t ~~~~~ ~~~~ ~~~~~ ~~~~~~~~~; sort<int>n1; sort<float>n2; cout<<\n Enter the array size:;

65

cin>>n; n1.get(n); n1.merge(1,n); n1.display(n); n2.get(n); n2.merge(1,n); n2.display(n); getch(); }

66

OUTPUT
MERGE SORT USING TEMPLATES ~~~~~ ~~~~ ~~~~~ ~~~~~~~~~ Enter the array size:3 Enter the array elements:2 3 1 The sorted array is 1 2 3

Enter the array elements:2.5 3.2 1.4 The sorted array is 1.4 2.5 3.2

67

RESULT
Thus the c++ program to do merge sort using templates was written and executed successfully. 68

EX NO: Stack using exception handling

DATE:

AIM
To implement stack using c++.

ALGORITHM Step 1: Create the class for stack. Step 2: Declare the member function for push, pop,peep and update operation. Step 3: Push operation to insert the element. Step 4: Pop operation to delete the element. Step 5: Peep operation to view the element. Step 6: Update operation to change the element. Step 7: Call the function using switch case. Step 8: Print the result and stop the program.

69

PROGRAM CODING
#include<iostream.h> #include<conio.h> #include<process.h> #define size 20 class stack { private: int st[size]; int top; public: stack() { top=-1; } void push(int data) { if(top==size-1) cout<<"stack is full"; else st[++top]=data; } 70

void pop() { if(top<0) cout<<"stack is empty"; else cout<<"popped element:"<<st[top--]; } void peep(int level) { if(level<=0||level>top+1) cout<<"level does not exist"; else cout<<"element found at level"<<level<<"is:"<<st[(top=1)-level]; } void update(int level,int data) { if(level<=0||level>top+1) cout<<"level does not exist"; else st[(top+1)-level]=data; } void display(); };

71

void stack::display() { int temp=top; while(temp>=0) cout<<st[temp--]<<" "; } void main() { clrscr(); stack s; char choice; int data,level; do { cout<<"\nPress:p(push),i(pop),r(peep),u(update),q(quit):-"; cin>>choice; switch(choice) { case 'p':cout<<"\n Enter the data:"; cin>>data; s.push(data); break; case 'i':

72

s.pop(); break; case 'r': cout<<"\nEnter the level no for access:"; cin>>level; s.peep(level); continue; case 'u': cout<<"\n Enter the level no to be updated:"; cin>>level; cout<<"\nEnter the new value:"; cin>>data; s.update(level,data); break; case 'q': exit(0); default: cout<<"\n wrong key pressed:"; continue; } cout<<"\nstack after the operation performed:"; s.display(); } while(1);

73

OUTPUT
Press:p(push),i(pop),r(peep),u(update),q(quit):-p Enter the data:5 stack after the operation performed:5 Press:p(push),i(pop),r(peep),u(update),q(quit):-p Enter the data:6 stack after the operation performed:6 5 Press:p(push),i(pop),r(peep),u(update),q(quit):-p Enter the data:7 stack after the operation performed:7 6 5 Press:p(push),i(pop),r(peep),u(update),q(quit):-i popped element:7 stack after the operation performed:6 5 Press:p(push),i(pop),r(peep),u(update),q(quit):-r Enter the level no for access:2 element found at level2is:2 Press:p(push),i(pop),r(peep),u(update),q(quit):-u Enter the level no to be updated:2 Enter the new value:8 stack after the operation performed:6 8 Press:p(push),i(pop),r(peep),u(update),q(quit):-q

74

RESULT
Thus stack is implemented to perform LIFO (Last In First Out) operation.

75

EX NO: Queue using exception handing AIM


To implement queue using c++.

DATE:

ALGORITHM Step 1: Create the class for queue. Step 2: Declare the member function for insert, delete, peep and update operation. Step 3: Insert operation to insert the element at front end. Step 4: Delete operation to delete the element at rear end. Step 5: Peep operation to view the element. Step 6: Update operation to change the element. Step 7: Call the function using switch case. Step 8: Print the result and stop the program.

76

PROGRAM CODING
#include<iostream.h> #include<conio.h> #include<process.h> #define sz 20 enum boolean {false,true}; class queue { private: int a[sz],front,rear; public: queue() { front=rear=0; } void insert(int data) { if(rear>sz) cout<<"\nQueue Overflow"; else { a[++rear]=data; if(rear==1) 77

front=rear; } } void deletee(); void peep(int); void update(int,int); void display(); boolean empty() { return((front==0)?true:false); } }; void queue::deletee() { if(front<1) cout<<"\nQueue underflow"; else cout<<"\nDelete element:"<<a[front++]; if(front>rear) front=rear=0; } void queue::peep(int level) {

78

int move=front; while(move!=level&&move++<=rear); if(move>rear) cout<<"level does not exist"; else cout<<"Element found at level"<<level<<"is:"<<a[move]; } void queue::update(int level,int data) { int move=front; while(move!=level&&move++<=rear); if(move>rear) cout<<"level does not exist;"; else a[move]=data; } void queue::display() { if(front>0) { int move=front; while(move<=rear) cout<<a[move++]<<' ';

79

} } void main() { clrscr(); queue q; char choice; int data,level; do { cout<<"\nPress:i(insert),d(delete),p(peep),u(update),q(quit):-"; cin>>choice; switch(choice) { case 'i': cout<<"\nEnter the data"; cin>>data; q.insert(data); break; case 'd': q.deletee(); break; case 'p':

80

if(q.empty()) cout<<"\nqueue is empty"; else cout<<"\nEnter the level no. for access:"; cin>>level; q.peep(level); continue; case 'u': if(q.empty()) { cout<<"\nqueue is empty"; continue; } cout<<"\nEnter the level no.to be updated:"; cin>>level; cout<<"\nEnter the new value:"; cin>>data; q.update(level,data); break; case 'q': exit(0); default: cout<<"\nWrong key pressed";

81

continue; } cout<<"\nQueue after the operation performed:"; q.display(); }while(1); }

82

OUTPUT
Press:i(insert),d(delete),p(peep),u(update),q(quit):-i Enter the data4 Queue after the operation performed:4 Press:i(insert),d(delete),p(peep),u(update),q(quit):-i Enter the data5 Queue after the operation performed:4 5 Press:i(insert),d(delete),p(peep),u(update),q(quit):-i Enter the data4 Queue after the operation performed:4 5 4 Press:i(insert),d(delete),p(peep),u(update),q(quit):-d Delete element:4 Queue after the operation performed:5 4 Press:i(insert),d(delete),p(peep),u(update),q(quit):-p Enter the level no. for access:2 Element found at level2is:5 Press:i(insert),d(delete),p(peep),u(update),q(quit):-u Enter the level no.to be updated:2 Enter the new value:6 Queue after the operation performed:6 4 Press:i(insert),d(delete),p(peep),u(update),q(quit):-q

83

RESULT
Thus queue is implemented to perform FIFO (First In First Out) operation.

84

EX NO: Implementation Of Graph

DATE:

AIM:
To implement the graph and to obtain a minimum cost spanning tree.

ALGORITHM: Step 1: Create two classes as POINT and ARC. Step 2: Define a Graph class which represents the collection of Point and Arc
Objects.

Step 3: Write a method to find a minimum cost spanning tree in a graph. Step 4: The method calculate minimum distance from the root node to all other
Nodes in a directed weighted graph.

Step 5: Print the result which consists of vertices, minimum distance and previous
Node.

Step 6: Stop the program.

85

PROGRAM CODING
#include<iostream.h> #include<conio.h> class point { protected: int n; }; class arc { protected: int b[10][10]; }; class graph1:public point,arc { int v[10],pv[10],k[10],dv[10]; public: void get(); void shortpath(); void put(); }; void graph1::get() { 86

int i,j; cout<<"\nEnter the no of vertices"; cin>>n; cout<<"\nInput matrix form of arc"; for(i=0;i<n;i++) for(j=0;j<n;j++) cin>>b[i][j]; for(i=0;i<n;i++) { v[i]=i+1; pv[i]=0; dv[i]=88; k[i]=0; } } void graph1::shortpath() { int i=0,j,c=0,m,f; dv[i]=0; for(;;) { k[i]=1; for(j=0;j<n;j++)

87

{ if(b[i][j]!=0&&b[i][j]!=99) { c=b[i][j]; if(c<dv[j]) { pv[j]=v[i]; dv[j]=c; } } } f=1; c=88; for(m=0;m<n;m++) { if(k[m]==0) { f=0; if(dv[m]<c&&dv[m]!=0) { c=dv[m]; i=m; } } }

88

if(f==1) return; } } void graph1::put() { int i; cout<<"\nver known dv pv\n"; for(i=0;i<n;i++) cout<<"\nv"<<v[i]<<" "<<k[i]<<" "<<dv[i]<<" v"<<pv[i]; } void main() { clrscr(); graph1 g1; g1.get(); g1.shortpath(); g1.put(); getch(); }

89

OUTPUT
Enter the no of vertices7 Input matrix form of arc 99 2 4 1 0 0 0 2 99 0 3 10 0 0 4 0 99 2 0 5 0 1 3 2 99 7 8 4 1 10 0 7 99 0 6 0 0 5 8 0 99 1 0 0 0 4 6 1 99

ver known dv pv v1 1 v2 1 v3 1 v4 1 v5 1 v6 1 v7 1 0 0 2 v1 2 v4 1 v1 6 v7 1 v7 1 v6

90

RESULT
Thus the concept of graph along with minimum spanning tree is implemented.

91

EX NO: Run Time Polymorphism

DATE:

AIM
To write a c++ program to implement virtual function or run time polymorphism.

ALGORITHM Step 1: Start the program. Step 2: Create a base class shape, with two member variable in double and two member
Functions getdata() and displayarea() .

Step 3: Create a derived class triangle, rectangle and circle from the base class shape.
The member function of the derived classes is displayarea().

Step 4: Declare display area as virtual function. Step 5: In the main function call the member function of the base class and derived
Classes using base class object as pointer.

Step 6: In display area function display the area of the circle, triangle and rectangle. Step 7: Stop the program.

PROGRAM CODING
92

#include<iostream.h> #include<conio.h> class shape { protected: double x; double y; public: void getdata(double x1,double y1) { x=x1; y=y1; } virtual void display() { } }; class triangle:public shape { double a1; public: void display(); };

93

class rectangle:public shape { double a2; public: void display() { a2=x*y; cout<<"\nArea of rectangle:"; cout<<a2; } }; class circle:public shape { double a3; public: void display() { a3=3.14*x*x; cout<<"\n Area of the circle:"; cout<<a3; } }; void triangle::display()

94

{ a1=0.5*x*y; cout<<"\nArea of the triangle"; cout<<a1; } void main() { double ba,h; shape *ptr; clrscr(); triangle t; rectangle r; circle c; cout<<"\n Enter two values for area calculation:"; cin>>ba>>h; t.getdata(ba,h); r.getdata(ba,h); c.getdata(ba,0); ptr=&t; ptr->display(); ptr=&r; ptr->display(); ptr=&c;

95

ptr->display(); getch(); }

OUTPUT
96

Enter two values for area calculation: 12 5

Area of the triangle30 Area of rectangle:60 Area of the circle:452.16

97

RESULT
Thus the virtual base class function is implemented in c++ program and area of geometric figures are calculated.

98

EX NO: FILE HANDLING AIM

DATE:

To implement the program that randomly generates complex numbers and write them two per line in a file along with an operator. Write another program to read one line at a time from this file, perform the corresponding operation on the two complex numbers and write the result to another file.

ALGORITHM Step 1: Start the program. Step 2: Generate complex numbers randomly using class complex. Step 3: Open a file input.c in input, append and truncate mode. Step 4: Write the complex numbers two per line in a file along with an operator like +,-,*,/ and
close the open file.

Step 5: Print the result and end the program. Step 6: Start a new program which open the file input.c in open mode. Step 7: Read the content of the file line by line. Step 8: Perform addition, subtraction, multiplication or division depending upon the in between
operator and store the result.

Step 9: Open a file output.c in a input, append and truncate mode. Step 10: Write the result in output.c file.
99

Step 11: print the result and stop the program.

PROGRAM CODING PROGRAM 1


#include<iostream.h> #include<conio.h> #include<string.h> #include<fstream.h> class complex { public: float rl; float img; void getdata() { cout<<"\nEnter real and imagenary part of a complex no:"; cin>>rl>>img; } void putdata() { cout<<"\ncomplex no:"; cout<<"\n"<<rl<<"+("<<img<<"i)"; } 100

}; void main() { int i,j; char c[20]; char a[5]; complex cx[12]; clrscr(); cout<<"get 10 complex nos:";

for(i=1;i<=10;i++) { cx[i].getdata(); } cout<<"\nthe complex nos:"; for(i=1;i<=10;i++) { cx[i].putdata(); } fstream file; file.open("input.c",ios::in|ios::trunc|ios::app); j=0; a[0]='+';

101

a[1]='-'; a[2]='*'; a[3]='/';

for(i=1;i<=10;i=i+2) { cout<<"{"<<cx[i].rl<<"+("<<cx[i].img<<"i)}"; cout<<a[j]; cout<<"{"<<cx[i+1].rl<<"+("<<cx[i+1].img<<"i)}"; file<<"{"<<cx[i].rl<<"+("<<cx[i].img<<"i)}"; file<<a[j]; file<<"{"<<cx[i+1].rl<<"+("<<cx[i+1].img<<"i)}\n"; j++; if(j>=4) j=0; } file.close(); getch(); }

102

OUTPUT 1
File name: Input.c {2+(2i)}+{2+(2i)} {3+(3i)}-{1+(1i)} {2+(2i)}*{1+(1i)} {4+(5i)}/{2+(3i)} {1+(1i)}+{1+(1i)}

103

PROGRAM 2
#include<iostream.h> #include<conio.h> #include<string.h> #include<fstream.h> #include<stdlib.h> #include<math.h> void main() { int i,m,k,j; float rl[10][4],im[10][4]; char tx[20][25],ch,wd[10],ar[10]; ifstream in; clrscr(); in.open("input.c"); if(!in) { cout<<"no file"; return; } in.seekg(0); 104

i=0; j=0; while(in) { in.get(ch); cout<<ch; if(ch!=NULL) { tx[j][i]=ch; i++; } if(ch=='\n') { tx[j][i]=NULL; j++; i=0; } } tx[j][i]=NULL; for(i=0;i<5;i++) { j=0; k=0;

while(k<2)

105

{ m=0; if(tx[i][j]=='{') { while(tx[i][j+1]!='+') { wd[m]=tx[i][j+1]; j++; m++; } wd[j]='\n'; rl[i][k]=atof(wd); j=j+2; } m=0; if(tx[i][j]=='(') { while(tx[i][j+1]!='i') { wd[m]=tx[i][j+1]; m++; j++; }

106

wd[m]='\n'; im[i][k]=atof(wd); } while(tx[i][j]!='}') j++; if(tx[i][j]=='}'&&tx[i][j+2]=='{') { ar[i]=tx[i][j+1]; j=j+2; } k=k+1;

while(tx[i][j]!='{') { j++; } } if(ar[i]=='+') { rl[i][2]=rl[i][0]+rl[i][1]; im[i][2]=im[i][0]+im[i][1]; } else if(ar[i]=='-')

107

{ rl[i][2]=rl[i][0]-rl[i][1]; im[i][2]=im[i][0]-im[i][1]; } else if(ar[i]=='*') { rl[i][2]=rl[i][0]*rl[i][1]-im[i][0]*im[i][1]; im[i][2]=rl[i][0]*im[i][1]+im[i][0]*rl[i][1]; } else if(ar[i]=='/') { float qt; qt=rl[i][1]*rl[i][1]+im[i][1]*im[i][1]; rl[i][2]=(rl[i][0]*rl[i][1]+im[i][0]*im[i][1])/qt; im[i][2]=(im[i][0]*im[i][1]-rl[i][0]*im[i][1])/qt; } cout<<rl[i][2]; if(im[i][2]<0) cout<<"-i"; else cout<<"+i"; cout<<fabs(im[i][2])<<endl; }

108

fstream out; out.open("output.c",ios::out|ios::trunc|ios::app); for(i=0;i<5;i++) { out<<rl[i][2]; if(im[i][2]<0) out<<"-i"; else out<<"+i"; out<<fabs(im[i][2])<<endl; } out.close(); getch(); }

109

OUTPUT 2
File name: output.c 4+i4 2+i2 0+i4 1.769231+i0.230769 2+i2

110

RESULT
Thus the c++ program for file handling to read and write the content in files was implemented and executed.

111

Potrebbero piacerti anche