Computer Programming and Technology
Tuesday, 29 March 2016
Monday, 21 March 2016
Implement multiple queues in a single dimensional array. Write algorithms for various queue operations for them.
#include<stdio.h>
#include<conio.h>
#define max 10
struct queue
{
int arr[max];
int front, rear;
}q;
void init()
{
q.front=-1,q.rear=-1;
}
void add_front(int n)
{
if (q.front==0)
{
printf("Element can not be inserted at front");
return;
}
else
{
q.front--;
q.arr[q.front]=n;
if(q.front==-1)
q.front=0;
}
}
void del_front()
{
int a;
if(q.front==-1)
{
printf("queue is empty");
return;
}
else
a=q.front;
printf("\n\deleted number is =%d at position %d",q.arr[a],a);
if(q.front==q.rear)
q.front=q.rear-1;
else
q.front=q.front+1;
}
void add_rear(int n) // add and element into the linked list
{
if (q.front==max-1)
{
printf("Element can not be inserted at rear");
return;
}
else
{
q.rear=q.rear+1;
q.arr[q.rear]=n;
if(q.rear==-1)
q.rear=0;
}
}
void del_rear()
{
int a;
if(q.rear==-1)
{
printf("queue is empty");
return;
}
else
{
a=q.front;
printf("\n\deleted number is =%d ",q.arr[a]);
if(q.front==q.rear)
q.front=q.rear-1;
else
{
q.rear=q.rear-1;
}
}
}
void disp()
{
int i;
printf("\n\nQueue as an array\n\n");
for(i=q.front;i<=q.rear;i++)
printf("--->%d",q.arr[i]);
}
int main()
{
int ch,n;
char c;
init();
do
{
printf("\n1-add front\n");
printf("2-add rear\n");
printf("3-delete front\n");
printf("4-delete rear\n");
printf("5-display rear\n");
printf("6- Exit\n");
printf("\n\n enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nenter the number");
scanf("%d",&n);
add_front(n);
break;
case 2:
printf("\nenter the number");
scanf("%d",&n);
add_rear(n);
break;
case 3:
del_front();
break;
case 4:
del_rear();
break;
case 5:
disp();
break;
case 6:
break;
default:
printf("sorry wrong choice");
}
printf("\nD you want to continue(y/n)");
c=getche();
}
while(c=='y');
getch();
}
#include<conio.h>
#define max 10
struct queue
{
int arr[max];
int front, rear;
}q;
void init()
{
q.front=-1,q.rear=-1;
}
void add_front(int n)
{
if (q.front==0)
{
printf("Element can not be inserted at front");
return;
}
else
{
q.front--;
q.arr[q.front]=n;
if(q.front==-1)
q.front=0;
}
}
void del_front()
{
int a;
if(q.front==-1)
{
printf("queue is empty");
return;
}
else
a=q.front;
printf("\n\deleted number is =%d at position %d",q.arr[a],a);
if(q.front==q.rear)
q.front=q.rear-1;
else
q.front=q.front+1;
}
void add_rear(int n) // add and element into the linked list
{
if (q.front==max-1)
{
printf("Element can not be inserted at rear");
return;
}
else
{
q.rear=q.rear+1;
q.arr[q.rear]=n;
if(q.rear==-1)
q.rear=0;
}
}
void del_rear()
{
int a;
if(q.rear==-1)
{
printf("queue is empty");
return;
}
else
{
a=q.front;
printf("\n\deleted number is =%d ",q.arr[a]);
if(q.front==q.rear)
q.front=q.rear-1;
else
{
q.rear=q.rear-1;
}
}
}
void disp()
{
int i;
printf("\n\nQueue as an array\n\n");
for(i=q.front;i<=q.rear;i++)
printf("--->%d",q.arr[i]);
}
int main()
{
int ch,n;
char c;
init();
do
{
printf("\n1-add front\n");
printf("2-add rear\n");
printf("3-delete front\n");
printf("4-delete rear\n");
printf("5-display rear\n");
printf("6- Exit\n");
printf("\n\n enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nenter the number");
scanf("%d",&n);
add_front(n);
break;
case 2:
printf("\nenter the number");
scanf("%d",&n);
add_rear(n);
break;
case 3:
del_front();
break;
case 4:
del_rear();
break;
case 5:
disp();
break;
case 6:
break;
default:
printf("sorry wrong choice");
}
printf("\nD you want to continue(y/n)");
c=getche();
}
while(c=='y');
getch();
}
Write an algorithm for the implementation of a Dequeue.
#include<stdio.h>
#include<conio.h>
#define max 10
struct queue
{
int arr[max];
int front, rear;
}q;
void init()
{
q.front=-1,q.rear=-1;
}
void add_front(int n)
{
if (q.front==0)
{
printf("Element can not be inserted at front");
return;
}
else
{
q.front--;
q.arr[q.front]=n;
if(q.front==-1)
q.front=0;
}
}
void del_front()
{
int a;
if(q.front==-1)
{
printf("queue is empty");
return;
}
else
a=q.front;
printf("\n\deleted number is =%d at position %d",q.arr[a],a);
if(q.front==q.rear)
q.front=q.rear-1;
else
q.front=q.front+1;
}
void add_rear(int n)
{
if (q.front==max-1)
{
printf("Element can not be inserted at rear");
return;
}
else
{
q.rear=q.rear+1;
q.arr[q.rear]=n;
if(q.rear==-1)
q.rear=0;
}
}
void del_rear()
{
int a;
if(q.rear==-1)
{
printf("queue is empty");
return;
}
else
{
a=q.front;
printf("\n\deleted number is =%d ",q.arr[a]);
if(q.front==q.rear)
q.front=q.rear-1;
else
{
q.rear=q.rear-1;
}
}
}
void disp()
{
int i;
printf("\n\nQueue as an array\n\n");
for(i=q.front;i<=q.rear;i++)
printf("--->%d",q.arr[i]);
}
main()
{
int ch,n;
char c;
init();
do
{
printf("\n1-add front\n");
printf("2-add rear\n");
printf("3-delete front\n");
printf("5-display rear\n");
printf("6- Exit\n");
printf("\n\n enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nenter the number");
scanf("%d",&n);
add_front(n);
break;
case 2:
printf("\nenter the number");
scanf("%d",&n);
add_rear(n);
break;
case 3:
del_front();
break;
case 4:
del_rear();
break;
case 5:
disp();
break;
case 6:
break;
default:
printf("sorry wrong choice");
}
printf("\nD you want to continue(y/n)");
c=getch();
}while(c=='y');
getch();
}
#include<conio.h>
#define max 10
struct queue
{
int arr[max];
int front, rear;
}q;
void init()
{
q.front=-1,q.rear=-1;
}
void add_front(int n)
{
if (q.front==0)
{
printf("Element can not be inserted at front");
return;
}
else
{
q.front--;
q.arr[q.front]=n;
if(q.front==-1)
q.front=0;
}
}
void del_front()
{
int a;
if(q.front==-1)
{
printf("queue is empty");
return;
}
else
a=q.front;
printf("\n\deleted number is =%d at position %d",q.arr[a],a);
if(q.front==q.rear)
q.front=q.rear-1;
else
q.front=q.front+1;
}
void add_rear(int n)
{
if (q.front==max-1)
{
printf("Element can not be inserted at rear");
return;
}
else
{
q.rear=q.rear+1;
q.arr[q.rear]=n;
if(q.rear==-1)
q.rear=0;
}
}
void del_rear()
{
int a;
if(q.rear==-1)
{
printf("queue is empty");
return;
}
else
{
a=q.front;
printf("\n\deleted number is =%d ",q.arr[a]);
if(q.front==q.rear)
q.front=q.rear-1;
else
{
q.rear=q.rear-1;
}
}
}
void disp()
{
int i;
printf("\n\nQueue as an array\n\n");
for(i=q.front;i<=q.rear;i++)
printf("--->%d",q.arr[i]);
}
main()
{
int ch,n;
char c;
init();
do
{
printf("\n1-add front\n");
printf("2-add rear\n");
printf("3-delete front\n");
printf("5-display rear\n");
printf("6- Exit\n");
printf("\n\n enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nenter the number");
scanf("%d",&n);
add_front(n);
break;
case 2:
printf("\nenter the number");
scanf("%d",&n);
add_rear(n);
break;
case 3:
del_front();
break;
case 4:
del_rear();
break;
case 5:
disp();
break;
case 6:
break;
default:
printf("sorry wrong choice");
}
printf("\nD you want to continue(y/n)");
c=getch();
}while(c=='y');
getch();
}
Write an Algorithm and a Program that sorts a list of 10 integers wherein each integer is having at least 25 digits.
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
int i,j,n;
char str[10][30]={"2234565456545654345432345","8234565456545654345432345","923465456545654345432345","234565456545654345432345","34565456545654345432345"};
char temp[30];
n=5;
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("THE stored string\n");
for(i=0;i<=n;i++)
puts(str[i]);
getch();
}
#include<string.h>
#include<conio.h>
main()
{
int i,j,n;
char str[10][30]={"2234565456545654345432345","8234565456545654345432345","923465456545654345432345","234565456545654345432345","34565456545654345432345"};
char temp[30];
n=5;
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("THE stored string\n");
for(i=0;i<=n;i++)
puts(str[i]);
getch();
}
Write an Algorithm and a Program that accepts a Binary Tree as input and checks whether it is a Height Balanced Tree.
#include<stdio.h>
#include<alloc.h>
struct btree
{
int data;
struct btree*left;
struct btree*right;
};
void add(struct btree**q,int n)
{
struct bttree*t,*temp;
t=malloc(sizeof(struct btree));
t->data=n;
t->left = NULL;
t->right =NULL;
if(*q==NULL)
{
*q=t;
}
else
{
temp=*q;
while(temp->left!=NULL && temp->right != NULL)
{
if(temp->data<=n)
temp = temp->right;
else
temp=temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n==NULL) return 0;
return max(height(n->left),height(n->right))+1
}
int is balanced(node*n)
{
if(n == NULL) return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return is balanced(n->left) && isbalaced(n->right));
}
inorder(struct btree*s)
{
if(s!= NULL)
{
inoroder(s->left);
{
if(temp->data<=n)
temp=temp->right;
else
temp= temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n == NULL) return 0;
return max(height(n->left), height(n->right))+1;
}
int isBalanced(node*n)
{
if(n ==NULL)return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return isbalanced(n->NULL) && isBalanced(n->right);
}
inorder(struct*s)
{
if(s!=NULL)
{
inorder(-s>left);
add(&p,10);
add(&p,5);
add(&p,60);
add(&p,70);
printf("\n\n in order=\n");
inorder(p);
printf("\n\n npre order=\n");
preorder(p);
printf("\n\n npost order=\n");
postorder(p);
h=hight(root);
printf("Hight:%d",h);
bal=isBalanced(root);
if(bal==1)
printf("it is balanceed");
getch();
}
}
}
#include<stdio.h>
#include<alloc.h>
struct btree
{
int data;
struct btree*left;
struct btree*right;
};
void add(struct btree**q,int n)
{
struct bttree*t,*temp;
t=malloc(sizeof(struct btree));
t->data=n;
t->left = NULL;
t->right =NULL;
if(*q==NULL)
{
*q=t;
}
else
{
temp=*q;
while(temp->left!=NULL && temp->right != NULL)
{
if(temp->data<=n)
temp = temp->right;
else
temp=temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n==NULL) return 0;
return max(height(n->left),height(n->right))+1
}
int is balanced(node*n)
{
if(n == NULL) return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return is balanced(n->left) && isbalaced(n->right));
}
inorder(struct btree*s)
{
if(s!= NULL)
{
inoroder(s->left);
{
if(temp->data<=n)
temp=temp->right;
else
temp= temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n == NULL) return 0;
return max(height(n->left), height(n->right))+1;
}
int isBalanced(node*n)
{
if(n ==NULL)return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return isbalanced(n->NULL) && isBalanced(n->right);
}
inorder(struct*s)
{
if(s!=NULL)
{
inorder(-s>left);
add(&p,10);
add(&p,5);
add(&p,60);
add(&p,70);
printf("\n\n in order=\n");
inorder(p);
printf("\n\n npre order=\n");
preorder(p);
printf("\n\n npost order=\n");
postorder(p);
h=hight(root);
printf("Hight:%d",h);
bal=isBalanced(root);
if(bal==1)
printf("it is balanceed");
getch();
}
}
}
#include<alloc.h>
struct btree
{
int data;
struct btree*left;
struct btree*right;
};
void add(struct btree**q,int n)
{
struct bttree*t,*temp;
t=malloc(sizeof(struct btree));
t->data=n;
t->left = NULL;
t->right =NULL;
if(*q==NULL)
{
*q=t;
}
else
{
temp=*q;
while(temp->left!=NULL && temp->right != NULL)
{
if(temp->data<=n)
temp = temp->right;
else
temp=temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n==NULL) return 0;
return max(height(n->left),height(n->right))+1
}
int is balanced(node*n)
{
if(n == NULL) return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return is balanced(n->left) && isbalaced(n->right));
}
inorder(struct btree*s)
{
if(s!= NULL)
{
inoroder(s->left);
{
if(temp->data<=n)
temp=temp->right;
else
temp= temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n == NULL) return 0;
return max(height(n->left), height(n->right))+1;
}
int isBalanced(node*n)
{
if(n ==NULL)return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return isbalanced(n->NULL) && isBalanced(n->right);
}
inorder(struct*s)
{
if(s!=NULL)
{
inorder(-s>left);
add(&p,10);
add(&p,5);
add(&p,60);
add(&p,70);
printf("\n\n in order=\n");
inorder(p);
printf("\n\n npre order=\n");
preorder(p);
printf("\n\n npost order=\n");
postorder(p);
h=hight(root);
printf("Hight:%d",h);
bal=isBalanced(root);
if(bal==1)
printf("it is balanceed");
getch();
}
}
}
#include<stdio.h>
#include<alloc.h>
struct btree
{
int data;
struct btree*left;
struct btree*right;
};
void add(struct btree**q,int n)
{
struct bttree*t,*temp;
t=malloc(sizeof(struct btree));
t->data=n;
t->left = NULL;
t->right =NULL;
if(*q==NULL)
{
*q=t;
}
else
{
temp=*q;
while(temp->left!=NULL && temp->right != NULL)
{
if(temp->data<=n)
temp = temp->right;
else
temp=temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n==NULL) return 0;
return max(height(n->left),height(n->right))+1
}
int is balanced(node*n)
{
if(n == NULL) return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return is balanced(n->left) && isbalaced(n->right));
}
inorder(struct btree*s)
{
if(s!= NULL)
{
inoroder(s->left);
{
if(temp->data<=n)
temp=temp->right;
else
temp= temp->left;
}
if(temp->data<=n)
temp->right=t;
else
temp->left=t;
}
}
int height(node*n)
{
if(n == NULL) return 0;
return max(height(n->left), height(n->right))+1;
}
int isBalanced(node*n)
{
if(n ==NULL)return 1;
if(abs(height(n->left)-height(n->right))>1)return 0;
return isbalanced(n->NULL) && isBalanced(n->right);
}
inorder(struct*s)
{
if(s!=NULL)
{
inorder(-s>left);
add(&p,10);
add(&p,5);
add(&p,60);
add(&p,70);
printf("\n\n in order=\n");
inorder(p);
printf("\n\n npre order=\n");
preorder(p);
printf("\n\n npost order=\n");
postorder(p);
h=hight(root);
printf("Hight:%d",h);
bal=isBalanced(root);
if(bal==1)
printf("it is balanceed");
getch();
}
}
}
Sunday, 20 March 2016
Write C++ program for addison of two complex numbers by overloading „+‟ operator. Make necessary assumptions wherever required.
#include<iostream>
#include<conio.h>
using namespace std;
class complex
{
int real,imaginary;
public:complex()
{
}
complex(int a,int b)
{
real=a;
imaginary=b;
}
void operator+(complex);
};
void complex::operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.imaginary=imaginary+c.imaginary;
cout<<"real sum is:"<<temp.real<<endl;
}
int main(void)
{
complex c1(10,20);
complex c2(20,30);
c1+c2;
getch();
}
#include<conio.h>
using namespace std;
class complex
{
int real,imaginary;
public:complex()
{
}
complex(int a,int b)
{
real=a;
imaginary=b;
}
void operator+(complex);
};
void complex::operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.imaginary=imaginary+c.imaginary;
cout<<"real sum is:"<<temp.real<<endl;
}
int main(void)
{
complex c1(10,20);
complex c2(20,30);
c1+c2;
getch();
}
Write a C++ program to demonstrate exception handling by using matrix multiplication operation. Matrix multiplication function should notify if the order of the matrix is invalid, using exception.
#include<iostream>
#include<conio.h>
#include<process.h>
#include<exception>
using namespace std;
void matsum()
{
int m, n, i, j, first[10][10], second[10][10], sum[10][10];
try
{
cout << "Enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "Enter the elements of first matrix\n";
if (m>10 || n>10)
//exit(0);
throw 1;
}
catch(int)
{
cout<<"subscript invalid";
}
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
cin >> first[i][j];
cout << "Enter the elements of second matrix\n";
for ( i = 0 ; i < m ;i++ )
for ( j = 0 ; j < n ; j++ )
cin >> second[i][j];
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
sum[i][j] = first[i][j] + second[i][j];
cout << "Sum of entered matrices:-\n";
for ( i = 0 ; i < m ; i++ )
{
for ( j = 0 ; j < n ; j++ )
cout << sum[i][j] << "\t";
cout << endl;
}
}
int main(void)
{
matsum();
getch();
}
#include<conio.h>
#include<process.h>
#include<exception>
using namespace std;
void matsum()
{
int m, n, i, j, first[10][10], second[10][10], sum[10][10];
try
{
cout << "Enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "Enter the elements of first matrix\n";
if (m>10 || n>10)
//exit(0);
throw 1;
}
catch(int)
{
cout<<"subscript invalid";
}
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
cin >> first[i][j];
cout << "Enter the elements of second matrix\n";
for ( i = 0 ; i < m ;i++ )
for ( j = 0 ; j < n ; j++ )
cin >> second[i][j];
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
sum[i][j] = first[i][j] + second[i][j];
cout << "Sum of entered matrices:-\n";
for ( i = 0 ; i < m ; i++ )
{
for ( j = 0 ; j < n ; j++ )
cout << sum[i][j] << "\t";
cout << endl;
}
}
int main(void)
{
matsum();
getch();
}
Subscribe to:
Comments (Atom)