0% found this document useful (0 votes)
8 views9 pages

Self-Study Structure Union Bitfields Vasantha Balan K

The document provides two programming exercises focused on structures in C. The first exercise involves creating a program to compare two dates using a structure, while the second exercise is a menu-driven library management system that allows users to add, display, and manage book information. Both exercises include sample inputs, outputs, and complete code solutions.

Uploaded by

k.s.yogeswar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Self-Study Structure Union Bitfields Vasantha Balan K

The document provides two programming exercises focused on structures in C. The first exercise involves creating a program to compare two dates using a structure, while the second exercise is a menu-driven library management system that allows users to add, display, and manage book information. Both exercises include sample inputs, outputs, and complete code solutions.

Uploaded by

k.s.yogeswar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SELF PRACTISE

Topic : Structure, Union and Bitfields

Learning Level : Basic and Easy

Q.
Question
No
1 Write a program that compares two given dates. To store date use
structure, say date that contains three members namely date, month and
year. If the dates are equal then display message as "Equal" otherwise
"Unequal".

Sample Input Enter first date:


Enter day: 15
Enter month: 8
Enter year: 2023

Enter second date:


Enter day: 15
Enter month: 8
Enter year: 2023

Sample Output
Equal

Sample Input
Enter first date:
Enter day: 25
Enter month: 12
Enter year: 2021

Enter second date:


Enter day: 31
Enter month: 12
Enter year: 2021

Sample Output
Unequal

Solution
#include <stdio.h>

struct Date {
unsigned int day : 5; // 5 bits to store day (0-31)
unsigned int month : 4; // 4 bits to store month (0-12)
unsigned int year : 12; // 12 bits to store year (0-4095)
};

Success is nothing more than a few simple disciplines practiced everyday


1
SELF PRACTISE

void inputDate(struct Date *date) {


printf("Enter day: ");
scanf("%u", &date->day);

printf("Enter month: ");


scanf("%u", &date->month);

printf("Enter year: ");


scanf("%u", &date->year);
}

int compareDates(struct Date date1, struct Date date2) {


if (date1.day == date2.day && date1.month == date2.month &&
date1.year == date2.year)
return 1; // Dates are equal
return 0; // Dates are unequal
}

int main() {
struct Date date1, date2;

printf("Enter first date:\n");


inputDate(&date1);

printf("Enter second date:\n");


inputDate(&date2);

if (compareDates(date1, date2)) {
printf("Equal\n");
} else {
printf("Unequal\n");
}

return 0;
}

2 Write a menu driven program that depicts the working of a library. The
menu options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Create a structure called library to hold accession number, title of the
book, author name, price of the book, and flag indicating whether book is
issued or not.

Sample Input and Output

Success is nothing more than a few simple disciplines practiced everyday


2
SELF PRACTISE

Library Management System


1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit

Enter your choice: 1

Enter accession number: 101


Enter title: C Programming Language
Enter author: Dennis Ritchie
Enter price: 29.99
Is the book issued? (1 for Yes, 0 for No): 0
Book added successfully.

Enter your choice: 1

Enter accession number: 102


Enter title: Algorithms Unlocked
Enter author: Thomas Cormen
Enter price: 39.99
Is the book issued? (1 for Yes, 0 for No): 1
Book added successfully.

Enter your choice: 1

Enter accession number: 103


Enter title: Clean Code
Enter author: Robert Martin
Enter price: 45.50
Is the book issued? (1 for Yes, 0 for No): 0
Book added successfully.

Enter your choice: 2

Library Books Information:

Book 1:
Accession Number: 101
Title: C Programming Language
Author: Dennis Ritchie
Price: $29.99
Issued: No

Book 2:
Accession Number: 102
Title: Algorithms Unlocked
Author: Thomas Cormen

Success is nothing more than a few simple disciplines practiced everyday


3
SELF PRACTISE

Price: $39.99
Issued: Yes

Book 3:
Accession Number: 103
Title: Clean Code
Author: Robert Martin
Price: $45.50
Issued: No

Enter your choice: 3


Enter author name: Robert Martin

Books by author 'Robert Martin':


Title: Clean Code

Enter your choice: 4


Enter accession number: 101

Title: C Programming Language

Enter your choice: 5


Total number of books in the library: 3

Enter your choice: 6

Books in the order of accession number:


Accession Number: 101, Title: C Programming Language
Accession Number: 102, Title: Algorithms Unlocked
Accession Number: 103, Title: Clean Code

Enter your choice: 7


Exiting the program.

Solution
#include <stdio.h>
#include <string.h>

#define MAX_BOOKS 100

struct Library {
int accessionNumber;
char title[100];
char author[100];
float price;
int isIssued; // 1 if issued, 0 if not issued

Success is nothing more than a few simple disciplines practiced everyday


4
SELF PRACTISE

};

int bookCount = 0;
struct Library books[MAX_BOOKS];

void addBook() {
if (bookCount >= MAX_BOOKS) {
printf("Library is full. Cannot add more books.\n");
return;
}

struct Library newBook;


printf("Enter accession number: ");
scanf("%d", &newBook.accessionNumber);
getchar(); // clear newline character

printf("Enter title: ");


fgets(newBook.title, 100, stdin);
newBook.title[strcspn(newBook.title, "\n")] = 0; // remove newline

printf("Enter author: ");


fgets(newBook.author, 100, stdin);
newBook.author[strcspn(newBook.author, "\n")] = 0; // remove newline

printf("Enter price: ");


scanf("%f", &newBook.price);

printf("Is the book issued? (1 for Yes, 0 for No): ");


scanf("%d", &newBook.isIssued);

books[bookCount++] = newBook;
printf("Book added successfully.\n");
}

void displayBooks() {
if (bookCount == 0) {
printf("No books in the library.\n");
return;

Success is nothing more than a few simple disciplines practiced everyday


5
SELF PRACTISE

printf("\nLibrary Books Information:\n");


for (int i = 0; i < bookCount; i++) {
printf("\nBook %d:\n", i + 1);
printf("Accession Number: %d\n", books[i].accessionNumber);
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Price: $%.2f\n", books[i].price);
printf("Issued: %s\n", books[i].isIssued ? "Yes" : "No");
}
}

void listBooksByAuthor() {
char author[100];
getchar(); // clear newline character
printf("Enter author name: ");
fgets(author, 100, stdin);
author[strcspn(author, "\n")] = 0; // remove newline

int found = 0;
printf("\nBooks by author '%s':\n", author);
for (int i = 0; i < bookCount; i++) {
if (strcmp(books[i].author, author) == 0) {
printf("Title: %s\n", books[i].title);
found = 1;
}
}
if (!found) {
printf("No books found by author '%s'.\n", author);
}
}

void listBookTitle() {
int accessionNumber;
printf("Enter accession number: ");
scanf("%d", &accessionNumber);

Success is nothing more than a few simple disciplines practiced everyday


6
SELF PRACTISE

for (int i = 0; i < bookCount; i++) {


if (books[i].accessionNumber == accessionNumber) {
printf("Title: %s\n", books[i].title);
return;
}
}
printf("No book found with accession number %d.\n", accessionNumber);
}

void countBooks() {
printf("Total number of books in the library: %d\n", bookCount);
}

void listBooksByAccessionNumber() {
struct Library temp;

// Simple bubble sort based on accession number


for (int i = 0; i < bookCount - 1; i++) {
for (int j = i + 1; j < bookCount; j++) {
if (books[i].accessionNumber > books[j].accessionNumber) {
temp = books[i];
books[i] = books[j];
books[j] = temp;
}
}
}

printf("\nBooks in the order of accession number:\n");


for (int i = 0; i < bookCount; i++) {
printf("Accession Number: %d, Title: %s\n", books[i].accessionNumber,
books[i].title);
}
}

int main() {
int choice;
do {
printf("\nLibrary Management System\n");

Success is nothing more than a few simple disciplines practiced everyday


7
SELF PRACTISE

printf("1. Add book information\n");


printf("2. Display book information\n");
printf("3. List all books of given author\n");
printf("4. List the title of specified book\n");
printf("5. List the count of books in the library\n");
printf("6. List the books in the order of accession number\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
addBook();
break;
case 2:
displayBooks();
break;
case 3:
listBooksByAuthor();
break;
case 4:
listBookTitle();
break;
case 5:
countBooks();
break;
case 6:
listBooksByAccessionNumber();
break;
case 7:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 7);

return 0;

Success is nothing more than a few simple disciplines practiced everyday


8
SELF PRACTISE

2.

3.

4.

5.

Success is nothing more than a few simple disciplines practiced everyday


9

You might also like