Question
The language is C /* Code starts here */ #include #include #include //You may NIOT include any other libraries #define MAX_STRING_LEN 128 typedef struct dimensions
The language is C
/*
Code starts here
*/
#include
//You may NIOT include any other libraries
#define MAX_STRING_LEN 128
typedef struct dimensions { // This struct stores the dimensions of an object. // The fields should be self explanatory. The units // don't matter here, but say they are meters and kgs. float height; float depth; float width; float weight; } Dimensions;
typedef struct furniture { // This struct stored information about a piece of // furniture. Note the nested struct containing the // information on the furniture dimensions. char name[MAX_STRING_LEN]; char material[MAX_STRING_LEN]; float price; Dimensions dimensions; } Furniture;
int find_material(Furniture catalog[], int catalog_size, char *material, Furniture *results[]) { /** * Given an array (with the size) of furniture items (a catalog), and a * string with the name of a material, find all the furniture items in the * catalog with the given material. For each matching item, add a pointer to * it to the results array (an array of pointers). The order of insertion * to `results` must be the same as the order in which the items appear in * the catalog. Assume `results` has enough space. * * Additionally, return the number of matches found. */
return -1; // Updated `results` directly and return number of matches }
int find_price(Furniture catalog[], int catalog_size, float min_price, float max_price, Furniture *results[]) { /** * Similar to the previous function, this time find all the items in the * catalog that are within the given price range (inclusive). The order of * insertion to `results` must be the same as the order in which the items * appear in the catalog. Assume `results` has enough space. * * Additionally, return the number of matches found. */
return -1; // Updated `results` directly and return number of matches }
//Here is a main function to help you with testing (You may add more cases if you want to test it more but not required :)
int main() {
// Defining and array of furniture items. Note that when initializing a // a struct (or an array), we can use the { ... } shorthand for them! Furniture catalog[CATALOG_SIZE] = { // |-------- Dimensions ------------| // name material price height depth width weight {"Docksta table", "Wood", 49.99, {2, 1, 1, 10}}, {"Ektorp sofa", "Leather", 99.99, {2, 5, 1, 100}}, {"Poang armchair", "Plastic", 24.95, {1, 0.5, 0.5, 4}}, {"Kallax shelves", "Wood", 149.99, {2, 1, 0.3, 70}}, {"Flaggskepp lamp", "Metal", 17.99, {0.1, 0.1, 0.1, 1}}, {"Balkarp sofa", "Leather", 120.00, {0.5, 2, 0.5, 50}}, {"Jonaxel basket", "Metal", 69.99, {2, 1, 1, 10}}, {"Nordli bed", "Wood", 339.00, {0.2, 2, 1, 20}}, {"Renberget chair", "Plastic", 59.99, {1, 0.5, 0.5, 9}}, {"Kolbjorn cabinet", "Metal", 99.00, {0.8, 0.4, 0.8, 17}}, };
// Let's create an array to store the results and the number of results. // At most, the number of matches is the same as the size of the array. Furniture *results[CATALOG_SIZE]; int results_n;
/***************************************************************************/ /* Find material */ /***************************************************************************/
results_n = find_material(catalog, CATALOG_SIZE, "Wood", results);
// To make sure our search was done correctly, we will print the results printf("Found %d items made of Wood: ", results_n); for (int i = 0; i < results_n; i++) { printf("Result %d: %s ", i + 1, results[i]->name); }
/***************************************************************************/ /* Find Price */ /***************************************************************************/
results_n = find_price(catalog, CATALOG_SIZE, 0.0, 50.0, results);
// To make sure our search was done correctly, we will print the results printf(" Found %d items between $0 and $50 (inclusive): ", results_n); for (int i = 0; i < results_n; i++) { printf("Result %d: %s, $%f ", i + 1, results[i]->name, results[i]->price); }
}
You are NOT able to include any more libraries
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started