Practical 8 by Paxton

 #include<stdio.h>

struct emp

{

    char ename[20];

    int eno;

    char esal[7];

};

int main()

{

    struct emp e[10];

    int st,en,n,i=0,key=0,j=0,mid,flag=0,count=0;

    printf("No of employee u wish to enter:");

    scanf("%d",&n);

    printf("Start Entering the data:\n");

    for(i=0;i<n;i++)

    {

        printf("Employee name:");

        scanf("%s",e[i].ename);

        printf("Employee no:");

        scanf("%d",&e[i].eno);

        printf("Enter sal of emp:");

        scanf("%s",e[i].esal);

    }

    printf("Your entered data:\n");

    printf("Employee name\tEmployee no\tSalary\n");

    for(i=0;i<n;i++)

    {

        printf("%s\t\t",e[i].ename);

        printf("%d\t\t",e[i].eno);

        printf("%s",e[i].esal);

        printf("\n");

    }

    printf("Enter the Employee no to Search His/Her Details:");

    scanf("%d",&key);

    st=0;

    en=n-1;

    while(st!=mid && en!=mid)

    {

        mid=(st+en)/2;

        //printf("Mid:%d",mid);

        count=count+1;

        if(key==e[mid].eno)

        {

           flag=mid;

           //printf("Flag:%d",flag);

           break;

        }

        else if(key>e[mid].eno)

        {

            st=mid+1;

            //printf("st:%d",st);

        }

        else

        {

            en=mid-1;

            //printf("en:%d",en);

        }


    }

    if(flag==0)

    {

        printf("No such Employee Exist!");

    }

    else

    {

        printf("Employee name:%s\n",e[mid].ename);

        printf("Employee no:%d\n",e[mid].eno);

        printf("Enter sal of emp:%s\n",e[mid].esal);

    }

    printf("No of Comparisions:%d",count);

    return 0;

}

Comments

Popular posts from this blog

DAA P4 bfs, dfs

DAA P8 graph coloring

Practical 8 by Karan