Structure



     1.  Write a program to define structure with tag state with fields state name, number of districts and total population. Read and display the data.

#include<stdio.h>
struct state
{
    char name[10];
    int n_dist;
    long population;
};
void main()
{
     struct state s1;
     printf("\n enter data of state: name, no of district and population");
     scanf("%s%d%d",s1.name,&s1.n_dist,&s1.population);

     printf("\n The Details are as under\n");
     printf("\n name is %s\n No of dist are %d \n population is      %d",s1.name,s1.n_dist,s1.population );
}


2. Write a program to create a list of books details. The details of a book include title, author, publisher, publishing year, number of pages, and price. [Do it yourself]
 3. Define a structure called Item with members: Item_code, Item_name, Price. Create an array of five Items. Create a function which accepts the Item array and modifies each element with an increase of 10% in the price.

#include<stdio.h>
#include<conio.h>
struct item
{
int code;
char name[10];
int price;
};
void inc(struct item it[],int x);
void main()
{
struct item it[5];
int i;
for(i=0;i<5;i++)
{
printf("\n enter code, name and price");
scanf("%d%s%d", &it[i].code,it[i].name,&it[i].price);
}
inc(it,5);
}
void inc(struct item it[],int x)
{
int i;
for(i=0;i<5;i++)
{
it[i].price += it[i].price*0.1;
printf("\n %d %s %d", it[i].code,it[i].name,it[i].price);
} }


4. Define a structure to represent a date. Use your structures that accept two different dates in the format mm dd of the same year. Write a C program to display the month names of both dates.

#include<stdio.h>
#include<conio.h>
struct date
{
int mm;
int dd;
int year;
};
void main()
{
struct date d1,d2;
label:
printf("\n Enter first mm,dd,year");
scanf("%d%d%d", &d1.mm,&d1.dd,&d1.year);
printf("\n Enter second mm,dd,year");
scanf("%d%d%d", &d2.mm,&d2.dd,&d2.year);
if(d1.year!=d2.year)
{
goto label;
}
else
{
printf("\n Valid Dates");
printf("\n %d",d1.mm);
printf("\n %d",d2.mm);
}
}


5. Define a structure that can describe a Hotel. It should have members that include name, address, grade, room charges, grade and no of rooms. Write a function to print out all hotel details with room charges less than a given value

#include<stdio.h>
struct hotel
{
char name[10];
char add[10]; char grade;
int roomc;
int nroom;
};
void printdata(struct hotel[ ],int);
void main()
{
struct hotel h1[3];
int i;
for(i=0;i<3;i++)
{
printf("\n Enter name,add, grade,room charges,number of rooms");
scanf("%s %s %c %d %d", h1[i].name,h1[i].add,&h1[i].grade, &h1[i].roomc,&h1[i].nroom);
}
printdata(h1,3);
}
void printdata(struct hotel h1[],int j)
{
int ch,i;
printf("\n Enter room charges");
scanf("%d", &ch);
for(i=0;i<j;i++)
{
if(h1[i].roomc <ch)
{
printf("\n name,add, grade,room charges,number of rooms");
printf("%s %s %c %d %d", h1[i].name,h1[i].add,h1[i].grade, h1[i].roomc,h1[i].nroom);
} }  }


6. Write a program to accept records of different states using array of structures. The structure should contain char state and number of int engineering colleges, int medical colleges, int management colleges and int universities. Calculate total colleges and display the state, which is having highest number of colleges.

#include<stdio.h>
#include<conio.h>
struct state
{
char sta[10];
int eng_clg;
int med_clg;
int mgmt_clg;
int uni;
int t_clg;
};
void main()
{
struct state s1[5];
int i,max=0;
printf("\n Enter Data");
for(i=0;i<5;i++)
{
scanf("%s%d%d%d%d",s1[i].sta,&s1[i].eng_clg,&s1[i].med_clg, &s1[i].mgmt_clg,&s1[i].uni);
s1[i].t_clg=s1[i].eng_clg+s1[i].med_clg+s1[i].mgmt_clg+s1[i].uni;
if(max<s1[i].t_clg)
max=s1[i].t_clg;
}
for(i=0;i<5;i++)
{
if(max==s1[i].t_clg)
{
printf("The state which is having highest clgs is %s",s1[i].sta);
}  }   }



7. Define a structure by name time with members seconds, minutes and hours of int type. A variable of the structure would thus represent time. If time1 and time2 are two variables of the structure type, write a program to find the difference of two times using a function.

#include<stdio.h>
struct st_time
{
int sec;
int min;
int hr;
};
void diff(struct st_time,struct st_time);
void main()
{
struct st_time t1,t2;
printf("\n Enter second,minute and Hour for the first time");
scanf("%d%d%d", &t1.sec, &t1.min,&t1.hr);
printf("\n Enter second,minute and Hour for the Second time");
scanf("%d%d%d", &t2.sec, &t2.min,&t2.hr);
diff(t1,t2);
}
void diff(struct st_time t1, struct st_time t2)
{
printf("\n Difference between two time is %d:%d:%d", t1.sec-t2.sec,t1.min-t2.min,t1.hr-t2.hr);
}




8. Write a program to accept records of different states using array of structures. The structure should contain char state, int population, int literacy rate and int per capita income. Assume suitable data. Display the state whose literacy rate is highest and whose per capita income is highest.

#include<stdio.h>
struct state
{

     char name[10];
     int ppltn;
     int litr;
     int incm;
  }s[5];
  void main()
  {
      int i, maxlitr=0,maxincm=0,indlit,indincm;
      for(i=0;i<5;i++)
      {
          printf("\n enter state name, population, literacy rate and income");
          scanf("%s%d%d%d",s[i].name,&s[i].ppltn,&s[i].litr,&s[i].incm);

      }
     for(i=0;i<5;i++)
     {
         if(s[i].litr>maxlitr)
            maxlitr=s[i].litr;
         if(s[i].incm>maxincm)
            maxincm=s[i].incm;
     }
     for(i=0;i<5;i++)
     {
         if(maxlitr==s[i].litr)
         {
             printf("\n State details with max literature rate");
             printf("\n %s\n%d\n%d\n%d\n\n",s[i].name,s[i].ppltn,s[i].litr,s[i].incm);
              break;
         }
     }

     for(i=0;i<5;i++)
     {
         if(maxincm==s[i].incm)
         {
             printf("\n State details with max income");
             printf("\n %s\n%d\n%d\n%d\n\n",s[i].name,s[i].ppltn,s[i].litr,s[i].incm);
             break;
         }     
     }
  }



9. Define a structure employee with members employee name, basic pay, dearness allowance, house rent, net salary. Declare an array of 5 employees. Write a function which calculates the net salary of employees and prints all employee details in descending order of their net salary.

#include<stdio.h>
struct emp
{
char name[10];
int bpay;
int da;
int hra;
int net_salary;
};
void cal(struct emp e[],int size);
void main()
{
struct emp e1[5],tmp;
int i,j;
for(i=for(i=0;i<5;i++)
{
printf("\n Enter name,bpay,da,hra of employee");
scanf("%s%d%d%d", e1[i].name,&e1[i].bpay,&e1[i].da,&e1[i].hra);
}
cal(e1,5);
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(e1[i].net_salary<e1[j].net_salary)
{
tmp = e1[i];
e1[i]=e1[j];
e1[j]=tmp;
}
}
}
printf("\n Employee Details in a Descending Order \n\n");
for(i=0;i<5;i++)
{
printf("\n %s %d %d %d %d", e1[i].name,e1[i].bpay,e1[i].da,e1[i].hra,e1[i].net_salary);
}
}
void cal(struct emp e1[],int size)
{
int i;
for(i=0;i<size;i++)
{
e1[i].net_salary = e1[i].bpay+e1[i].da+e1[i].hra;
}
}



10. Define a structure with tag population with fields Men and Women. Create structure with in structure using state and population structure. Read and display the data.

#include<stdio.h>
struct population
{
    int nw;
    int nm;
};

struct state
{

    char name[10];
    struct population p1;

};
void main()
{
     struct state s1;
     printf("\n enter data of state: name, no of women and men");
     scanf("%s%d%d",s1.name,&s1.p1.nw,&s1.p1.nm);

     printf("\n The Details are as under\n");
     printf("\n name is %s\n No of women are %d \n Men are %d",s1.name,s1.p1.nw,s1.p1.nm);
}