Powered By Blogger

Monday, May 2, 2011

Extended Pigeonhole Principle

#include<stdio.h>
#include<conio.h>
void main()
{
    int p,ph,res=0;
    printf("Enter the no. of pigeons : ");
    scanf("%d",&p);
    printf("Enter the no. of pigeon holes : ");
    scanf("%d",&ph);
    res=((p-1)/ph)+1;
    printf("There must be at least %d pigeons in each pigeon hole.\n",res);
    getch();
}

Program for funtion analysis

//This program takes as input the function in matrix form and checks
// whether the given function is injective,surjective or bijective.
#include<stdio.h>
#include<conio.h>
void main()
{
    int mat[10][10],i,j,in=0,sur=0,bij=0,m,tmp,p,q;
    printf("Enter the order of the matrix :");
    scanf("%d",&m);
    printf("Enter the matrix row-wise : \n");
    for(i=0;i<m;i++)
        for(j=0;j<m;j++)
            scanf("%d",&mat[i][j]);
    //To check whether the given function is valid or not
    for(i=0;i<m;i++)
    {
        tmp=0;
        for(j=0;j<m;j++)
            if(mat[i][j]==1)
                tmp++;
        if(tmp!=1)
        {
            printf("The given function is not valid.\n");
            break;
        }
    }
    //To print the given matrix
    printf("The given function matrix is : \n\n");
    for(i=0;i<m;i++)
        printf("\t%d",i+1);
    printf("\n\n    --------------------------");
    for(i=0;i<m;i++)
    {
        printf(" \n   |\n %d |",i+1);
        for(j=0;j<m;j++)
            printf("\t%d",mat[i][j]);
    }
    printf("\n");
    //To check if its injective
    for(i=0;i<m;i++)
    {
        in=0;
        for(j=0;j<m;j++)
            if(mat[i][j]==1)
                in++;
        if(in>1)
        {
            printf("\nThe given funtion is not injective.\n");
            break;
        }
    }
    if(in==1)
    {
        p=1;
        printf("\nThe given function is injective.\n");
    }
    //To check if its surjective
    for(i=0;i<m;i++)
    {
        sur=0;
        for(j=0;j<m;j++)
            if(mat[i][j]==1)
                sur++;
        if(sur==1)
        {
            printf("\nThe function is not surjective.\n");
            break;
        }
    }
    if(sur>1)
    {
        q=1;   
        printf("\nThe function is surjective\n");
    }
    if(p==1 && q==1)
        printf("\nThe function is bijective.\n");
    else
        printf("\nThe function is not bijective.\n");
    getch();
}