Вы находитесь на странице: 1из 25

NAME: REGNO:

// MATRIX ADDITION USING STATIC DEFAULT ARGUMENT AND


FRIEND FUNCTION.//

#include<iostream.h>

#include<conio.h>

class matrix

private:

int a[5][5],b[5][5],c[5][5];

static int m,n;

public:

void readmatrix();

void display(int [5][5]);

friend void addmatrix(matrix,int=2,int=2);

};

int matrix::m;

int matrix::n;

void matrix::readmatrix()

cout<<”\n ENTER A MATRIX”<<endl;

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)
cin>>a[i][j];

cout<<”\n ENTER B MATRIX”<<endl;

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

cin>>b[i][j];

void matrix::display(int x[5][5])

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

cout<<x[i][j]<<”\t”;

cout<<”\n”;

void addmatrix(matrix mobj,int r,int c)

mobj.m=r;

mobj.n=c;

mobj.readmatrix();

for(int i=0;i<r;i++)

for(int j=0;j<c;j++)

mobj.c[i][j]=mobj.a[i][j]+mobj.b[i][j];
clrscr();

cout<<”\n THE GIVEN A MATRIX ARE”<< endl;

mobj.display(mobj.a);

cout<<”\n THE GIVEN B MATRIX ARE”<<endl;

mobj.display(mobj.b);

cout<<”\n THE MATRIX C=A+B ARE”<<endl;

mobj.display(mobj.c);

void main()

matrix m;

clrscr();

addmatrix(m,2,2);

getch();

}
SAMPLE OUTPUT:

ENTER THE A MATRIX:

2 6

8 5

ENTER THE B MATRIX:

7 1

3 9

SAMPLE OUTPUT:

THE GIVEN A MATRIX IS:

2 6

8 5

THE GIVEN B MATRIX IS:

7 1

3 9

THE MATRIX C=A+B IS:

9 7

11 14
NAME : REG.NO :

\\ IMPLEMENTATION OF LINKED LIST USING TEMPLATES \\

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

template<class T>

class list

struct node

T data;

node *link;

}*p;

public:

void inlast(T);

void deleteelement(T);

void disp();

list()

p=NULL;

~list();
};

template<class T>

void list <T>::inlast(T x)

node *q,*t;

if(p==NULL)

p=new node;

p->data=x;

p->link=NULL;

else

q=p;

while(q->link!=NULL)q=q->link;

t=new node;

t->data=x;

t->link=NULL;

q->link=t;

cout<<"\n Inserted successfully at the end";

disp();

}
template <class T>

void list <T>::deleteelement(T x)

node *q,*r;

q=p;

if(q->data==x)

p=q->link;

delete q;

return;

r=q;

while(q!=NULL)

if(q->data==x)

r->link=q->link;

delete q;

return;

r=q;

q=q->link;

}
cout<<"\n Element you entered"<<x<<"is not found";

template <class T>

list <T>::~list()

node *q;

if(p==NULL)

return;

while(p!=NULL)

q=p->link;

delete p;

p=q;

template <class T>

void list <T>::disp()

node *q;

q=p;

if(q==NULL)

cout<<"\n No data is in the list"<<endl;


return;

cout<<"\n The item present in the list are: "<<endl;

while(q!=NULL)

cout<<"\t"<<q->data;

q=q->link;

void main()

list<int>l;

int ch,v,p,ps;

do

clrscr();

cout<<"\n Operation on list using numbers";

cout<<"\n 1.Insertion 2.Deletion 3.Display 4.Exit";

cout<<"\n enter your choice:";

cin>>ch;

switch(ch)

case 1:
cout<<"\n Enter the integer value to insert:";

cin>>v;

l.inlast(v);

break;

case 2:

l.disp();

cout<<"\n enter the element to be deleted:";

cin>>v;

l.deleteelement(v);

cout<<"\n The list after deletion:";

l.disp(); break;

case 3:

l.disp(); break;

case 4: break;

default:

cout<<"\n The option is invalid";

return;}

getch();}

while(ch!=4);

getch();

return;

}
SAMPLE OUTPUT:

Operations on the list using number:

1.Insertion

2.Deletion

3.Display

4.Exit

Enter your choice: 1

Enter the integer value to insert: 7

Inserted successfully at the end

The item present in the list are: 7

Enter your choice: 1

Enter the integer value to insert: 28

Inserted successfully at the end

The item present in the list are: 7 28

Enter your choice: 2

The item present in the list are:7 28

Enter the element to be deleted: 28

The list after deletion

The item present in the list are: 7

Enter your choice: 4


NAME : REG.NO :

/* IMPLEMENATION OF TEMPLATES USING BUBBLE SORT


ALGORITHM*/

#include<iostream.h>

#include<conio.h>

template<class T>

void bubble_sort( T a[],int n){

for(int i=0;i<n-1;i++)

for(int j=i;j<n;j++)

if(a[i]>a[j]) {

T temp=a[i];

a[i]=a[j];

a[j]=temp; }}

template <class T>

void display(T a[],int n) {

for(int i=0;i<n;i++)

cout<<a[i]<<"\t";}

void main() {

int a[10],i,n;

float rank[10];

char c[5];

clrscr();
cout<<"\nEnter the size of list"<<endl;

cin>>n;

cout<<"\nEnter the list of numbers"<<endl;

for(i=0;i<n;i++)

cin>>a[i];

cout<<"\nEnter the list of ranks"<<endl;

for(i=0;i<n;i++)

cin>>rank[i];

cout<<"\nEnter the any 5 character"<<endl;

for(i=0;i<n;i++)

cin>>c[i];

bubble_sort(a,n);

cout<<"\n The sorted numbers are"<<endl;

display(a,n);

bubble_sort(rank,n);

cout<<"\n the sorted ranks are"<<endl;

display(rank,n);

bubble_sort(c,n);

cout<<"\n the sorted characters are"<<endl;

display(c,n);

getch();

}
SAMPLE OUTPUT :

Enter your five numbers:

2 4 8 1 5

Enter any five fraction values:

5.5 1.5 3.5 4.5 2.5

Enter any five characters

O O P S S

The sorted integer,float,and characters:

1.5

2.5

3.5

4.5

5.5

S
NAME : REG.NO :

/* IMPLEMENTATION OF COMPLEX NUMBER CLASS WITH


NECESSARY OPERATOROVERLODING AND TYPE CONVERSIONS
SUCH AS INTEGER TO COMPLEX ANDCOMPLEX TO INTEGER */

#include<iostream.h>

#include<conio.h>

class complex

private:

float real;

float imag;

public:

complex()

complex(float x)

real=imag=x;

complex(float x,float y)

real=x;

imag=y;
}

complex operator +(complex c2);

void display()

cout<<"\n"<<real<<"+i"<<imag;

operator int()

int real;

cout<<"\n enter the integer value"<<endl;

cin>>real;

return(real);

};

complex complex::operator +(complex c2)

complex temp;

temp.real=real+c2.real;

temp.imag=imag+c2.imag;

return(temp);

void main()

{
clrscr();

complex c1(3.4,2.3),c2(4.5,2.6),c3;

c3=c1+c2;

c3.display();

int x;

complex c4;

cout<<"\n enter an integer value"<<endl;

cin>>x;

c4=x; //conversion of integer to complex

c4.display();

x=c4; //conversion of complex to integer

cout<<"\n x value="<<x;

getch();

}
SAMPLE OUTPUT:

3.4+i2.3

4.3+i2.3

7.7+i4.6

Enter a integer value:

2+i2

X value: 2
NAME : REG.NO :

/* IMPLEMENTATION OF MATRIX CLASS WITH DYNAMIC


ALLOCATION,CONSTRUCTOR, DESTRUCTOR AND OVERLOADING
OF +,* AND = OPERATOR */

#include<iostream.h>

#include<conio.h>

class matrix{

int **a;

public:

matrix() {

a=new int *[10]; }

~matrix() {}

void readmatrix();

void printmatrix();

void operator=(matrix);

matrix operator+(matrix);

matrix operator*(matrix); };

matrix matrix::operator+(matrix m){

matrix t;

for(int i=0;i<2;i++)

for(int j=0;j<2;j++)

t.a[i][j]=a[i][j]+m.a[i][j];
return(t);}

matrix matrix::operator*(matrix m){

matrix t;

for(int i=0;i<2;i++)

for(int j=0;j<2;j++) {

t.a[i][j]=0;

for(int k=0;k<2;k++)

t.a[i][j]+=a[i][k]+m.a[k][j]; }

return(t);}

void matrix::operator=(matrix m){

for(int i=0;i<2;i++)

for(int j=0;j<2;j++)

a[i][j]=m.a[i][j];

void matrix::readmatrix()

for(int i=0;i<2;i++)

for(int j=0;j<2;j++)

cin>>a[i][j];}

void matrix::printmatrix()

for(int i=0;i<2;i++)

{
cout<<"\n";

for(int j=0;j<2;j++)

cout<<a[i][j]<<"\t"; }}

void main(){

clrscr();

matrix a,b,c;

cout<<"\n enter 2*2 matrix";

a.readmatrix();

cout<<"\n enter 2*2 matrix";

b.readmatrix();

c=a+b;

cout<<"\n the output a+b=";

c.printmatrix();

c=a*b;

cout<<"\n the output a*b";

c.printmatrix();

c=(a+b)*(a+b);

cout<<"\n the output a+b*a+b=";

c.printmatrix();

cout<<"\n the output a=b are:"; a=b;

a.printmatrix();

getch();

}
SAMPLE OUTPUT :

Enter 2*2 A matrix

2 2

2 2

Enter 2*2 B matrix

2 2

2 2

The output of a+b

4 4

4 4

The output of a*b

8 8

8 8

The output of(a+b)*(a+b)

32 32

32 32

The output ofa=a+b(Or) a+=b

4 4

4 4
NAME : REG.NO :

/*OVERLOADING OF NEW AND DELETE OPERATOR TO FIND SUM OF


N NUMBERS*/

#include<iostream.h>

#include<conio.h>

class number

private:

int *a;

public:

void* operator new(size_t size) //size_t Type used for memory


object size {

number *x;

x=::new number;//it refers to global new

x->a=new int[10];

return x; }

void operator delete(void* num)

number *n=(number*)num;

delete (int*)n->a;

::delete num;

void read();
int sum();

};

void number::read()

for(int i=0;i<5;i++)

cin>>a[i];

int number::sum()

int sum=0;

for(int i=0;i<5;i++)

sum+=a[i];

return sum;}

void main()

number *n=new number;

clrscr();

cout<<"\nEnter 5 vlaues"<<endl;

n->read();

cout<<"\nsum of numbers="<<n->sum();

delete n;

getch();

}
SAMPLE OUTPUT

enter 5 values

sum of numbers=15

Вам также может понравиться