Tuesday, July 2, 2013

C PROGRAM FOR CIRCULAR QUEUE

/*PROGRAM TO TEST INSERTION & DELETION OPERATION IN CIRCULAR QUEUE*/
#include<stdio.h>
#include<conio.h>
#define max 5
int front,rear,q[max];
void inqueue();
void delqueue();
void qdisplay();
int main() {
int c;
clrscr();
front=rear=-1;
do {
printf("\n1:INSERTION \n2:DELETION\n3:DISPLAY ");
printf("\n4:EXIT\nEnter Your Choice:");
scanf("%d",&c);
switch(c) {
case 1: {inqueue ();
break; }
case 2: {delqueue ();
break; }
case 3: {qdisplay ();
break; }
}
}
while(c!=4);
}

void inqueue() {
int x;
if ((front==0&&rear==max-1)|| (front==rear+1))
{ printf("\nQUEUE OVERFLOW\n");
return; }
if(front==-1)
{ front=rear=0; }
else
{ if(rear==max-1)
{ rear=0; }
else { rear++; } }
printf("\nEnter The Number:");
scanf("%d",&x);
q[rear]=x;
return; }

void delqueue() {
int y;
if(front==-1) {
printf("\nQUEUE IS UNDERFLOW \n");
return; }
y=q[front];
if(front==rear)
{ front=rear=-1; }
else
{ if(front==max-1)
{ front=0; }
else
{ front++; } }
printf("\n%d SUCESSFULLY DELETED \n",y);
return; }

void qdisplay() {
int i,j;
if(front==rear==-1)
{ printf("\nQUEUE IS EMPTY \n");
return; }
printf("\nITEMS ARE:");
for(i=front;i!=rear;i=(i +1)%max)
{ printf(" %d ",q[i]); }
printf(" %d ",q[rear]);
return; }





/************************************************************/
/*OUTPUT:-\

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:1

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:2

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:3

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:4

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:5

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

QUEUE OVERFLOW

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 1  2  3  4  5
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:2

1 SUCESSFULLY DELETED

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 2  3  4  5
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:7

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 2  3  4  5  7
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:4 */

C PROGRAM FOR 2D MATRIX MULTIPLICATION

/*PROGRAM TO MULTIPLY TWO MATRICES*/

#include<stdio.h>
#include<conio.h>
void main() {
int i,j,k,m,n,o,p,sum=0;
int a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter order of 1st 2D matrix:");
scanf("%d %d",&m,&n);
printf("Enter order of 2nd 2D matrix");
scanf("%d %d",&o,&p);
if(n==o){
printf("Enter elements of 1st 2d matrix:");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++){
                  printf("\na[%d][%d]=", i,j);
                  scanf("%d", &a[i][j]);
                  }
printf("Enter elements of 2nd 2D matrix:");
for(i=1;i<=o;i++)
for(j=1;j<=p;j++){
                  printf("\nb[%d][%d]=", i,j);
                  scanf("%d", &b[i][j]);
                  }
printf("\nThe product of two matrices has these elements:");
for(i=1;i<=m;i++)
for(j=1;j<=p;j++){
                  for(k=1;k<=n;k++)
                  sum=sum+a[i][k]*b[k][j];
                  c[i][j]=sum;
                  sum=0;
                  printf("\nc[%d][%d]=%d", i,j,c[i][j]);
                  }                
}
else
printf("The matrices cannot be multiplied");
getch();
}

/* OUTPUT:-
Enter order of 1st 2D matrix:1
2
Enter order of 2nd 2D matrix2
3
Enter elements of 1st 2d matrix:
a[1][1]=1

a[1][2]=2
Enter elements of 2nd 2D matrix:
b[1][1]=3

b[1][2]=4

b[1][3]=5

b[2][1]=6

b[2][2]=7

b[2][3]=8

The product of two matrices has these elements:
c[1][1]=15
c[1][2]=18
c[1][3]=21  */

C PROGRAM FOR 2D MATRIX ADDITION

/* PROGRAM TO ADD TWO MATRICES*/

#include<stdio.h>
#include<conio.h>
void main() {
int i,j,m,n;
int a[5][5],b[5][5],c[10][10];
clrscr();
printf("Enter size of 2D arrays:");
scanf("%d %d",&m,&n);
printf("Enter elements of 1st 2d matrix:");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++){
                  printf("\na[%d][%d]=", i,j);
                  scanf("%d", &a[i][j]);
                  }
printf("Enter elements of 2nd 2D matrix:");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++){
                  printf("\nb[%d][%d]=", i,j);
                  scanf("%d", &b[i][j]);
                  }
printf("\nThe sum of two matrix has these elements:");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++){
                  c[i][j]=a[i][j]+b[i][j];
                  printf("\nc[%d][%d]=%d", i,j,c[i][j]);
                  }                
getch();
}

/* OUTPUT:-
Enter size of 2D arrays:1
2
Enter elements of 1st 2d matrix:
a[1][1]=1

a[1][2]=2
Enter elements of 2nd 2D matrix:
b[1][1]=1

b[1][2]=5

The sum of two matrix has these elements:
c[1][1]=2
c[1][2]=7  */