49 lines
1.2 KiB
C
49 lines
1.2 KiB
C
#include <stdio.h>
|
|
|
|
// union for book
|
|
typedef union Book {
|
|
char title[50];
|
|
char author[50];
|
|
int pages;
|
|
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);
|
|
printf("Number of Pages: %d\n", book->pages);
|
|
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) {
|
|
print_book(&arr[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
// Books to test function
|
|
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);
|
|
|
|
}
|