Question: The language is C Sorry this question has a lot of text but most of it is just defining things and stuff I made to
The language is C
Sorry this question has a lot of text but most of it is just defining things and stuff I made to help for testing the code
/*
Code starts here
*/
#include #include #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;
Furniture *update_material(Furniture catalog[], int catalog_size, char *name, char *new_material) { /** * Given the furniture catalog, the (unique) name of an item, and a new * material name, update the material of the furniture item in the catalog * with the given name. Returns a pointer to the updated item. * * If no item exists in the catalog with the given name, don't change * anything and return NULL. */
return NULL; // Update the correct item if needed and return the pointer }
Furniture *update_dimensions(Furniture catalog[], int catalog_size, char *name, Dimensions *new_dimensions) { /** * Given the furniture catalog, the (unique) name of an item, and a * *pointer* to a new dimensions struct, update the dimensions of the * furniture item in the catalog with the given name. Returns a pointer * to the updated item. * * If no item exists in the catalog with the given name, don't change * anything and return NULL. */
return NULL; // Update the correct item if needed and return the pointer }
char *biggest_ripoff_name(Furniture catalog[], int catalog_size) { /** * We all know that the bigger an item is (more volume), the better it is! * (Maybe not, but let's assume that is the case). The biggest ripoff would * be the item that costs the *most* amount of money per unit volume. Assume * that it is unique, and return it's name. */ return NULL; // Return the name of the correct item }
//I have included a main function to help with testing (You do not have to add anything else to it but if it helps you with testing you are welcome to add) :)
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;
/***************************************************************************/ /* Biggest Ripoff Name */ /***************************************************************************/
char *name = biggest_ripoff_name(catalog, CATALOG_SIZE); printf(" Name of the biggest ripoff item is: %s ", name);
/***************************************************************************/ /* Update Material */ /***************************************************************************/
// Let's create a pointer to a Furniture and the item we need to test 4 and 5 Furniture *ret; ret = update_material(catalog, CATALOG_SIZE, "Docksta table", "Plastic"); printf(" %s now has material %s ", ret->name, ret->material);
/***************************************************************************/ /* Update Dimensions */ /***************************************************************************/
Dimensions new_dims = {9.1, 8.1, 7.1, 6.1};
ret = update_dimensions(catalog, CATALOG_SIZE, "Balkarp sofa", &new_dims); printf(" %s now has dimensions: {%f, %f, %f, %f} ", ret->name, ret->dimensions.height, ret->dimensions.depth, ret->dimensions.width, ret->dimensions.weight);
return 0; }
You can NOT include any other c libraries and can NOT use any built in functions other than ones that can examine strings such as strcpy() and strcmp()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
