Showing posts with label two. Show all posts
Showing posts with label two. Show all posts

Saturday, August 9, 2014

Program to merge two arrays

Program to merge two arrays. This isnt really an important program. But anyways it might be handy.

#include<stdio.h>
main()
{
 int a[50],b[50],i,j,k;
 printf("Enter the number of elements of the first array:\n");
 scanf("%d",&j);
 printf("Enter the elements of the first array:\n");
 for(i=0;i<j;i++)
 scanf("%d",&*(a+i));
 printf("Enter the number of elements of the second array:\n");
 scanf("%d",&k);
 printf("Enter the elements of the second array:\n");
 for(i=0;i<k;i++)
 scanf("%d",&*(b+i));
 for(i=0;i<k;i++)
 *(a+j+i)=*(b+i);
 j=j+k;
 printf("The merged array is :\n");
 for(i=0;i<j;i++)
 printf("%d\t",*(a+i));
}

Program to swap two numbers

Program to swap two numbers with address and pointers. Again, a necessary program for all amateur CSE students.

#include<stdio.h>
void swap(int *x,int *y)
{
 int temp;
 temp=*x;
 *x=*y;
 *y=temp;
}
void main()
{
  int a,b;
  printf("Enter the number a:");
  scanf("%d",&a);
  printf("\n Enter the number b:");
  scanf("%d",&b);
  swap(&a,&b);
  printf("\n a contain %d",a);
  printf("\n b contain %d",b);
}

Program of the various operations on a dequeue

This is a simple C program of the various operation on a dequeue.

#include<stdio.h> void input_re(); void output_re(); void insert_rear(); void insert_front(); void delet_front(); void delet_rear(); void display(); int dq[50],n,ch,rear=-1,front=-1,d,x,i; main() { int ch,c; printf("\nEnter the size of deque:"); scanf("%d",&n); do{ printf(" MENU\n1.input restricted deque\n2.output restricted deque"); printf("\n enter the choice"); scanf("%d",&ch); switch(ch) { case 1: input_re(); break; case 2: output_re(); break; default: printf("\n Wrong option"); } printf("\n do you want to continue::"); scanf("%d",&c); }while(c==1); } void input_re() { int c; do { int ch; printf("MENU\n1.insert rear\n2.delete front\n3.delete rear\n4.display"); printf("\n enter the choice"); scanf("%d",&ch); switch(ch) { case 1: insert_rear(); break; case 2: delet_front(); break; case 3: delet_rear(); break; case 4: display(); break; default: printf("\n Wrong option"); } printf("\n do you want to continue the input restricted function :"); scanf("%d",&c); }while(c==1); } void output_re() { int ch,c; do { printf("menu\n1.insert rear\n2.insert front\n3.delet from front\n4.display"); printf(" enter the choice"); scanf("%d",&ch); switch(ch) { case 1: insert_rear(); break; case 2: insert_front(); break; case 3: delet_front(); break; case 4: display(); break; default: printf(" invalid option"); } printf("\n do you want to continue the output restricted function:"); scanf("%d",&c); }while(c==1); } void insert_rear() { printf("\n enter the element to be inserted"); scanf("%d",&x); if(((front==0)&&(rear==n-1))||(front==rear+1)) printf("\n deque overflow"); else if((front==-1)&&(rear==-1)) {front=0; rear=0; dq[rear]=x; } else rear=(rear+1)%n; dq[rear]=x; } void insert_front() { printf("\n enter the element to insert:"); scanf("%d",&x); if(((front==0)&&(rear==n-1))||(front==rear+1)) printf("\n deque is full"); else if((front==-1)&&(rear==-1)) front=rear=0; else { if(front==0) front=n-1; else front=front-1; dq[front]=x; }} void delet_front() { if((front==-1)&&(rear==-1)) printf("\n deque is empty"); else { if(front==rear) { front=-1; rear=-1; } else front++; } } void delet_rear() { if((front==-1)&&(rear==-1)) printf("\n deque is empty"); else { if(front==rear) { front=-1; rear=-1; } else { if(rear==0) rear=n-1; else rear=rear-1; } } } void display() { if((front==-1)&&(rear==-1)) printf("\n deque is empty"); else { if(front>rear) { for(i=front;i<n;i++) { d=dq[i]; printf("%d\t",d); } for(i=0;i<=rear;i++) { d=dq[i]; printf("%d\t",d); } } else { for(i=front;i<=rear;i++) { d=dq[i]; printf("%d\t",d); } } } }

SImple Matrix addition

Simple C program for the addition of 2 matrices. If you have any doubts, please let me know.

#include<stdio.h> #define size 30 int sp1[size][3],sp2[size][3],sp3[size][3],s1=0,s2=0,s3=0,r,c; int main() { void add(); int a[size][size],i,j; printf("\n\n\t\tEnter number of rows and columns :"); scanf("%d%d",&r,&c); printf("\n\n\t\tEnter matrix 1 : \n"); for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d",&a[i][j]); for(i=0;i<r;i++) for(j=0;j<c;j++) { if(a[i][j]!=0) { s1++; sp1[s1][0]=i; sp1[s1][1]=j; sp1[s1][2]=a[i][j]; } } printf("\n\n\t\tEnter matrix 2 : \n"); for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d",&a[i][j]); for(i=0;i<r;i++) for(j=0;j<c;j++) { if(a[i][j]!=0) { s2++; sp2[s2][0]=i; sp2[s2][1]=j; sp2[s2][2]=a[i][j]; } } sp1[0][0]=r; sp1[0][1]=c; sp1[0][2]=s1; sp2[0][0]=r; sp2[0][1]=c; sp2[0][2]=s2; printf("\n\n\t\t The matrices are\n\t\t\tA : \n"); for(i=0;i<=s1;i++) { printf("\n"); for(j=0;j<3;j++) printf("\t %d",sp1[i][j]); } printf("\n\t\t\tB : \n"); for(i=0;i<=s2;i++) { printf("\n"); for(j=0;j<3;j++) printf("\t %d",sp2[i][j]); } printf("\n\n"); add(); printf("\n\n"); } void add() { int i=1,j=1; while((i!=s1+1)&&(j!=s2+1)) { if(sp1[i][0]==sp2[j][0]) { if(sp1[i][1]==sp2[j][1]) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]+sp2[j][2]; i++; j++; } else if(sp1[i][1]<=sp2[j][1]) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]; j++; } else { s3++; sp3[s3][0]=sp2[j][0]; sp3[s3][1]=sp2[j][1]; sp3[s3][2]=sp2[j][2]; j++; } } else if(sp1[i][0]<sp2[j][0]) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]; i++; } else { s3++; sp3[s3][0]=sp2[j][0]; sp3[s3][1]=sp2[j][1]; sp3[s3][2]=sp2[j][2]; j++; } } if(i!=s1+1) { while(i!=s1+1) { s3++; sp3[s3][0]=sp1[i][0]; sp3[s3][1]=sp1[i][1]; sp3[s3][2]=sp1[i][2]; i++; } } if(j!=s2+1) { while(j!=s2+1) { s3++; sp3[s3][0]=sp2[j][0]; sp3[s3][1]=sp2[j][1]; sp3[s3][2]=sp2[j][2]; j++; } } sp3[0][0]=r; sp3[0][1]=c; sp3[0][2]=s3; printf("\nThe Sum is :\n"); for(i=0;i<=s3;i++) { printf("\n"); for(j=0;j<3;j++) printf("\t%d",sp3[i][j]); } }