Friday, May 13, 2022

Assignment 5: Structures and union in C

 Assignment 5: Structures and union in C

Structure: A structure is a composition of variables possibly of different data types, grouped 

together under a single name. Each variable within the structure is called a ‘member’.

Nested structures: The individual members of a structure can be other structures as well. 

This is called nesting of structures.

Union :A union is a variable that contains multiple members of possibly different data types 

grouped together under a single name. However, only one of the members can be used at a 

time. They occupy the same memory area.

Declaring a structure :

 struct structure-name

{

type member-1 ;

type member-2;

…..

type member-n ;

};

Example: 

struct student

{

char name[20]; 

int rollno;

int marks;

};


Creating structure variables:-

struct structurename variable; 

Example:-

struct student stud1; 


Accessing structure members :-

variable.member 

Example:-

stud1.name 

stud1.rollno 

stud1.marks


Initializing a structure variable

the initialization values have to be 

given in {} and in order

Example:-

struct student stud1 =

{“ABCD”,10,95};


Pointer to a structure :-
struct structure-name *pointer_name; 
Example:-
struct student *ptr; ptr = &stud1;


Accessing members using Pointer
pointer-name-> member-name; 
Example:-
ptr->name;
ptr->rollno;

Array of structures 

struct structure-name array_name[size];
Example:-

struct student stud[10];


passing Structures to Functions
return-type function-name (struct structure-name variable);
Example:-
void display(struct student s);

pass an array of structures to a function
return-type function-name ( struct structure-name array[size]);
Example:-
void display(struct student stud[10]);

Creating a nested structure Method 1
struct structure1
{
. . .
struct structure2
{
. . .
} variable;
. . .
};

Example:-
struct student
{
int rollno; 
char name[20]; 
struct date
{
int dd, mm, yy;
} admdate;
};


Creating a nested structure Method 2
struct structure2
{
. . .
};
struct structure1
{
. . .
struct structure2 
variable;
. . .
};

Example:-
struct date
{
int dd, mm, yy;
};
struct student
{
int rollno; 
char name[20]; 
struct date admdate;
};


Accessing nested structure members

nested structure members can be accessed using the (.)operator repeatedly.
Example:-
stud1.bdate.dd, stud1.bdate.mm

self-referential structure

A structure containing a pointer to the same structure
Example:-
struct node
{
int info;
struct node *next;
};

Unions

 union union-name
{
type member-1 ;
type member-2;
 …..
type member-n ;
};

Example:-
union u
{
char a; int b;
};



1. Create a structure employee (id, name, salary). Accept details of n employees and 
display display it in summary format.
#include <stdio.h>
#include <stdlib.h>
 
typedef struct{
    char name[30];
    int id;
    double salary;
} Employee;
 
int main()
{
    //number of employees
    int n=2;

    //array to store structure values of all employees
    Employee employees[n];
 
    //Taking each employee detail as input
    printf("Enter %d Employee Details \n \n",n);
    for(int i=0; i<n; i++){
        printf("Employee %d:- \n",i+1);

        //Name
        printf("Name: ");
        scanf("%[^\n]s",employees[i].name);

        //ID
        printf("Id: ");
        scanf("%d",&employees[i].id);

        //Salary
        printf("Salary: ");
        scanf("%lf",&employees[i].salary);

        //to consume extra '\n' input
        char ch = getchar();
 
        printf("\n");
    }
 
    //Displaying Employee details
    printf("-------------- All Employees Details ---------------\n");
    for(int i=0; i<n; i++){
        printf("Name \t: ");
        printf("%s \n",employees[i].name);
 
        printf("Id \t: ");
        printf("%d \n",employees[i].id);
 
        printf("Salary \t: ");
        printf("%.2lf \n",employees[i].salary);
 
        printf("\n");
    }
 
    return 0;
}


1. Create a structure student (roll number, name, marks of 3 subjects, percentage). Accept details of n students and write a menu driven program to perform the following operations. Write separate functions for the different options.

 

1. Search

2. Modify

3. Display all student details

4. Display all student having percentage >

5. Display student having maximum percentage

 

#include<stdio.h>

#include<string.h>

struct student

{

int roll_no,math,fileo,proc;

char name[20];

float percent;

};

void Search(struct student S[],int size);

void Modify(struct student S[],int size,int roll_no);

void DisplayAll(struct student S[],int size);

void PercentGT(struct student S[],float checkPercent,int size);

void MaxPercent(struct student S[],int size);

void main()

{

struct student S[200];

int i,roll_no,size,ope;

float checkPercent;

printf("\n How many students are there:- ");

scanf(" %d",&size);

printf("\n Enter the information of students as follow:- \n \n");

printf("\n Roll No\t Name \t Mathematics \t File Organisation \t Programming In C \n \n");

 

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

{

scanf(" %d %s %d %d %d",&S[i].roll_no,S[i].name,&S[i].math,&S[i].fileo,&S[i].proc);

S[i].percent=(S[i].math+S[i].fileo+S[i].proc)/3;

}

do{

printf("\n \t ***** Menu *****");

printf("\n 1.Search");

printf("\n 2.Modify");

printf("\n 3.Display all student details");

printf("\n 4.Display all students having percentage greater than ___");

printf("\n 5.Display student having maximum percentage");

printf("\n 6.Exit \n \n");

printf("\n Enter Your Choice:- ");

scanf(" %d",&ope);

switch(ope)

{

case 1:

Search(S,size);

break;

case 2:

printf("\n Enter roll no to modify the details:- ");

scanf(" %d",&roll_no);

Modify(S,size,roll_no);

break;

case 3:

DisplayAll(S,size);

break;

case 4:

printf("\n Enter percentage:- ");

scanf("%f",&checkPercent);

PercentGT(S,checkPercent,size);

break;

case 5:

MaxPercent(S,size);

break;

}}while(ope!=6);

}

void Search(struct student S[],int size)

{

char name[20];

int i,flg=0,roll_no,searchBy;

do{

printf("\n How do you want to search:- 1.By Roll No \n \t\t\t     2.By Name \n");

scanf(" %d",&searchBy);

   

if(searchBy==1)

{

printf("\n Enter roll no of the student:- ");

scanf(" %d",&roll_no);

}

else

{

printf("\n Enter the name of the student:- ");

scanf(" %s",name);

}}while(searchBy<1 || searchBy>2);

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

{

if(S[i].roll_no==roll_no || strcmp(S[i].name,name)==0)

{

flg=1;

break;

}

}

 

if(flg==1)

{

printf("\n We have found this result:- \n ");

printf("\n Roll No\t Name \t Mathematics \t File Organisation \t Programming In C  \t Percentage \n \n");

printf("\n %d \t\t %s \t %6d \t\t %1d \t\t %8d \t\t %f \n",S[i].roll_no,S[i].name,S[i].math,S[i].fileo,S[i].proc,S[i].percent);

}

else

printf("\n We haven't found any record \n \n");

}

// Modify Function

void Modify(struct student S[],int size,int roll_no)

{

char name[20];

int i,update,newData;

do{

printf("\n What do you want to update:- 1.Roll NO \n \t\t\t      2.Name \n \t\t\t      3.Marks of 'Mathematics' \n \t\t\t      4.Marks of 'File Organisation' \n \t\t\t      5.Marks of 'Programming in C' \n");

scanf(" %d",&update);

if(update==3 || update==4 || update==5)

{

printf("\n Enter new marks:- ");

scanf("%d",&newData);

}

else if(update==1)

{

printf("\n Enter new roll no:- ");

scanf(" %d",&newData);

}

else if(update==2)

{

printf("\n Enter new name:- ");

scanf(" %s",name);

}

else

{

printf("\n Please make a valid choice \n \n");

}

 

}while(update<1 || update>5);

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

{

if(S[i].roll_no==roll_no)

{

if(update==1)

{

S[i].roll_no=newData;

break;

}   

else if(update==3)

{

S[i].math=newData;

break;

}

else if(update==4)

{

S[i].fileo=newData;

break;

}

else if(update==5)

{

S[i].proc=newData;

break;

}

else

{

strcpy(S[i].name,name);

break;

}

}

}

 

printf("\n Modified details of students are as follows:- \n");

printf("\n Roll No\t Name \t Mathematics \t File Organisation \t Programming In C  \t Percentage \n \n");

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

{

printf("\n %d \t\t %s \t %6d \t\t %1d \t\t %8d \t\t %f \n",S[i].roll_no,S[i].name,S[i].math,S[i].fileo,S[i].proc,S[i].percent);

}

}

// DisplayAll Function

void DisplayAll(struct student S[200],int size)

{

int i;

printf("\n The Information of students is as follow:- \n \n");

printf("\n Roll No\t Name \t Mathematics \t File Organisation \t Programming In C  \t Percentage \n \n");

 

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

printf("\n %d \t\t %s \t %6d \t\t %1d \t\t %8d \t\t %f \n",S[i].roll_no,S[i].name,S[i].math,S[i].fileo,S[i].proc,S[i].percent);

}

// PercentGT Function

void PercentGT(struct student S[],float checkPercent,int size)

{

int i;

printf("\n The Information of students who scored percentage>%f :- \n \n",checkPercent);

printf("\n Roll No\t Name \t Mathematics \t File Organisation \t Programming In C  \t Percentage \n \n");

 

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

{

if(S[i].percent>checkPercent)

{

printf("\n %d \t\t %s \t %6d \t\t %1d \t\t %8d \t\t %f \n",S[i].roll_no,S[i].name,S[i].math,S[i].fileo,S[i].proc,S[i].percent);

}

}

}

// MaxPercent Function

void MaxPercent(struct student S[200],int size)

{

int i;

float maxPercent=S[0].percent;

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

if(S[i].percent>maxPercent)

{  

maxPercent=S[i].percent;

break;

}

printf("\n \"%s\" scored maximum percentage. Below are his details:- - \n ",S[i].name);

printf("\n Roll No\t Name \t Mathematics \t File Organisation \t Programming In C  \t Percentage \n \n");

printf("\n %d \t\t %s \t %6d \t\t %1d \t\t %8d \t\t %f \n",S[i].roll_no,S[i].name,S[i].math,S[i].fileo,S[i].proc,S[i].percent);

}

 

 

2. Create a structure employee (id, name, salary). Accept details of n employees and write a menu driven program to perform the following operations. Write separate functions for the different options.

1. Search by name

2. Search by id

3. Display all

4. Display all employees having salary >

5. Display employee having maximum salary

 

#include<stdio.h>

#include<conio.h>

struct emp

{

   int id,salary;

   char name[20];

}e[100];

 

//To accept employee details

void accept(int n)

{

   printf("\nEnter employee id: ");

   scanf("%d",&e[n].id);

   printf("\nEnter employee name: ");

   scanf("%s",e[n].name);

   printf("\nEnter employee salary: ");

   scanf("%d",&e[n].salary);

}

 

//To display employee details

void display(int n)

{

   printf("\nemployee id:\t\t%d",e[n].id);

   printf("\nName:\t\t\t%s",e[n].name);

   printf("\nsalary:\t\t\t%d\n",e[n].salary);

}

 

void main()

{

   int c,n,i,Id, int sal_limit,max_sal; //c=choice , n=number of employees , Id=employee id

char ename[20];

   do

   {

      printf("\n1.Accept Details\n2.Display Details\n 3. Search Employee by Name \n  4.Search Employee by Id \n 

5. Display all \n 6. Display all employees having salary >   \n 7. Display employee having maximum salary \n 8 .Exit \nEnter your   choice:");

      scanf("%d",&c);

      switch(c)

      {

       case 1:printf("Enter the number of employees:");

                   scanf("%d",&n);

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

                   {

          accept(i);

                   }

                    break;

         case 2:printf("\n===============Details of employees=====================\n");

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

                   {

            display(i);

                   }

                    break;

         case 3: printf("Enter the employee Name : ");

               scanf("%s",ename);

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

                    {

                   if(strcmp(ename ,e[n].name )

                        {

                           display(i);

                             break;

                        }

                  }

 

         case 4: printf("Enter the employee Id: ");

               scanf("%d",&Id);

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

                    {

                   if(Id==e[i].id)

                        {

                           display(i);

                             break;

                        }

                  }

 

case 5:  printf("All Employee Details : ");

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

                   {

            display(i);

                   }

                    break;

case 6: printf("Enter the salary  : ");

               scanf("%d”, &sal_limit);

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

{

if(e[i].salary>sal_)

{

 printf("\n Name of the Employee : %s ",e[i].name);

 printf("\n id of the Employee : %d ",e[i].eid);

 printf("\n Salary of the Employee : %d ",e[i].salary);

}

}

break;

 

case 7: printf(“ Display employee having maximum salary”);

max_sal=e[0].salary;

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

{

if(e[i].salary>max_sal)

{

max_sal=e[i].salary;

}

}

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

{

if(e[i].salary==max_sal)

{

 

 printf("\n Name of the Employee : %s ",e[i].name);

 printf("\n id of the Employee : %d ",e[i].eid);

 printf("\n Salary of the Employee : %d ",e[i].salary);

}

}

Break;

      }

   }while(c<7);

}

 


Accept book details of ‘n’ books viz, book title, author, publisher and cost. Assign an accessionnumbers

to each book in increasing order. (Use dynamic memory allocation). Write a menudriven program

for the following options.

1. Books of a specific author

2. Books by a specific publisher

3. All books having cost >=

4. Information about a particular book (accept the title)

5. All books.

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct book
{
     int bno,bcost,baccno;
     char bname[20],bpub[20],bauthor[20];
}p[10];
int main()
{
     int n,i,ch,cost;
     char pubname[20],authorname[20],booknm[20];
     printf("/*How Many Records of Books You Want to Add*/\n\nEnter Limit : ");
     scanf("%d",&n);
     printf("------------------------------------------\n");
     for(i=0;i<n;i++)
     {
          printf("\tEnter Details of Book-%d",i+1);
          printf("\n------------------------------------------\n");
          printf("Book Number      : ");
          scanf("%d",&p[i].bno);
          printf("Book Name        : ");
          scanf("%s",p[i].bname);
          printf("Author Name     : ");
          scanf("%s",p[i].bauthor);
          printf("Publication : ");
          scanf("%s",p[i].bpub);
          printf("Cost             : ");
          scanf("%d",&p[i].bcost);
          printf("Accession Number : ");
          scanf("%d",&p[i].baccno);
          printf("------------------------------------------\n");
     }
     while(1)
     {
          printf("\n\t\tMENU\n");
          printf("------------------------------------------\n");
          printf("\n1.Books of Specific Author");
          printf("\n2.Books of Specific Publisher");
          printf("\n3.All books having cost >= “);

printf(“\n 4. Information about a particular book (accept the title) “);

Printf(“\n 5. All books.”);
          printf("\n4.All Books");
          printf("\n5.Exit");
          printf("\n------------------------------------------\n");
          printf("\nEnter Your Choice : ");
          scanf("%d",&ch);
          printf("\n");
          switch(ch)
          {
               case 1:
                    printf("Enter Author Name : ");
                    scanf("%s",authorname);
                    for(i=0;i<n;i++)
                    {
                         if(strcmp(p[i].bauthor,authorname)==0)
                         printf("\nBook Number      : %d\nBook Name        : %s\nAccession Number : %d\n",p[i].bno,p[i].bname,p[i].baccno);
                    }
                    break;
               case 2:
                    printf("Enter Publication Name : ");
                    scanf("%s",pubname);
                    for(i=0;i<n;i++)
                    {
                         if(strcmp(p[i].bpub,pubname)==0)
                              printf("\nBook Number      : %d\nBook Name        : %s\nAccession Number : %d\n\n",p[i].bno,p[i].bname,p[i].baccno);
                    }
                    break;
               case 3:

printf(“\n enter the cost for book:”);

scanf(“%d”,&cost);
                    for(i=0;i<n;i++)
                    {
                         if(p[i].bcost>=cost)
                         {
                              printf("Book Number : %d\n",p[i].bno);
                              printf("Book Name : %s \n",p[i].bname);
                              printf("Cost : %d\n",p[i].bcost);
                              printf("Accession Number : %d\n",p[i].baccno);
                              printf("\n------------------------------------------\n");
                         }
                    }
                    break;
              

case 4:

  printf("Enter Book Name : ");
                    scanf("%s",booknm);
                    for(i=0;i<n;i++)
                    {
                         if(strcmp(p[i].bname ,booknm)==0)
                         printf("\nBook Number : %d\nBook Name  : %s\nAccession Number : %d\n",p[i].bno,p[i].bname,p[i].baccno);
                    }
                    break;

 case 5:
                    for(i=0;i<n;i++)
                    {
                         printf("Book Number   : %d\n",p[i].bno);
                         printf("Book Name : %s \n",p[i].bname);
                         printf("Author : %s\n",p[i].bauthor);
                         printf("Publisher : %s\n",p[i].bpub);
                         printf("Cost : %d\n",p[i].bcost);
                         printf("Accession Number : %d\n",p[i].baccno);
                         printf("\n------------------------------------------\n");
                    }
                    break;
               case 5:
                    exit(0);
          }
     }
     return 0;
}

 

No comments:

Post a Comment