From 4894b6d8e2d88e3bae9da50ce712cc86403697f4 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 22 Oct 2024 21:55:45 -0700 Subject: [PATCH] Update Q4.c --- Q4.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Q4.c b/Q4.c index 2536f42..17edd16 100644 --- a/Q4.c +++ b/Q4.c @@ -1,6 +1,6 @@ #include -#include +// union for book typedef union Book { char title[50]; char author[50]; @@ -8,6 +8,8 @@ typedef union Book { double price; } Book; +//Print book details +// Note that this is impossible since Book is a union, it cannot have all these details initialized at once void print_book(Book *book) { printf("Title: %s\n", book->title); printf("Author: %s\n", book->author); @@ -15,6 +17,7 @@ void print_book(Book *book) { printf("Price: %0.02f\n\n", book->price); } +// Prints all books with price higher than threshold void print_books_higher_than_threshold(Book arr[], int len, double threshold) { for(int i = 0; i < len; i++) { if(arr[i].price > threshold) { @@ -24,6 +27,7 @@ void print_books_higher_than_threshold(Book arr[], int len, double threshold) { } int main() { + // Books to test function Book b1; b1.price = 2.99; Book b2; @@ -37,9 +41,8 @@ int main() { /* Ok so this kinda doesnt make sense. The threshold check only works if Book is set with a price. Kinda goes in * line with the fact that union doesnt make sense at all here - * Also, when printing any book, since only 1 value is assigned (as its a UNION) the other fields are gibberish - - + * Also, when printing any book, since only 1 value is assigned (as its a union) the other fields are gibberish + * */ print_books_higher_than_threshold(arr, sizeof(arr)/sizeof(arr[0]), 10.0); -} \ No newline at end of file +}