bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / luc113.c

luc113.c (2071B)


      1 /* Store date in a structure using bit fields (day: 5 bits, month: 4 bits, year: 12 bits). Read joining dates of 10 employees and display them sorted by year.
      2 */
      3 /* Let Us C, Chap- 22 (Miscellaneous Features), Qn No.: C(a) */
      4 
      5 /* This file is auto-generated by a bot. */
      6 /* This code is not compiled; it is for reference only. */
      7 
      8 
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 
     13 // Define structure with bit-fields
     14 struct date
     15 {
     16     unsigned int day : 5;   // 1-31 takes 5 bits
     17     unsigned int month : 4; // 1-12 takes 4 bits
     18     unsigned int year : 12; // Sufficient for year 0-4095
     19 };
     20 
     21 struct employee
     22 {
     23     char name[30];
     24     struct date doj; // Date of Joining
     25 };
     26 
     27 int compare_dates(const void *a, const void *b);
     28 
     29 int main()
     30 {
     31     struct employee emp[10];
     32     int i;
     33     // Temporary variables for input because we cannot take address of a bit-field
     34     int d, m, y; 
     35 
     36     printf("Enter details for 10 employees:\n");
     37     for (i = 0; i < 10; i++)
     38     {
     39         printf("\nEmployee %d Name: ", i + 1);
     40         scanf("%s", emp[i].name);
     41 
     42         printf("Date of Joining (dd mm yyyy): ");
     43         scanf("%d %d %d", &d, &m, &y);
     44 
     45         // Assign to bit-fields
     46         emp[i].doj.day = d;
     47         emp[i].doj.month = m;
     48         emp[i].doj.year = y;
     49     }
     50 
     51     // Sort based on year using qsort
     52     qsort(emp, 10, sizeof(struct employee), compare_dates);
     53 
     54     printf("\n--- Employees Sorted by Joining Year ---\n");
     55     for (i = 0; i < 10; i++)
     56     {
     57         printf("%-15s | DOJ: %02d-%02d-%d\n", 
     58                emp[i].name, emp[i].doj.day, emp[i].doj.month, emp[i].doj.year);
     59     }
     60 
     61     return 0;
     62 }
     63 
     64 int compare_dates(const void *a, const void *b)
     65 {
     66     struct employee *e1 = (struct employee *)a;
     67     struct employee *e2 = (struct employee *)b;
     68 
     69     // Primary sort by Year
     70     if (e1->doj.year != e2->doj.year)
     71         return e1->doj.year - e2->doj.year;
     72     
     73     // Secondary sort by Month
     74     if (e1->doj.month != e2->doj.month)
     75         return e1->doj.month - e2->doj.month;
     76         
     77     // Tertiary sort by Day
     78     return e1->doj.day - e2->doj.day;
     79 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror