Posts

Showing posts from February, 2021

Power set

 #include <stdio.h> #include <math.h> void printPowerSet(int *set, int set_size) { unsigned int pow_set_size = pow(2, set_size); int counter, j,m; for(counter = 0; counter < pow_set_size; counter++) {printf("{");m=0; for(j = 0; j < set_size; j++) { if(counter & (1<<j)) printf(" %d ", set[j]); }printf("}"); printf("\n"); } } int main() {  int x,i;  printf("Enter the size of set: "); scanf("%d",&x); int set[x]; printf("Enter the set\n"); for(i=0;i<x;i++)  scanf("%d",&set[i]);  printf("Power set will be: \n"); printPowerSet(set, x); return 0; }

Djisktra Complete

 #include<bits/stdc++.h> using namespace std; int adj[20][20]; void print_djistra(int key[], int x) {     int sum=0,i;     for(i=1;i<=x;i++)     {         cout<<"VERTEX:"<<i<<"Min-Distance:"<<key[i]<<endl;     }     cout<<"\nMin weight is : "<<sum; } int min_key(int key[],bool mstset[], int x) {     int mn=9999999, min_index,i;     for(i=1;i<=x;i++)     {         if(mstset[i]==false && key[i]<mn)         {             mn=key[i];             min_index=i;         }     }     return min_index; } void djistra(int x) {     int key[x+1],i,u,v;     bool mstset[x+1];     for(i=1;i<=x;i++)     {         key[i]=9999999;   ...

Union

#include<bits/stdc++.h> using namespace std; int main() {     int x,y,i,j;     cout<<"Enter the size of first array :";     cin>>x;     cout<<"Enter the size of second array :";     cin>>y;     int a[x], b[y];     cout<<"Enter the elements of first array :";     for(i=0;i<x;i++)     cin>>a[i];     cout<<"Enter the elements of second array :";     for(i=0;i<y;i++)     cin>>b[i];     sort(a,a+x,greater<int>());     sort(b,b+y,greater<int>());     i=0;j=0;     while(i<x && j<y)     {         if(a[i]>b[j])         cout<<a[i++]<<" ";         else if(a[i]<b[j])         cout<<b[j++]<<" ";         else if(a[i]==b[j]...

Symmetric Difference

 #include<bits/stdc++.h> using namespace std; int main() {     int x,y,i,j,f;     cout<<"Enter the size of first array :";     cin>>x;     cout<<"Enter the size of second array :";     cin>>y;     int a[x], b[y],c[x+y];     cout<<"Enter the elements of first array :";     for(i=0;i<x;i++)     cin>>a[i];     cout<<"Enter the elements of second array :";     for(i=0;i<y;i++)     cin>>b[i];     for(i=0;i<x;i++)     {         f=0;         for(j=0;j<y && f==0;j++)             if(a[i]==b[j])             f=1;         if(f==0)         cout<<a[i]<<" ";     }     for(i=0;i<y;i++)     {     ...

Set difference

#include<bits/stdc++.h> using namespace std; int main() {     int x,y,i,j,f;     cout<<"Enter the size of first array :";     cin>>x;     cout<<"Enter the size of second array :";     cin>>y;     int a[x], b[y];     cout<<"Enter the elements of first array :";     for(i=0;i<x;i++)     cin>>a[i];     cout<<"Enter the elements of second array :";     for(i=0;i<y;i++)     cin>>b[i];     cout<<"\nA-B : ";     for(i=0;i<x;i++)     {         f=0;         for(j=0;j<y && f==0;j++)             if(a[i]==b[j])             f=1;         if(f==0)         cout<<a[i]<<" ";     }     cout<<"\nB-A : "; ...

Intersection

#include<bits/stdc++.h> using namespace std; int main() {     int x,y,i,j,f=1;     cout<<"Enter the size of first array :";     cin>>x;     cout<<"Enter the size of second array :";     cin>>y;     int a[x], b[y];     cout<<"Enter the elements of first array :";     for(i=0;i<x;i++)     cin>>a[i];     cout<<"Enter the elements of second array :";     for(i=0;i<y;i++)     cin>>b[i];     for(i=0;i<x;i++)     for(j=0;j<y;j++)     if(a[i]==b[j])     {cout<<a[i]<<" ";f=0;}     if(f==1)         cout<<"NO common elements !"; }

Cartesian Product

#include<bits/stdc++.h> using namespace std; int main() {     int x,y,i,j;     cout<<"enter the size of first set : ";     cin>>x;     cout<<"Enter the size of second set : ";     cin>>y;     int a[x], b[y];     cout<<"Enter the elements of first set : ";     for(i=0;i<x;i++)     cin>>a[i];     cout<<"Enter the elements of second set : ";     for(i=0;i<y;i++)     cin>>b[i];     cout<<"\nCartesian product from A to B is :";      for(i=0;i<x;i++)         for(j=0;j<y;j++)             cout<<"{"<<a[i]<<","<<b[j]<<"}";     cout<<"\nCartesian product from B to A is :";     for(i=0;i<y;i++)         for(j=0;j<x;j++)           ...

AND OR NOT Truth table

#include<stdio.h> int main() { int a[2][2],b[2][2],c[2]; int i,j; for(i=0;i<=1;i++) { for(j=0;j<=1;j++)  {  a[i][j]=(i&&j);  b[i][j]=(i||j);  } } for(i=0;i<=1;i++) { c[i]=(!i); } printf("\nThe Truth Table for AND Gate( && ) is..\n"); printf(" A B : C=A&&B\n"); for(i=0;i<=1;i++) {  for(j=0;j<=1;j++)  {  printf(" %d %d : %d\n",i,j,a[i][j]);  } } printf("\nThe Truth Table for OR Gate( || ) is..\n"); printf(" A B : C=A||B\n"); for(i=0;i<=1;i++) {  for(j=0;j<=1;j++)  {  printf(" %d %d : %d\n",i,j,b[i][j]);  } } printf("\nThe Truth Table for NOT Gate (!) is..\n"); printf(" A : B=!A\n"); for(i=0;i<=1;i++) {  printf(" %d : %d\n",i,c[i]); } return 0; }

Prim's Algo

#include<bits/stdc++.h> using namespace std; int adj[20][20]; void print_mst(int parent[], int key[], int x) {     int sum=0,i;     for(i=1;i<=x;i++)     {         cout<<"VERTEX:"<<i<<" "<<"PARENT"<<parent[i]<<" "<<"weight"<<key[i]<<endl;         sum+=key[i];     }     cout<<"\nMin weight is : "<<sum; } int min_key(int key[],bool mstset[], int x) {     int mn=9999999, min_index,i;     for(i=1;i<=x;i++)     {         if(mstset[i]==false && key[i]<mn)         {             mn=key[i];             min_index=i;         }     }     return min_index; } void prim_MST(int x) {     int parent[x+1], key[x+1],i,u,v;     bool mstset[x+1]...

Qucik Sort

#include<stdio.h> #include<conio.h> int partitions(int a[], int lb, int ub) {     int pivot=a[lb],start=lb, end=ub,temp;     while(start<end)     {         while(a[start]<=pivot)             start+=1;         while(a[end]>pivot)             end-=1;         if(start<end)         {             temp=a[start];             a[start]=a[end];             a[end]=temp;         }     }     temp=a[lb];     a[lb]=a[end];     a[end]=temp;     return end; } void quick_sort(int a[], int lb, int ub) {     int loc;     if(lb<ub)     {         loc=partitions(a,lb,ub);         quick_sort...