The Programming Project: C
Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Wednesday, October 8, 2014

Binary Search Tree (BST) : Finding Leaves : C Code

 Binary Search Tree (BST) : Finding Leaves : C Code

The following C code prints all the leaves nodes of a Binary Search Tree. A leaf-node is a node which has not left or right child. The code first scans the root node and looks whether it has a left and right child or not. If the condition is satisfied, it  is a leave node or else we recursively examine the left and right child.

#include<stdlib.h>
#include<stdio.h>
struct binaryTree {
   int key;
   struct binaryTree *right, *left;
};

typedef struct binaryTree node;

void insert(node **tree, int val);
void printLeaves(node *root);

int main() {
   node *root;
   int i,val,search;
   root = NULL;
   while(1) {
      printf("\n Enter the value (-1 to exit):");
      scanf("%d",&val);   
      if( val == -1)
          break;
      insert(&root, val);
   }
  
   printLeaves(root);
   printf("\n");
   
   return 0;
}
void printLeaves(node *root) {
    if (root->right == NULL && root->left == NULL) {
        printf("\n Leaf Node: %d",root->key);
        return;
        }
    if(root->right != NULL)    
        printLeaves(root->right);
    if(root->left != NULL)   
    printLeaves(root->left);
    }
void insert(node **tree, int val) {
   if((*tree) == NULL) {
      *tree = (node *)malloc(sizeof(node));
      (*tree)->key = val;
      (*tree)->right=(*tree)->left=NULL;
      return;
   }
   if(val < (*tree)->key)
      insert(&(*tree)->left, val);
   else if(val > (*tree)->key)
      insert(&(*tree)->right, val);
    }

Monday, October 6, 2014

Addition of positive numbers using linked list: C code

Addition of large positive integers in C is quite difficult since there will be overflow error once the range of 'int' is crossed. But this problem can be overcome by the use of linked list. Happy addition! 



#include<stdio.h>
#include<stdlib.h>
struct numberList {
    int data;
    struct numberList *nextDigit;
    struct numberList *revDigit;
    };
    typedef struct numberList node;
void inputNumber(node*);
void display(node*,int);   
void addNumber(node*,node*);
int main() {
   
    node *firstNumber,*secondNumber,*sum;
    firstNumber = (node *)malloc(sizeof(node));
    secondNumber = (node *)malloc(sizeof(node));
    printf("\n Enter the first number(one digit at a time) type -1 to terminate the number:");
    printf("\n Enter the digit:");
    scanf("%d",&firstNumber->data);
    inputNumber(firstNumber);
   
    printf("\n Enter the second number(one digit at a time) type -1 to terminate the number:");
    printf("\n Enter the digit:");
    scanf("%d",&secondNumber->data);
    inputNumber(secondNumber);
   
    display(firstNumber,1);
    display(secondNumber,2);
   
    addNumber(firstNumber,secondNumber);   
    return 0;
    }   
void addNumber(node *p,node *q) {
    int len1=0,len2=0,min,i,once=0;
    while (p->nextDigit !=NULL) {
        len1++;
        p = p->nextDigit;
        }
    p = p->revDigit;   
    while (q->nextDigit !=NULL) {
        len2++;
        q = q->nextDigit;
        }
    q = q->revDigit;
    min = len1 <= len2 ? len1:len2;
    node *sum,*tmp,*sum1;   
    sum = (node *)malloc(sizeof(node));
    sum1 = sum;
    sum->revDigit = NULL;
    for ( i = 1; i <= min; i++) {
        sum->data = (p->data+q->data+once)%10;
        tmp = sum;
        once = (p->data+q->data+once)/10;
        p=p->revDigit;
        q=q->revDigit;
        sum->nextDigit = (node *)malloc(sizeof(node));
        sum = sum->nextDigit;
        sum->revDigit=tmp;
        }
    if ( len1 == len2 && once !=0) {
        sum->data = once;
        sum->revDigit=tmp;
        }   
    if (len1 > len2) {
        int t = len1-len2;
        sum = sum->revDigit;
        for ( i = 1; i<=t; i++) {
            tmp = sum;
            sum->nextDigit = (node *)malloc(sizeof(node));
            sum = sum->nextDigit;
            sum->revDigit=tmp;
            sum->data = (p->data+once)%10;
            once = (p->data+once)/10;
            p = p->revDigit;
            } 
        if ( once !=0) {
            tmp = sum;
            sum->nextDigit = (node *)malloc(sizeof(node));
            sum = sum->nextDigit;
            sum->data = once;
            sum->revDigit=tmp;
            }
               
        }   
    if (len1 < len2) {
        sum = sum->revDigit;
        int t = len2-len1;
        for ( i = 1; i<=t; i++) {
            tmp = sum;
            sum->nextDigit = (node *)malloc(sizeof(node));
            sum = sum->nextDigit;
            sum->revDigit=tmp;
            sum->data = (q->data+once)%10;
            once = (q->data+once)/10;
            q = q->revDigit;
            }
        if ( once !=0) {
            tmp = sum;
            sum->nextDigit = (node *)malloc(sizeof(node));
            sum = sum->nextDigit;
            sum->data = once;
            sum->revDigit=tmp;
            }   
        }   
    sum->nextDigit = NULL;   
    printf("\n Sum of the numbers is:");
    while (sum1->nextDigit !=NULL) {
        //printf("\n%d",sum1->data);
        sum1 = sum1->nextDigit;
        }
    //sum1 = sum1->revDigit;   
    while (sum1 !=NULL) {
        printf("%d",sum1->data);
        sum1 = sum1->revDigit;
        }
    printf("\n");   
    }   
void display(node *p,int tx) {
    if (tx==1)
        printf("\nFirst Number\n");
    else
        printf("\nSecond Number\n");
    while (p->nextDigit !=NULL) {
        printf("%d",p->data);
        p = p->nextDigit;
        }
    p = p->revDigit;   
    /*while (p !=NULL) {
        //printf("%d",p->data);
        p = p->revDigit;
        }*/   
    }   
void inputNumber(node *p) {
    node *q;
    p->revDigit = NULL;
    while (1) {
        q = p;
        p->nextDigit = (node *)malloc(sizeof(node));
        p = p->nextDigit;
        printf("\n Enter the digit:");
        scanf("%d",&p->data);
        p->revDigit = q;
        if (p->data == -1)
            break;
        }
    p->nextDigit = NULL;
    }     

Tuesday, September 30, 2014

Largest product in a grid : Problem 11 Euler Project

In the 20×20 grid below, four numbers along a diagonal line have been marked in red.
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

The product of these numbers is 26 × 63 × 78 × 14 = 1788696.
What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?

Largest product in a grid : Problem 11 Source: Euler Project
I have saved the matrix in a file "11.txt"

C Code:

#include<stdio.h>
#include<malloc.h>
int main()
{
int **matrix,row,col,maxProduct=1;
int i,j;
int s1,s2,s3,s4,s5,s6,s7,s8;
FILE *fp;
matrix = (int **)malloc(20*sizeof(int*));
for(i=0;i<20;i++)
    matrix[i]=(int *)malloc(20*sizeof(int));
fp = fopen("11.txt","r");
while(!feof(fp)) {
    for(row=0;row<20;row++) {
        for(col=0;col<20;col++) {
        fscanf(fp,"%d",&matrix[row][col]);
            }
        }
    }
for(i=0;i<20;i++) {
    s1=s2=s3=s4=s5=s6=s7=s8=1;
    for(j=0;j<20;j++) {
        // left-sum
        if(j-3 < 0)
            s1 = 1;
        else
            s1 = matrix[i][j]*matrix[i][j-1]*matrix[i][j-2]*matrix[i][j-3];
        if (maxProduct < s1)   
            maxProduct =s1;
        // right-sum   
        if(j+3 >= 20)
            s2 = 1;
        else
            s2 = matrix[i][j]*matrix[i][j+1]*matrix[i][j+2]*matrix[i][j+3];   
        if (maxProduct < s2)   
            maxProduct =s2;   
        // up-sum
        if(i-3 <0)
            s3 = 1;
        else
            s3 = matrix[i][j]*matrix[i-1][j]*matrix[i-2][j]*matrix[i-3][j];   
        if (maxProduct < s3)   
            maxProduct =s3;           
        // down-sum
        if(i+3 >= 20)
            s4 = 1;
        else
            s4 = matrix[i][j]*matrix[i+1][j]*matrix[i+2][j]*matrix[i+3][j];   
        if (maxProduct < s4)   
            maxProduct =s4;   
        // diag-up-left
        if(j-3 < 0 || i - 3 < 0)
            s5 = 1;
        else
            s5 = matrix[i][j]*matrix[i-1][j-1]*matrix[i-2][j-2]*matrix[i-3][j-3];   
        if (maxProduct < s5)   
            maxProduct =s5;   
        // diag-up-right
        if(i-3 < 0 || j + 3 >= 20)
            s6 = 1;
        else
            s6 = matrix[i][j]*matrix[i-1][j+1]*matrix[i-2][j+2]*matrix[i-3][j+3];
        if (maxProduct < s6)   
            maxProduct =s6;   
        // diag-down-left       
        if(i+3 >= 20 || j-3 < 0)
            s7 = 1;
        else
            s7 = matrix[i][j]*matrix[i+1][j-1]*matrix[i+2][j-2]*matrix[i+3][j-3];
        if (maxProduct < s7)   
            maxProduct =s7;   
        // diag-down-right       
        if(i+3 >= 20 || j+3 >=20)
            s8 = 1;
        else
            s8 = matrix[i][j]*matrix[i+1][j+1]*matrix[i+2][j+2]*matrix[i+3][j+3];   
        if (maxProduct < s8)   
            maxProduct =s8;           
        }
       
    }   
printf("\nLargest Product is %d",maxProduct);   
fclose(fp);           
return 0;   
}

Thursday, April 17, 2014

Binomial Coefficient using recursion in Python and C language





C Code
#include<stdio.h>
long int Binomial(long int, long int);
int main()
    {
    long int n,k;
    printf("\n Enter the value of n and k, n is greater than k:");
    scanf("%ld %ld",&n,&k);
    while( k < 0 || n < 1 || k > n) {
        printf("\n Wrong input, enter again:");
        scanf("%ld %ld",&n,&k);
        }
    printf("\n Value of %ldC%ld: %ld \n", n,k,Binomial(n,k));
    return 0;
    }
long int Binomial(long int n, long int k)
    {
    return (k <= 1 ? (k == 0 ? 1 : n) : (n*Binomial(n-1,k-1))/k);
    } 


Python Code
 
#File: binomial.py
#A simple program to calculate nCk using recursion
def main():
    print "Binomial Coefficient"
    n = input("Enter the value of n:")
    k = input("Enter the value of k:")
    x,y= n,k
    def binomial(n,k):
        if k < 2:
            if k == 1:
                return n
            else:
                return 1
        else:
            return (n*binomial(n-1,k-1))/k
   
    print "Value of nCk is",binomial(x,y)
   
main()


OUTPUT:

administrator@ubuntu:~/python$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:26)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import binomial
Binomial Coefficient
Enter the value of n:1200
Enter the value of k:600
Value of nCk is 396509646226102014036716014862605729531608195706791651137874888645453416610941265150218719101931467943643355545203450497992759241509133380338379948816055787676920090991204851973167965845932778899299658455186568000803781988756668261491937388063732393433822461878746138860879613812823280622769290795808335597761702284943981759657834318699226167559487050708295600
>>>

/* To calculate the Binomial Coeffcient using recursion */
/* We have use the formula nCk = (n/k)*{(n-1)C(k-1)} */

Thursday, April 10, 2014

Rational Numbers representation Using classes and structures


Basic operations on Rational Numbers using C, C++ and Java.
The same program is written in the above languages, helping you to grasp the basic principles of classes and objects in C++ and Java. Students moving from C++ to Java will be able to relate the similarities at the same time the major differences between them.

Code in C
#include<stdio.h>
#define TRUE 1
#define FALSE 0
typedef struct
{
int numerator;
int denominator;
} RATIONAL;
void multiply(RATIONAL *frst,RATIONAL *scnd);
void add(RATIONAL *frst,RATIONAL *scnd);
void subtract(RATIONAL *frst,RATIONAL *scnd);
int equality(RATIONAL *frst,RATIONAL *scnd);
void divide(RATIONAL *frst,RATIONAL *scnd);
void reduce(RATIONAL *rat);

int main()
{
RATIONAL x,y;
int choice;
printf("\n Enter the numerator and denominator of the first rational:");
scanf("%d %d",&x.numerator,&x.denominator);
printf("\n Enter the numerator and denominator of the second rational:");
scanf("%d %d",&y.numerator,&y.denominator);
//printf("\n Reduced form of the input rationals are:");
reduce(&x);
//printf("\n %d / %d ",x.numerator,x.denominator);
reduce(&y);
//printf("\n %d / %d ",y.numerator,y.denominator);
printf("\n Press 1 for addition:");
printf("\n Press 2 for subtraction:");
printf("\n Press 3 for multiplication:");
printf("\n Press 4 for divison:");
printf("\n Press 5 to check for equatilty");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
add(&x,&y);
break;
case 2:
subtract(&x,&y);
break;
case 3:
multiply(&x,&y);
break;
case 4:
divide(&x,&y);
break;
case 5:
equality(&x,&y)==TRUE ? printf("\n They are equal:\n") : printf("\n They are unequal:\n");
break;
default:
break;
}
return 0;
}
void divide(RATIONAL *frst,RATIONAL *scnd)
{
RATIONAL sum;
sum.numerator = (frst->numerator)*(scnd->denominator);
sum.denominator = (frst->denominator)*(scnd->numerator);
reduce(&sum);
printf("\n After dividing the new rational is: %d / %d \n",sum.numerator,sum.denominator);
}
void multiply(RATIONAL *frst,RATIONAL *scnd)
{
RATIONAL sum;
sum.numerator = (frst->numerator)*(scnd->numerator);
sum.denominator = (frst->denominator)*(scnd->denominator);
reduce(&sum);
printf("\n Product of the rationals is: %d / %d \n",sum.numerator,sum.denominator);
}
int equality(RATIONAL *frst,RATIONAL *scnd)
{
return ( (frst->numerator == scnd->numerator && frst->denominator == scnd->denominator) ? TRUE : FALSE );
}
void subtract(RATIONAL *frst,RATIONAL *scnd)
{
int lcm=1,i;
for(i=1;;i++)
{
if (frst->denominator % i == 0 && scnd->denominator % i== 0)
break;
}
lcm = ((frst->denominator)*(scnd->denominator))/i;
RATIONAL sum;
sum.numerator = (frst->numerator)*(lcm/frst->denominator)-(scnd->numerator)*(lcm/scnd->denominator);
sum.denominator = lcm;
reduce(&sum);
printf("\n Sum of the rationals is: %d / %d \n",sum.numerator,sum.denominator);
}
void add(RATIONAL *frst,RATIONAL *scnd)
{
int lcm=1,i;
for(i=1;;i++)
{
if (frst->denominator % i == 0 && scnd->denominator % i== 0)
break;
}
lcm = ((frst->denominator)*(scnd->denominator))/i;
RATIONAL sum;
sum.numerator = (frst->numerator)*(lcm/frst->denominator)+ (scnd->numerator)*(lcm/scnd->denominator);
sum.denominator = lcm;
reduce(&sum);
printf("\n Sum of the rationals is: %d / %d \n",sum.numerator,sum.denominator);
}
void reduce(RATIONAL *rat)
{
int a,b,rem;
if(rat->numerator > rat->denominator) {
a = rat->numerator;
b = rat->denominator;
}
else {
a = rat->denominator;
b = rat->numerator;
}
while(b!=0) {
rem = a%b;
a = b;
b = rem;
}
rat->numerator /= a;
rat->denominator /= a;
}



Code in C++

#include<iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
class Rational {
private:
int numerator;
int denominator;
public:
Rational() { numerator = 1; denominator = 1;}
void setValues()
{
cout<<"\n Enter the numerator:";
cin>>numerator;
cout<<"\n Enter the denominator:";
cin>>denominator;
}
void dispValue()
{
cout<<"\n"<<numerator<<"/"<<denominator<<"\n";
}
void reduce();
void add(Rational r);
void subtract(Rational r);
void multiply(Rational r);
void divide(Rational r);
int equality(Rational r);
};
int Rational::equality(Rational r)
{
return ( (numerator == r.numerator && denominator == r.denominator) ? TRUE : FALSE );
}
void Rational::divide(Rational r)
{
Rational sum;
sum.numerator = (numerator)*(r.denominator);
sum.denominator = (denominator)*(r.numerator);
sum.reduce();
cout<<"\n On divison the rationals:";
sum.dispValue();
}
void Rational::multiply(Rational r)
{
Rational sum;
sum.numerator = (numerator)*(r.numerator);
sum.denominator = (denominator)*(r.denominator);
sum.reduce();
cout<<"\n Product of the rationals is:";
sum.dispValue();
}
void Rational::subtract(Rational r)
{
int lcm=1,i;
for(i=1;;i++)
{
if (denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm = ((denominator)*(r.denominator))/i;
Rational sum;
sum.numerator = numerator*(lcm/denominator)-(r.numerator)*(lcm/r.denominator);
sum.denominator = lcm;
sum.reduce();
cout<<"\n Difference of the rationals is:";
sum.dispValue();
}
void Rational::add(Rational r)
{
int lcm=1,i;
for(i=1;;i++)
{
if (denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm = ((denominator)*(r.denominator))/i;
Rational sum;
sum.numerator = numerator*(lcm/denominator)+(r.numerator)*(lcm/r.denominator);
sum.denominator = lcm;
sum.reduce();
cout<<"\n Sum of the rationals is:";
sum.dispValue();
}
void Rational::reduce()
{
int a,b,rem;
if(numerator > denominator) {
a = numerator;
b = denominator;
}
else {
a = denominator;
b = numerator;
}
while(b!=0) {
rem = a%b;
a = b;
b = rem;
}
numerator /= a;
denominator /= a;
}
int main()
{
int choice;
Rational x,y;
x.setValues();
y.setValues();
x.reduce();
y.reduce();
cout<<"\n Press 1 for addition:";
cout<<"\n Press 2 for subtraction:";
cout<<"\n Press 3 for multiplication:";
cout<<"\n Press 4 for divison:";
cout<<"\n Press 5 to check for equatilty";
cout<<"\n Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
x.add(y);
break;
case 2:
x.subtract(y);
break;
case 3:
x.multiply(y);
break;
case 4:
x.divide(y);
break;
case 5:
x.equality(y)==TRUE ? cout<<"\n They are equal:\n" : cout<<"\n They are unequal:\n";
break;
default:
break;
}
return 0;
}


Code in JAVA

import java.util.*;
public class Rational
{
public static void main(String[] args)
{
int choice;
Scanner in = new Scanner(System.in);
OperationRational x = new OperationRational();
OperationRational y = new OperationRational();
x.setValues();
y.setValues();
x.reduce();
y.reduce();
System.out.println("You have entered the following rationals:");
x.dispValue();
y.dispValue();
System.out.println("Press 1 for addition:");
System.out.println("Press 2 for subtraction:");
System.out.println("Press 3 for multiplication:");
System.out.println("Press 4 for divison:");
System.out.println("Press 5 to check for equatilty");
System.out.println("Enter your choice:");
choice = in.nextInt();
switch(choice)
{
case 1:
x.add(y);
break;
case 2:
x.subtract(y);
break;
case 3:
x.multiply(y);
break;
case 4:
x.divide(y);
break;
case 5:
if (x.equality(y) == true)
System.out.println("They are equal:");
else
System.out.println("They are unequal:");
break;
default:
break;
}
}
}
class OperationRational
{
OperationRational()
{
numerator = 1;
denominator = 1;
}
public void setValues()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the numerator:");
numerator = in.nextInt();
System.out.println("Enter the denominator:");
denominator = in.nextInt();
}
public void dispValue()
{
System.out.println(numerator+"/"+denominator);
}
public void reduce()
{
int a,b,rem;
if(numerator > denominator) {
a = numerator;
b = denominator;
}
else {
a = denominator;
b = numerator;
}
while(b!=0) {
rem = a%b;
a = b;
b = rem;
}
numerator /= a;
denominator /= a;
}
public void add(OperationRational r)
{
int lcm=1,i;
for(i=1;;i++)
{
if (denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm = ((denominator)*(r.denominator))/i;
OperationRational sum = new OperationRational();
sum.numerator = numerator*(lcm/denominator)+(r.numerator)*(lcm/r.denominator);
sum.denominator = lcm;
sum.reduce();
System.out.println("Sum of the rationals is:");
sum.dispValue();
}
public void subtract(OperationRational r)
{
int lcm=1,i;
for(i=1;;i++)
{
if (denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm = ((denominator)*(r.denominator))/i;
OperationRational sum = new OperationRational();
sum.numerator = numerator*(lcm/denominator)-(r.numerator)*(lcm/r.denominator);
sum.denominator = lcm;
sum.reduce();
System.out.println("Difference of the rationals is:");
sum.dispValue();
}
public void multiply(OperationRational r)
{
OperationRational sum = new OperationRational();;
sum.numerator = (numerator)*(r.numerator);
sum.denominator = (denominator)*(r.denominator);
sum.reduce();
System.out.println("Product of the rationals is:");
sum.dispValue();
}
public void divide(OperationRational r)
{
OperationRational sum = new OperationRational();;
sum.numerator = (numerator)*(r.denominator);
sum.denominator = (denominator)*(r.numerator);
sum.reduce();
System.out.println("On divison the rationals:");
sum.dispValue();
}
public boolean equality(OperationRational r)
{
return ( (numerator == r.numerator && denominator == r.denominator) ? true : false );
}
private int numerator;
private int denominator;
}

Get the pdf file, click here

Tuesday, September 24, 2013

Combination in Lexicographical Order




#include<stdio.h>
#include<malloc.h>
#include<conio.h>
int main()
{
char alpha[]={'?','A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int n,i,pos,r,tmp,j,flag=1;
long int tot=0;
char *combination;
printf("\n Enter the number of objects (max value is 26):");
scanf("%d",&n);
printf("\n Enter the value of r ( 1<= r <= n):");
scanf("%d",&r);
combination =(char *)malloc(n*sizeof(char));
for(i=1;i<=n;i++)
combination[i]=alpha[i];
printf("\n The combinations are (is):\n");
printf("\t\t      ");
for(i=1;i<=r;i++)
printf("%c",combination[i]);
tot=1;
while(flag)
{
for(i=r;i>=1;i--)
{
if(combination[i]-64==n-r+i)
{
}
else
{
pos=i;
break;
}
}
if(i==0)
{
flag=0;
break;
}
for(j=1;j<=26;j++)
if(combination[pos]==alpha[j])
break;
combination[pos]=alpha[j+1];
for(i=pos+1;i<=r;i++)
combination[i]=alpha[(combination[i-1]-64)+1];
printf("\n");
printf("\t\t      ");
for(i=1;i<=r;i++)
printf("%c",combination[i]);
tot++;
}
printf("\n Total number of combination is: %ld",tot);
return 0;
}


Monday, September 16, 2013

Intersection of Sets



To find intersection of two sets through C

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void inter_set(int *s1,int*s2,int n,int m);
int main()
{
int n,m,i;
int *s1,*s2;
printf("\n Enter the number of elements in the sets:");
scanf("%d %d",&n,&m);
s1=(int *)malloc(n*sizeof(int));
s2=(int *)malloc(m*sizeof(int));
printf("\n Enter the elements of the set 1:");
for(i=0;i<n;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s1[i]);
}
printf("\n Enter the elements of the set 2:");
for(i=0;i<m;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s2[i]);
}
inter_set(s1,s2,n,m);
return 0;
}
void inter_set(int *s1,int*s2,int n,int m)
{
int *s,i=0,j,k;
int flag=0;
s=(int *)malloc((m<n ? m: n)*sizeof(int));
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
if(s2[j]==s1[k])
{
flag=1;
break;
}
}

if(flag==1)
{
s[i]=s2[j];
         i++;
flag=0;
}
 if(flag==0)
continue;
 }
 if(i==0)
printf("\n Intersection is Null:");
else
{
  printf("\n Intersection of the entered sets is:");
  printf("\n { ");
  for(j=0;j<i-1;j++)
printf(" %d,",s[j]);
  printf(" %d }\n ",s[i-1]);
}


return;
}

Union of Sets


This C program finds the Union of two Sets (atleast one of is non-empty)
#Task- Apply recursion to the program below to find Union of finite number of sets

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void union_set(int *s1,int*s2,int n,int m);
int main()
{
int n,m,i;
int *s1,*s2;
printf("\n Enter the number of elements in the sets:");
scanf("%d %d",&n,&m);
s1=(int *)malloc(n*sizeof(int));
s2=(int *)malloc(m*sizeof(int));
printf("\n Enter the elements of the set 1:");
for(i=0;i<n;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s1[i]);
}
printf("\n Enter the elements of the set 2:");
for(i=0;i<m;i++)
{
printf("\n Enter the element %d:",i+1);
scanf("%d",&s2[i]);
}
union_set(s1,s2,n,m);
return 0;
}
void union_set(int *s1,int*s2,int n,int m)
{
int *s,i,j,k;
int flag=0;
s=(int *)malloc((m+n)*sizeof(int));
for(i=0;i<n;i++)
s[i]=s1[i];
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
if(s2[j]==s[k])
{
flag=1;
break;
}
}

if(flag==1)
{
flag=0;
continue;
         }
else
      if(flag==0)
{
s[i]=s2[j];
i++;
flag=0;
}
}
  printf("\n Union of the entered sets is:");
  printf("\n { ");
  for(j=0;j<i-1;j++)
printf(" %d,",s[j]);
  printf(" %d }\n ",s[i-1]);



}