-->

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");

        }

}

 

   

 


Lab works-6(user defined function)

 questions:

q1)Write a program to find the simple interest using function.

q2)Write a program find out greatest number among four different numbers.

q3)Write a program to print multiplication table of a number.

q4)Write a program using user defined function to calculate y raise power to x.

q5)Write a program to find factorial value of a number using recursion.

q6)Write a program to print Fibonacci series using recursive function

q7)Write a program to find sum and average of n numbers using array and function.

q8)Write a program to print greatest number among n numbers using array and function.

q9)Write a program to print transpose of matrices of order 2x2 using array and function.

q10)Write a program to print sum of two matrices of order 2x2 using array and function.

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

solutions:-

q1)Write a program to find the simple interest using function.

Code:-

//Write a program to find the simple interest using function.

#include<stdio.h>                      

float simpleinterest(float,float,float);                         

int main()                              

{

float p,t,r;

printf("enter p,t and r\n");

scanf("%f%f%f",&p,&t,&r);

printf("simple interest=%f",simpleinterest(p,t,r) );                                     

return 0;                                      

}

float simpleinterest(float p,float t,float r)                      

{

 float sinterest;

sinterest=p*t*r/100;

return sinterest;

}

q2)Write a program find out greatest number among four different numbers.

ans:

code:

//Write a program find out greatest number among four different numbers.

#include<stdio.h>                      

float greatestnum(float,float,float,float);                         

int main()                              

{

float num1,num2,num3,num4;

printf("enter four numbers\n");

scanf("%f%f%f%f",&num1,&num2,&num3,&num4);

printf("greatest number=%f",greatestnum(num1,num2,num3,num4) );                                     

return 0;                                      

}

float greatestnum(float a,float b,float c,float d)                 

{

 if(a>b&& a>c &&a>d)

  return a;

 else if(b>a&& b>c &&b>d)

  return b;

 else if(c>a&& c>b &&c>d)

  return c;

 else

return d;

}


q3)Write a program to print multiplication table of a number.

ans:

code:

//Write a program to print multiplication table of a number

#include<stdio.h>                      

void mtable(int x);                         

int main()                              

{

int number;

printf("enter a number\n");

scanf("%d",&number);

printf("multiplication table of number %d is\n",number);

   mtable(number);                                 

return 0;                                      

}

void mtable(int x)             

{

 int i;

 for(i=1;i<=10;i++)

{

printf("%d*%d=%d\n",x,i,x*i);

}

}

q4)Write a program using user defined function to calculate y raise power to x.
ans:
code:
//Write a program using user defined function to calculate y raise power to x.
#include<stdio.h> 
#include<math.h>                     
int xtoy(int x,int y);                         
int main()                              
{
int x,y;
printf("enter a number(base))\n");
scanf("%d",&x);
printf("enter a number (power)\n");
scanf("%d",&y);
printf("result of y raise to power of x=%d\n",xtoy(x,y));
return 0;                                      
}
int xtoy(int x,int y)             
{
 int result;
 result=pow(x,y);
 return result;
}

q5)Write a program to find factorial value of a number using recursion.

ans:

code:

//program to find factorial value of a number using recursive function.

#include<stdio.h>                      

int factorial(int);                         

int main()                              

{

int number;

printf("enter a number\n");

scanf("%d",&number);

printf("factorial value=%d",factorial(number));                                     

return 0;                                      

}

int factorial(int n)                            

{

if(n<=0)

{

return 1;

}

else

{

return n*factorial(n-1);

}

}

q6)Write a program to print Fibonacci series using recursive function

q7)Write a program to find sum and average of n numbers using array and function.

ans:-

code:-

//Write a program to find sum and average of n numbers using array and function.

#include<stdio.h>                   

void sumandaverage(int ar[],int size);                         

int main()                              

{

int x[100],size,i;

printf("enter total size of array\n");

scanf("%d",&size);

printf("enter elements\n");

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

{

scanf("%d",&x[i]);

}

sumandaverage(x,size);

return 0;                                      

}

void sumandaverage(int ar[100],int size)             

{

 int sum=0;

 float average;

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

 {

  sum=sum+ar[i];

 }

 average=(float)sum/size;

 printf("sum=%d,average=%f",sum,average);

}


q8)Write a program to print greatest number among n numbers using array and function.

ans:

code:

//Write a program to print greatest number among n numbers using array and function.
#include<stdio.h>                   
int greatestnum(int ar[],int size);                         
int main()                              
{
int x[100],size,i;
printf("enter total size of array\n");
scanf("%d",&size);
printf("enter elements\n");
for(i=0;i<=size-1;i++)
{
scanf("%d",&x[i]);
}
printf("greatest number=%d",greatestnum(x,size));
return 0;                                      
}
int greatestnum(int ar[],int size)                               
{
 int gr=ar[0];
 for(int i=0;i<=size-1;i++)
 {
  if(gr<ar[i])
  {
      gr=ar[i];
  }
 }
 return gr;
}

q9)Write a program to print transpose of matrices of order 2x2 using array and function.

ans:-

code:-

//Write a program to print transpose of matrices of order 2x2 using array and function.
#include<stdio.h>                   
void transpose(int ar[2][2]);                         
int main()                              
{
int x[2][2],i,j;
printf("enter elements\n");
for(i=0;i<=1;i++)
{
    for(j=0;j<=1;j++)
    {
scanf("%d",&x[i][j]);
    }
}
transpose(x); 
return 0;                                      
}
void transpose(int ar[2][2])                         
                     
{
    int i,j;
for(i=0;i<=1;i++)
{
    for(j=0;j<=1;j++)
    {
printf("%d",ar[j][i]);
    }
    printf("\n");
}
 
}

q10)Write a program to print sum of two matrices of order 2x2 using array and function.

//Write a program to print sum of two matrices of order 2x2 using array and function.
#include<stdio.h>                   
void sum_matrix(int ma[2][2],int ma1[2][2]);                         
int main()                              
{
int m1[2][2],m2[2][2],i,j;
printf("enter elements of first matrix\n");
for(i=0;i<=1;i++)
{
    for(j=0;j<=1;j++)
    {
scanf("%d",&m1[i][j]);
    }
}
printf("enter elements of second matrix\n");
for(i=0;i<=1;i++)
{
    for(j=0;j<=1;j++)
    {
scanf("%d",&m2[i][j]);
    }
}
sum_matrix(m1,m2); 
return 0;                                      
}
void sum_matrix(int ma[2][2],int ma1[2][2])                        
{
    int i,j;
for(i=0;i<=1;i++)
{
    for(j=0;j<=1;j++)
    {
printf(" %d ",ma[i][j]+ma1[i][j]);
    }
    printf("\n");
}
 
}

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

case study:

Write an interactive program that takes a set of numbers and performs the following operation based on user's choice.Make separate function for the respective problem(array related multi functions program).

1. Print in ascending order

2.Average of the numbers

3.Search for a key number

4.find out maximum

5.Exit from the program

Ans:-

/*case study of user defined function*/

#include<stdio.h>

#include<stdlib.h>

void ascend_order(int numbers[],int size);

void average_of_numbers(int numbers[],int size);

void search_a_number(int numbers[],int size);

void max_a_number(int numbers[],int size);

int main()

{

int number[100];

int i,n,ch;

printf("enter the size of array\n");

scanf("%d",&n);

printf("enter numbers\n");

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

    {

    scanf("%d",&number[i]);

    }

    while(1)

    {

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

    printf("\n|1--->to arrange numbers in ascending order|\n");

    printf("\n|2--->to find Average of numbers           |\n");

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

    printf("\n|4--->to find Highest/maximum Marks        |\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 arrange records in ascending order\n");

ascend_order( number,n);

break;

    case 2: printf("average of numbers=\n");

average_of_numbers(number,n);

break;

case 3: printf("we are going to search a number\n");

search_a_number(number,n);

break;

case 4: printf("\nwe are going to find maximum number:\n");

max_a_number(number,n);

break;

default:

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

exit(0);

    }

}

}

void ascend_order(int numbers[],int size)

{

int i,j,t;

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

{

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

{

if(numbers[i]>numbers[j])

{

t=numbers[i];

numbers[i]=numbers[j];

numbers[j]=t;

}

}

}

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

{

         printf("%d\n",numbers[i]);

}

}

void average_of_numbers(int numbers[],int size)

{

int i,j,sum=0;

float average;

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

{

sum=sum+numbers[i];

}

average=(float)sum/size;

printf("average=%f\n",average);

}

void search_a_number(int numbers[],int size)

{

int i,j,number_to_search;

int flag=0;

printf("enter a number to search\n");

scanf("%d",&number_to_search);

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

{

        if(number_to_search==numbers[i])

            flag=1;

}

if(flag==0)

{

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

}

else

{

    printf("number is in the list\n");

}

}

void max_a_number(int numbers[],int size)

{

    int i,j,max;

    max=numbers[0];

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

{

    if(max<numbers[i])

    max=numbers[i];

}

printf("maximum number=%d",max);

}