Sei sulla pagina 1di 28

# If there are images in this attachment, they will not be displayed.

*Program-1*
*Object:- WAP for matrix multiplication using templates and dynamic *
* memory allocation*.
*Coding:-*
#include<iostream.h>
#include<conio.h>
template <class T>
class matrix
{
T **p;
int m,n;
public:
matrix();
matrix(matrix<T> &,matrix<T> &);
void set_mat();
void disp_mat();
void mul_mat(matrix<T>,matrix<T>);
};
template <class T>
matrix<T>::matrix()
{
cout<<endl<<"ENTER NO. OF ROWS FOR THE MATRIX: ";
cin>>m;
cout<<endl<<"ENTER NO. OF COLUMNS FOR THE MATRIX: ";
cin>>n;
p=new T *[m];
for(int i=0;i<m;i++)
p[i]=new T[n];
}
template <class T>
matrix<T>::matrix(matrix<T> &m1,matrix<T> &m2)
{
this->m=m1.m;
this->n=m2.n;
p=new T *[m];
for(int i=0;i<m;i++)
p[i]=new T[n];
for(i=0;i<m;i++)
for(int j=0;j<n;j++)
p[i][j]=0;
}
template <class T>
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
cout<<endl<<"ENTER ELEMENT ["<<i+1<<","<<j+1<<"]: ";
cin>>p[i][j];
}
}
template <class T>
void matrix <T> :: disp_mat()
{
cout<<endl<<"MATRIX ELEMENTS ARE:"<<endl<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cout<<p[i][j]<<" \t";
cout<<endl;
}
}
template <class T>
void matrix <T>:: mul_mat(matrix<T> m1,matrix<T> m2)
{
if(m1.n!=m2.m)
cout<<endl<<"MATRICES ARE NOT COMPATIBLE FOR MULTIPLICATION. "<<endl;
else
{
for(int i=0;i<m1.m;i++)
for(int j=0;j<m1.n;j++)
for(int k=0;k<m2.n;k++)
p[i][j]+=m1.p[i][k]*m2.p[k][j];
cout<<endl<<endl<<"RESULT OF MULTIPLICATION OF TWO MATRICES
IS:"<<endl;
disp_mat();
}}
int main()
{
clrscr();
matrix <float> m1;
matrix <float> m2;
m1.disp_mat();
m2.disp_mat();
matrix <float> m3(m1,m2);
m3.mul_mat(m1,m2);
getch();
return 0;
}
*Program-2*
*Object: - WAP for selection sorting using constructor deconstructor in *
* C++. *
*Coding:-*
#include<iostream.h>
#include<conio.h>
class selectionsort
{int *x,items;
public:
selectionsort(int);
void input(int []);
void display();
void sort();
~selectionsort();
};
selectionsort::selectionsort(int n)
{
items=n;
x=new int[items];
}
selectionsort::~selectionsort()
{
delete[]x;
}
void selectionsort::input(int a[])
{
for(int i=0;i<items;++i)
x[i]=a[i];
}
void selectionsort::display()
{
cout<<"\nSorted elements are:\n";
for(int i=0;i<items;++i)
cout<<" "<<x[i];
}
void selectionsort::sort()
{
for(int j=0;j<items-1;++j)
{
int lowest=j;
for(int k=j+1;k<items;++k)
{
if(x[k]<x[lowest])
lowest=k;
}
int temp;
temp=x[j];
x[j]=x[lowest];
x[lowest]=temp;
} }
void main()
{ int elements[100],newitems;
clrscr();
cout<<"\nEnter how many no. you want\n";
cin>>newitems;
cout<<"\nEnter "<<newitems<<" elements"<<endl;
for(int i=0;i<newitems;++i)
cin>>elements[i];
selectionsort obj(newitems);
obj.input(elements);
obj.sort();
obj.display();
getch();
}
*Program-3*
*Object: - WAP for Bubble sorting using constructor deconstructor in *
* C++*.
*Coding:-*
#include<iostream.h>
#include<conio.h>
class bubblesort
{ private:
int x;
int items;
public:
bublesort(int);
~bubblesort();
void input(int[]);
void display();
void sort();
};
bubblesort::bubblesort(int n)
{
items=n;
x=new int[items];
}
bubblesort::~bubblesort()
{
delete[]x;
}
void bubblesort::input(int a[])
{
for(int i=0;i<items;i++)
x[i]=a[i];
}
void bubblesort::dislay()
{
cout<<"\n sorted elements are:\n";
for(int i=0;i<items;i++)
cout<<x[i];
}
void bublesort::sort()
{
int swap=1;
for(int i=0;i<items&swap==1;i+)
{
swap=0;
for(int j=0;j<items-[i+1];++i)
if(x[j]>x[j+1])
{
int temp;
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
swap=1;
}}}
void main()
{
int elements[100],noofitems;
cout<"\nenter how many elements do u want to feed\n";
cin>>noofitems;
cout<<"\n enter"<<noofitems<<"elements";
for(int i=0;i<noofitems;++i)
cin>>elements[i];
bublesort.obj(noofitems);
bubblesort.obj.input(elements);
bublesort.obj.sort();
bubblesort.obj.display();
}

*Program-4*
*Object: - WAP C++ Object Oriented code for Huffman coding & *
* Decoding.*
*Coding:-*
#include<iostream.h>
#include<string.h>
#include<.h>
#include<stdlib.h>
#include<conio.h>
struct tree
{
char a[20];
int s;
struct tree *left,*right;
}*root=NULL,*tt[20]={NULL},*temp,*temp2,*t2,*ri,*le;
struct pqu
{
int info;
char a[20];
struct pqu *ptr;
}*front=NULL,*t,*par,*t1,*p1,*p2;
struct pqu* fp(int info)
{
struct pqu *p=NULL;
for(t1=front;t1->info<info&&t1!=NULL;t1=t1->ptr)
{
p=t1;
}
return (p);
}
void enqu(char a[20],int p)
{
t=(struct pqu*)malloc(sizeof(struct pqu));
strcpy(t->a,a);
t->info=p;
t->ptr=NULL;
if(front==NULL)
{
front=t;
}
else
{
par=fp(p);
if(par==NULL)
{
t->ptr=front;
front=t;
}
else
{
t->ptr=par->ptr;
par->ptr=t;
}
}
}
struct pqu* dequ()
{
t1=front;
front=front->ptr;
return t1;
}
void info(char c[2])
{
int m=0,i;
temp2=root;
while(strcmp(c,temp2->a)!=0)
{
t2=temp2->left;
for(i=0;i<strlen(t2->a);i++)
{
if(t2->a[i]==c[0])
{
temp2=temp2->left;
m=1;
cout<<"0";
break;
}
}
if(m!=1)
{
temp2=temp2->right;
cout<<1;
}
m=0;
} }
void insert()
{
char a1[20],b1[20],v1[20];
int i,j,z=0,l;
while(front!=NULL)
{
p1=dequ();
strcpy(a1,p1->a);
l=p1->info;
p2=dequ();
if(p2==NULL)
break;
strcpy(b1,p2->a);
strcpy(v1,a1);
temp=(struct tree*)malloc(sizeof(struct tree));
strcpy(temp->a,strcat(v1,b1));
temp->s=l+p2->info;
temp->left=NULL;
temp->right=NULL;
temp2=temp;
root=temp;
for(i=0;i<z;)
{
if(strcmp(tt[i]->a,a1)==0)
{
temp->left=tt[i];
for(l=i;l<z;l++)
{
tt[l]=tt[l+1];
}
i=0;
continue;
}
else if(strcmp(tt[i]->a,b1)==0)
{
temp->right=tt[i];
for(l=i;l<z;l++)
{
tt[l]=tt[l+1];
}
i=0;
continue;
}
i++;
}
if(temp->left==NULL)
{
le=(struct tree*)malloc(sizeof(struct tree));
strcpy(le->a,a1);
le->left=NULL;
le->right=NULL;
temp2->left=le;
}
if(temp->right==NULL)
{
ri=(struct tree*)malloc(sizeof(struct tree));
strcpy(ri->a,b1);
ri->left=NULL;
ri->right=NULL;
temp2->right=ri;
}
if(front!=NULL)
enqu(temp2->a,temp2->s);
tt[z++]=temp2;
}
}
void disp(struct tree *rt)
{
if(rt!=NULL)
{
disp(rt->left);
cout<<"
"<<rt->a;
disp(rt->right);
}
}
void main()
{
textmode(MONO);
int i=0,g,h,p,y,n;
char m[20],b[20][2],re;
while(1)
{
clrscr();
cout<<"=========================";
cout<< HUFFMAN CODING";
cout<<"===================================";
cout<<"Enter the total no of characters : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"
Enter the character : ";
cin>>m;
strcpy(b[i],m);
cout<<"
Enter frequency for "<<m<<" : ";
cin>>g;
enqu(m,g);
}
insert();
disp(root);
clrscr();
cout<<"==========================================";
cout<<"The corresponding codes are.....";
cout<<"===========================================";
for(i=0;i<n;i++)
{
cout<<""<<b[i]<<" : ";
info(b[i]);
}
cout<<"DO YOU WANT TO CONTINUE Y OR N: ";
cin>>re;
if(re=='y'||re=='Y')
continue;
else
exit(0);
}
}

*Program-5*
*Object: - WAP C++ Object Oriented code for represent a Graph *
search *
* starting from any node. *
*Coding:-*
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
void create(); // For creating a graph
void dfs(); // For Deapth First Search(DFS) Traversal.
void bfs(); // For Breadth First Search(BFS) Traversal.
struct node // Structure for elements in the graph
{
int data,status;
struct node *next;
};
{
struct node *next;
};
struct node *start,*p,*q;
int main()
{
int choice;
clrscr();
create();
while(1)
{
cout<<"-----------------------";
cout<<"1: DFS2: BSF3: ExitEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
dfs();
break;
case 2:
bfs();
break;
case 3:
exit(0);
break;
default: cout<<Incorrect choice! Re-enter your choice.";
getch();
}
}
return 0;
}
void create() // Creating a graph
{
int dat,flag=0;
start=NULL;
cout<<"Enter the nodes in the graph(0 to end): ";
while(1)
{
cin>>dat;
if(dat==0)
break;
p=new node;
p->data=dat;
p->status=0;
p->next=NULL;
if(flag==0)
{
start=p;
q=p;
flag++;
}
else
{
q->next=p;
q=p;
}
}
p=start;
while(p!=NULL)
{
cout<<"Enter the links to "<<p->data<<" (0 to end) : ";
flag=0;
while(1)
{
cin>>dat;
if(dat==0)
break;
if(flag==0)
{
l=k;
flag++;
}
else
{
l=k;
}
q=start;
while(q!=NULL)
{
if(q->data==dat)
k->next=q;
q=q->next;
}
}
p=p->next;
}
cout<<"-------------------------";
return;
}

## // Deapth First Search(DFS) Traversal.

void bfs()
{
int qu[20],i=1,j=0;
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
qu[0]=p->data;
p->status=1;
while(1)
{
if(qu[j]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==qu[j])
break;
p=p->next;
}
while(k!=NULL)
{
q=k->next;
if(q->status==0)
{
qu[i]=q->data;
q->status=1;
qu[i+1]=0;
i++;
}
}
j++;
}
j=0;
cout<<"---------------------------";
while(qu[j]!=0)
{
cout<<qu[j]<<" ";
j++;
}
getch();
return;
}

## // Breadth First Search(BFS) Traversal.

void dfs()
{
int stack[25],top=1;
cout<<"Deapth First Search Results";
cout<<"---------------------------";
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
stack[0]=0;
stack[top]=p->data;
p->status=1;
while(1)
{
if(stack[top]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==stack[top])
break;
p=p->next;
}
cout<<stack[top]<<" ";
top--;
while(k!=NULL)
{
q=k->next;
if(q->status==0)
{
top++;
stack[top]=q->data;
q->status=1;
}
} }
getch();
return;
}

*Program-6*
*Object: - WAP C++ Object Oriented code for create new string class say new
string.*
*Coding:-*
#include<stdio.h>
#include<conio.h>
#include<string.h>
STRLEN(char*);
int STRCPY(char*,char*);
int STRCMP(char*,char*);
int STRCAT(char*,char*,char*);
int STRREV(char*);
void main()
{
int c;
char str[20],str1[10],str2[10],str3[20];
clrscr();
re:
printf("
Enter choice=>");
printf("
1:string len.
2:string copy
3:string
cmp.
4:string cat.
5:string rev.");
printf("
6:for exit=>");
scanf("%d",&c);switch(c)
{
case 1:
printf("Enter the string=>");
scanf("%s",&str1);
printf("string length=>%d
",STRLEN(str1));
break;
case 2:
printf("
Enter the string=>");
scanf("%s",str1);
STRCPY(str2,str1);
printf("copied string=>");
puts(str2);
break;
case 3:
printf("Enter two string=>");
scanf("%s",&str1);
scanf("%s",&str2);
if(STRCMP(str2,str1))
printf("string is equal");
else
printf("String is not equal");
break;
case 4:
printf("Enter two string=>");
scanf("%s",str1);
scanf("%s",str2);
STRCAT(str3,str2,str1);
puts(str3);
break;
case 5:
printf("Enter the string=>");
scanf("%s",str1);
STRREV(str1);
printf("Reverse stringis=>");
puts(str1);
break;
default:
goto end;
}
goto re;
end:
getch();
}
int STRLEN(char *s)
{
int i=0;
while(*s!=NULL)
{
i++;
s++;
}
return i;
}
int STRCPY(char *s2,char *s1)
{
while(*s1!=NULL)
{
*s2=*s1;
s2++;
s1++;
}
*s2=NULL;
return 1;
}
int STRCMP(char *s2,char *s1)
{
int i=0,len1,len2;
len1=strlen(s1);
len2=strlen(s2);
if(len1==len2)
{
while(*s2==*s1 && *s2 != NULL && *s1!=NULL)
{
i++;
s1++;
s2++;
}
if(i==len1)
return 1;
else
return 0;
}
else
{
return 0;
}
}
int STRREV(char *s)
{
int len;
char *s1;
char *ptr;
len=strlen(s);
s1=(char *)malloc(sizeof(char));
strcpy(s1,s);
ptr=s1+len-1;
while(*s!=NULL)
{
*s=*ptr;
ptr--;
s++;
s1++;
}
*s=NULL;
return 1;
}
int STRCAT(char *s3,char *s2,char *s1)
{
while(*s1!=NULL)
{
*s3=*s1;
s3++;
s1++;
}
s3++;
while(*s2!=NULL)
{
*s3=*s2;
s3++;
s2++;
}
*s3=NULL;
return 1;
}