This commit is contained in:
2024-10-21 16:49:33 -07:00
parent 42255f4a6d
commit 6984b18ce0

45
Q4.c Normal file
View File

@@ -0,0 +1,45 @@
#include <stdio.h>
#include <string.h>
typedef union Book {
char title[50];
char author[50];
int pages;
double price;
} Book;
void print_book(Book *book) {
printf("Title: %s\n", book->title);
printf("Author: %s\n", book->author);
printf("Number of Pages: %d\n", book->pages);
printf("Price: %0.02f\n\n", book->price);
}
void print_books_higher_than_threshold(Book arr[], int len, double threshold) {
for(int i = 0; i < len; i++) {
if(arr[i].price > threshold) {
print_book(&arr[i]);
}
}
}
int main() {
Book b1;
b1.price = 2.99;
Book b2;
b2.price = 19.99;
Book b3;
b3.price = 99.99;
Book b4;
b4.price= 9.99;
Book arr[] = {b1, b2, b3, b4};
/* 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
print_books_higher_than_threshold(arr, sizeof(arr)/sizeof(arr[0]), 10.0);
}