-->

Lab works-7(struct and union)

 question:

---------------------------------------------------------------------------------------------------------------

q1.)Write  a program considering user defined variable distance with members:feet and inch.

Calculate the sum two different distances.

q2)Write  a program considering user defined variable time with members:hh,mm and ss.

Calculate the sum two different times.

q3)Consider structure student with members:sid,name and height. Write a program that takes maximum 100 records,rearrange the records in ascending order on the basis of height and prints them.

q4)Write a program that takes empid,name and salary of 100 employees and search a particular employee's record in the array of structure of the basis of empid.Also find the position of the record.


q5)Define structure type employee with members: empid,name ,post and bsalary.Write a program to print the record of the employee who has maximum salary.

-------------------------------------------------------------------------------------------------------------------

ans:-

q1.)Write  a program considering user defined variable distance with members:feet and inch.

Calculate the sum two different distances.

ans:

code:

#include<stdio.h>                           //header file

struct distance                             //struct tag
{
    int inch;                               //first member
    int feet;                               //second member
}d1,d2,tot;                                  //variables to access members
int main()
{
    printf("enter first inch and feet\n");
    scanf("%d%d",&d1.inch,&d1.feet);          //gets data
    printf("enter first inch and feet\n");
    scanf("%d%d",&d2.inch,&d2.feet);          //gets data
    tot.inch=(d1.inch+d2.inch)%12;               //returns left part of inch
    tot.feet=d1.feet+d2.feet+(d1.inch+d2.inch)/12;//returns total feet
    printf("total inch=%d feet=%d\n",tot.inch,tot.feet);//prints result
    return 0;
}


q2)Write  a program considering user defined variable time with members:hh,mm and ss.

Calculate the sum two different times.

ans:

code:

//program to add two times given in hrs,mm and ss using struct
#include <stdio.h>
struct timeaddition
{
    int hrs;
    int mm;
    int ss;
}time1,time2,ttime;
int main()
{
    int totalmm;
    printf("enter first time in hrs,minutes and seconds\n");
    scanf("%d%d%d",&time1.hrs,&time1.mm,&time1.ss);
    printf("enter second time in hrs,minutes and seconds\n");
    scanf("%d%d%d",&time2.hrs,&time2.mm,&time2.ss);
    totalmm=time1.mm+time2.mm+(time1.ss+time2.ss)/60;
    ttime.ss=(time1.ss+time2.ss)%60;
    ttime.mm=totalmm%60;
    ttime.hrs=time1.hrs+time2.hrs+totalmm/60;
    printf("total hrs=%d minutes=%d and seconds=%d\n",ttime.hrs,ttime.mm,ttime.ss);
    return 0;
}

q3)Consider structure student with members:sid,name and height. Write a program that takes maximum 100 records,rearrange the records in ascending order on the basis of height and prints them.

ans:

code:

//program to sort records using struct from a to z
#include<stdio.h>
#include<conio.h>
struct detail
{
int sid;
char sname[100];
int sheight;
}var[100],temp;

int main()
{
int i,j,n;
printf("now enter records of students\n");
for(i=0;i<100;i++)
{
printf("enter student's id.\n");
scanf("%d",&var[i].sid);

printf("enter student's height\n");
scanf("%d",&var[i].sheight);
printf("enter student's name\n");
scanf("%s",var[i].sname);

}
printf("now sorting from a to z on the basis of  height\n");
for(i=0;i<100;i++)
 {
     for(j=i+1;j<100;j++)
       {
            if((var[i].sheight>var[j].sheight)
                  {
                      temp=var[i];
                      var[i]=var[j];
                     var[j]=temp;
                   }
         }
}
printf("sorted data (from a to z i.e. ascending order )on the basis of height are:\n:");
  for(i=0;i<100;i++)
{
printf("student's id=%d, student's height=%d, student name=%s, \n",var[i].sid,var[i].sheight,var[i].sname);

}
getch();

return 0;
}

q4)Write a program that takes empid,name and salary of 100 employees and search a particular employee's record in the array of structure of the basis of empid.Also find the position of the record.

ans:

code:

// program to search data in given  detail of some  employees       
#include <stdio.h>                                
struct data                                      
{

     int empid;     
    char empname[80];                            
     float empsalary;                          
                                         
}access[100];                 
int main()
{
   int n,i,loc=0;                                         //variable declaration
   int empsearch;
    printf("enter records\n");
 
   for(i=0;i<100;i++)
   {                                                
    puts("enter employee id\n");
   scanf("%d",&access[i].empid);                      
   puts("enter employee name \n");
   scanf("%s",access[i].empname);                       
   puts("enter employee salary\n");
   scanf("%f",&access[i].empsalary);    
   }
   printf("enter emp id to be searched \n");
   scanf("%d",&empsearch);         //data input to be searched
   for(i=0;i<100;i++)
   {
       if(access[i].empid==empsearch)//data comparison
        {
           loc=1;
           break;                    //terminating the loop
        }
   }
   if(loc==1)
   {
       printf("data found and location=%d",i);//printing the location of data
       printf("data is,empid=%d,\tempname=%s,\temp salary=%f\n",access[i].empid,access[i].empname,access[i].empsalary);


   }
   else
   {
       printf("not found\n");
   }

    return 0;
}

q5)Define structure type employee with members: empid,name ,post and bsalary.Write a program to print the record of the employee who has maximum salary.

ans:-

code:


// program to search data in given  detail of some  employees       
#include <stdio.h>                                
struct data                                      
{

     int empid;     
    char empname[80]; 

    char emppost[80];                           
     float empsalary;                          
                                         
}access[100];                 
int main()
{
   int i,k,loc=0;                                         
    printf("enter records\n");
 
   for(i=0;i<100;i++)
   {                                                
    puts("enter employee id\n");
   scanf("%d",&access[i].empid);                      
   puts("enter employee name \n");
   scanf("%s",access[i].empname);     

    puts("enter employee post \n");

   scanf("%s",access[i].emppost);                       
   puts("enter employee salary\n");
   scanf("%f",&access[i].empsalary);    
   }
   k=access[0].empsalary;
   for(i=0;i<n;i++)
   {
       if(k<access[i].empsalary)//data comparison
        {
           k=access[i].empsalary; 

            loc=i;          

        }
   }
   
       printf("data with maximum salary is\n");

printf("empid=%d,\empname=%s,emp post=%s,\temp salary=%f\n",access[loc].empid,access[loc].empname,access[loc].emppost,access[loc].empsalary);


  

    return 0;
}

case study:

Define structure type book with members: bid,name ,name,edition,author,price.Write a program to print the record of the employee who has maximum salary.Write a small program using following interface.

1. input book record

2.view the book record

3.search a book using book number as parameter.

4. Exit

ans:-

#include <stdio.h>

#include<stdlib.h>

int size;

int search;

void book_input(int n);

void book_display(int n);

void book_search(int booksearch);

struct book

{

    int bid;

    char name[100];

    char author[100];

    char edition[100];

    float price;

}v[100];

int main()

{

    int ch,search;

    printf("enter the size of 'n' as total record\n");

                scanf("%d",&size);

    printf("we have following menu\n");

     while(1)

    {

                    printf("\n|------------------------------------------|\n");

                printf("\n|1--->to input book record                 |\n");

                    printf("\n|2--->to view book record                  |\n");

                    printf("\n|3--->to Search a book Record              |\n");

                    printf("\n|5--->to Exit                              |\n");

  printf("\n|------------------------------------------|\n");

                    printf("\n|Please input your choice:                 |\n");

                    scanf("%d",&ch);

                    switch(ch)

                    {         

                        case 1: printf("\nwe are going to input book record \n");

                                                        book_input( size);                 

                                                                break;

                                    case 2: printf("books are=\n");

                                                                book_display(size);                                                        

                                                                break;

                                    case 3:

                                        printf("we are going to search a book\n");

                                        printf("enter book id to be searched\n");

                                        scanf("%d",&search);

                                                    book_search(search);                               

                                                                break;

                                    default:

                                        printf("this choice is not in the list\n");

                                      printf("\nthank you for using program! now exiting...");

                                        exit(0);

                    }

                }

}

void book_input(int n)

{

    int i;

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

    {

        printf("enter book id\n");

        scanf("%d",&v[i].bid);

        printf("enter book name\n");

        scanf("%s",v[i].name);

        printf("enter author name\n");

        scanf("%s",v[i].author);

        printf("enter edition \n");

        scanf("%s",v[i].edition);

        printf("enter book price\n");

        scanf("%f",&v[i].price);

    }

}

void book_display(int n)

{

    int i;

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

    {

        printf("book id=%d\n",v[i].bid);

        printf("book name=%s\n",v[i].name);

        printf("book author=%s\n",v[i].author);

        printf("book edition=%s\n",v[i].edition);

        printf("book price=%f\n",v[i].price);

        printf("------------------------------\n");

    }

}

void book_search(int booksearch)

{

    int i,flag=0;

    for(i=0;i<=size-1;i++)

    {

        if(booksearch==v[i].bid)

            flag=1;

    }

    if(flag==1)

        {

            printf("book id found\n");

        }

        else

        {

            printf("book not found\n");

        }

}

 

   

 


No comments:

Post a Comment