DAA

Program for factorial 

#include<iostream.h>
void fibbo(int,int,int);
void main()
{
int d1=0,d2=1,n;
cin>>n;
cout<<d1;
fibbo(n,d1,d2);
}
void fibbo(int n,int d1,int d2)
{
int res;
if(n>0)
{
res=d2+d1;
d2=d1;
d1=res;
cout<<res;
fibbo(n-1,d1,d2);
}
}


Program for gcd 



#include<iostream.h>
int gcd(int x,int y)
{
if(x==y)
return (x);
else
{
if(x>y)
gcd(x-y,y);
else
gcd(x,y-x);
}
}
void main()
{
int n1,n2,g;
cin>>n1>>n2;
g=gcd(n1,n2);
cout<<"gcd of "<<n1<<" & "<<n2<<" is :"<<g;
}


************************quicksort****************

#include<stdio.h>

void quicksort(int [10],int,int);

int main(){
  int x[20],size,i;

  printf("Enter size of the array: ");
  scanf("%d",&size);

  printf("Enter %d elements: ",size);
  for(i=0;i<size;i++)
    scanf("%d",&x[i]);

  quicksort(x,0,size-1);

  printf("Sorted elements: ");
  for(i=0;i<size;i++)
    printf(" %d",x[i]);

  return 0;
}

void quicksort(int x[10],int first,int last){
    int pivot,j,temp,i;

     if(first<last){
         pivot=first;
         i=first;
         j=last;

         while(i<j){
             while(x[i]<=x[pivot]&&i<last)
                 i++;
             while(x[j]>x[pivot])
                 j--;
             if(i<j){
                 temp=x[i];
                  x[i]=x[j];
                  x[j]=temp;
             }
         }

         temp=x[pivot];
         x[pivot]=x[j];
         x[j]=temp;
         quicksort(x,first,j-1);
         quicksort(x,j+1,last);

    }
}

Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8

********************mergeshort********************

#include<stdio.h>
#define MAX 50

void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);

int main(){
 
    int merge[MAX],i,n;

    printf("Enter the total number of elements: ");
    scanf("%d",&n);

    printf("Enter the elements which to be sort: ");
    for(i=0;i<n;i++){
         scanf("%d",&merge[i]);
    }

    partition(merge,0,n-1);

    printf("After merge sorting elements are: ");
    for(i=0;i<n;i++){
         printf("%d ",merge[i]);
    }

   return 0;
}

void partition(int arr[],int low,int high){

    int mid;

    if(low<high){
         mid=(low+high)/2;
         partition(arr,low,mid);
         partition(arr,mid+1,high);
         mergeSort(arr,low,mid,high);
    }
}

void mergeSort(int arr[],int low,int mid,int high){

    int i,m,k,l,temp[MAX];

    l=low;
    i=low;
    m=mid+1;

    while((l<=mid)&&(m<=high)){

         if(arr[l]<=arr[m]){
             temp[i]=arr[l];
             l++;
         }
         else{
             temp[i]=arr[m];
             m++;
         }
         i++;
    }

    if(l>mid){
         for(k=m;k<=high;k++){
             temp[i]=arr[k];
             i++;
         }
    }
    else{
         for(k=l;k<=mid;k++){
             temp[i]=arr[k];
             i++;
         }
    }
 
    for(k=low;k<=high;k++){
         arr[k]=temp[k];
    }
}


Sample output:

Enter the total number of elements: 5
Enter the elements which to be sort: 2 5 0 9 1
After merge sorting elements are: 0 1 2 5 9

*******************************matrix *************************

#include<stdio.h>
int main(){
  int a[2][2],b[2][2],c[2][2],i,j;
  int m1,m2,m3,m4,m5,m6,m7;

  printf("Enter the 4 elements of first matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&a[i][j]);

  printf("Enter the 4 elements of second matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&b[i][j]);

  printf("\nThe first matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",a[i][j]);
  }

  printf("\nThe second matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",b[i][j]);
  }

  m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
  m2= (a[1][0]+a[1][1])*b[0][0];
  m3= a[0][0]*(b[0][1]-b[1][1]);
  m4= a[1][1]*(b[1][0]-b[0][0]);
  m5= (a[0][0]+a[0][1])*b[1][1];
  m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
  m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

  c[0][0]=m1+m4-m5+m7;
  c[0][1]=m3+m5;
  c[1][0]=m2+m4;
  c[1][1]=m1-m2+m3+m6;

   printf("\nAfter multiplication using \n");
   for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",c[i][j]);
   }

   return 0;
}

****************************************prims********************************

/*WAP to find min spanning tree using prim's algorithm*/
#include<iostream.h>
#include<conio.h>
void pmst(int g[10][10],int nodes)
{
int i,j,tree[10],min_dist=999,v1,v2,total=0;
for(i=0;i<nodes;i++)
tree[i]=0;
tree[0]=1;
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
if(g[i][j] && (tree[i] && !tree[j]) || (!tree[i] && tree[j]))
{
if(g[i][j]<min_dist)
{
min_dist=g[i][j];
v1=i;
v2=j;
cout<<"("<<v1<<","<<v2<<")"<<"\tminimum distance"<<min_dist;
tree[v1]=tree[v2]=1;
total=total+min_dist;
}

}

}
min_dist=999;

}
cout<<"total:-"<<total;
}
void main()
{
int n,g[10][10],i,j;
cout<<"Enter no.of nodes";
cin>>n;
cout<<"Enter adjacency matrix:";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"Enter element"<<(i+1)<<(j+1)<<" ";
cin>>g[i][j];
}
}
pmst(g,n);

}
*******************************knapsack *******************
#include<iostream.h>
struct kru
{
  int i,p,w;
  float pr;

}g[10];
void main()
{

int n,j,k,wa;
float total=0;
struct kru temp;
cout<<"enter data ow W";
cin>>wa;
cout<<"Enter no.of nodes";
cin>>n;
cout<<"Enter for sum of calculation:";
for(j=0;j<n;j++)
{
g[j].i=j+1;
cout<<"i th profit =";
cin>>g[j].p;
cout<<"i th weight =";
cin>>g[j].w;
}
for(j=0;j<n;j++)
{
g[j].pr=g[j].p/float(g[j].w);
}
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
if(g[j].pr<=g[k].pr)
{
 temp=g[j];
 g[j]=g[k];
 g[k]=temp;

}
}
}
for(j=0;j<n;j++)
{
 if(wa>=g[j].w)
 {
 total=total+g[j].p;
 wa=wa-g[j].w;
 }
 total=total+(g[j].pr*wa);
}
cout<<"total is "<<total;

}



1 comment: