bsc

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

luc113.c (2262B)


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