Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 4 - Structs and Pointers Quarantine has got us doing different activities at home, including house renovations. In Brian's Depot, we have a list

Exercise 4 - Structs and Pointers

Quarantine has got us doing different activities at home, including house renovations. In Brian's Depot, we have a list of all the furniture that is available. Each items record contains information such as the name of the furniture, the material, the dimensions, and most importantly the price.

When a customer tries to find a specific type of item, it could be useful to find all possible relevant items. Moreover, sometimes we need to update some records and we should be able to retrieve them and change the necessary fields.

This week we learnt about Compound Data Types and how it helps ups to collect a group of variables that correspond to a specific element. We are going to apply these techniques to create a new struct, in that way, we can group all the specifications of the furniture. You will be working with 2 different structs:

  • dimensions - a struct representing the dimensions of an object
  • furniture - a struct representing an item of furniture

In this exercise, you will write functions to:

  • search for furniture items matching a given material from a catalogue (list of furniture items)
  • search for furniture items within a given price range from a catalogue
  • find the name of the piece of furniture which is the "biggest ripoff"
  • find and update a furniture item with a new material
  • find and update a furniture item with new dimensions

image text in transcribed

image text in transcribed

/**

* Exercise 4 - Structs and Pointers

*

* Please read the comments below carefully, they describe your task in detail.

* Any clarifications and corrections will be posted to Piazza so please keep

* an eye on the forum!

*

* Starter code: Angela Zavaleta & Mustafa Quraish, 2020

*/

#include

#include

#include

#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

}

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

}

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

}

///////////////////////////////////////////////////////////////////////////////

#ifndef __testing__ // You know the drill; don't remove this.

// However, Feel free to change the line below to add more tests.

#define CATALOG_SIZE 10

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

{

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

{

printf("Result %d: %s, $%f ", i + 1, results[i]->name, results[i]->price);

}

/***************************************************************************/

/* 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;

}

#endif

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Exercise 4 - Structs and Pointers Quarantine has got us doing different activities at home, including house renovations. In Brian's Depot, we have a list of all the furniture that is available. Each item's record contains information such as the name of the furniture, the material, the dimensions, and most importantly the price. When a customer tries to find a specific type of item, it could be useful to find all possible relevant items. Moreover, sometimes we need to update some records and we should be able to retrieve them and change the necessary fields. This week we learnt about Compound Data Types and how it helps ups to collect a group of variables that correspond to a specific element. We are going to apply these techniques to create new struct, in that way, we can group all the specifications of the furniture. You will be working with 2 different structs: dimensions - a struct representing the dimensions of an object furniture - a struct representing an item of furniture In this exercise, you will write functions to: search for furniture items matching a given material from a catalogue (list of furniture items) search for furniture items within a given price range from a catalogue find the name of the piece of furniture which is the "biggest ripoff" find and update a furniture item with a new material find and update a furniture item with new dimensions Download Starter Code: ex4.ca The starter code contains documentation and some example output for the functions, as well as a few tests to help you check your code. The tests in the main function are not comprehensive. You should test your code on many different input parameters yourself to ensure that they actually work as intended. What to do: Implement the functions as needed. Add helper functions if you want to make your code clearer. Check to see if your output makes sense. The data you are working with is small enough that you should be able to figure out the expected value of the test cases yourself. What NOT to do: Change the name of any functions in the starter code Remove the #ifndef _testing__ and #endif lines Compilation: Compile your code with the Wall and -Werror flags. For instance, you might compile this exercise using: gcc -Wall -Werror ex4.c What to submit: You will submit your .c program file only. The file name does not matter. 1 ** 2 2 * Exercise 4. Structs and Pointers 3 4 * Please read the comments below carefully, they describe your task in detail. 5 * Any clarifications and corrections will be posted to Piazza so please keep an eye on the forum! 7 a a * Starter code: Angela Zavaleta & Mustafa Quraish, 2020 9 9 10 11 #include 12 #include 13 #include 14 15 #define MAX_STRING_LEN 128 16 17 typedef struct dimensions 18 ( 19 // This struct stores the dimensions of an object. 20 // The fields should be self explanatory. The units 21 // don't matter here, but say they are meters and kgs. 22 float height; 23 float depth; 24 float width; 25 float weight; 26 Dimensions; 27 28 typedef struct furniture 29 ( 30 // This struct stored information about a piece of 31 // furniture. Note the nested struct containing the 32 // information on the furniture dimensions. 33 char namo [MAX_STRING_LEN); char material[MAX_STRING_LEN); 35 float price; 36 Dimensions dimensions; 37 Furniture; 38 40 41 int find_material(Furniture catalog[], int catalog_size, char -material, Furniture *results[]) 43 { 42 44 45 46 47 * 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 itern, 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. 48 50 51 52 53 54 * Additionally, return the number of matches found. return -1; // Updated results directly and return number of matches 56 } 57 58 int find_price(Furniture catalog[], int catalog_size, float min_price, 58 int find_price(Furniture catalog[], int catalog_size, float min_price, 59 float max_price, Furniture #results[]) 60 { 61 62 Similar to the previous function, this time find all the items in the 63 * catalog that are within the given price range inclusive). The order of 64 * insertion to 'results must be the same as the order in which the items 65 * appear in the catalog. Assume results has enough space, 66 * Askiitionally, return the number of matches found. 68 49 70 return -1; // Updated results' directly and return number of matches 71 ) 72 75 char *biggest_ripott_name(Furniture catalog[], int catalog_size) 74 75 76 - We all know that the bigger an item is more volume), the better it is (, ! 77 - (Maybe not, but let's assume that is the case). The biggest ripoff would 78 be the item that costs the most amount of money per unit volume. Assume 79 * that it is unique, and return it's name. 80 81 92 return NULL; // Return the name of the correct item 83] 94 95 Furniture *update_material(Furniture catalog[], int catalog_size, char *nane, 96 char *new_material) 97 { 38 89 Given the furniture catalog, the unique) name of an item, and a new 90 * material name, update the material of the furniture item in the catalog 91 with the given name. Retumse pointer to the updated item. 92 93 - I no item exists in the catalog with the given name, don't change 914 anything and return NULL. 95 96 93 return NULL; // Update the correct itcm if needed and return the pointer 98] 99 100 Furniture *update_dimensions(Furniture catalog[], int catalog_size, char *name, 101 Dimensions *new_dimensions) 102 { 103 104 * Given the furniture catalog, the (unique) name of an item, and a 105 > pointer to a new dimensions struct, update the dimensions of the 106 * furniture Item in the catalog with the given name. Retums s pointer 107 to the updated item. 100 109 I no item exists in the catalog with the given name, don't change 110 anything and retum NULL. 111 112 113 return NULL; // Update the correct item if needed and return the pointer 114 ) 116 ///wwwwwwwwwww 117 118 #ifndef __testing__ // You know the drill; don't remove this. 119 120 // However, Feel free to change the line below to add more tests. 121 #dcfinc CATALOG_SIZE 10 122 123 int main() 124 ( 125 126 // Defining and array of furniture items. Note that when initializing a 127 // a struct (ar an array), we can use the {--- } shorthand for them! 128 Furniture catalog(CATALOG_SIZE] = { 129 |-------- Dimensions ------------| 130 // nane material price height depth width weight 131 {"Dacksta table", "Wood", 49.99, [2, 1, 1, 19}}, 132 {"Ektorp sofa", "Leather", 99.99, (2, 5, 1, 100}}, 133 {"Paang arnchair", "Plastic", 24.95, {1, 0.5, 0.5, 4}}, 134 {"Kallax shelves", "Wood", 149.99, (2, 1, 0.3, 78}}, 135 {"Flaggskepp lamp", "Metal", 17.99, (0.1, 0.1, 0.1, 1}}, 136 {"Balkarp sofa", "Leather", 120.88, (0.5, 2, 2.5, 58}}, 137 {"Janaxel basket", "Metal", 69.99, 12, 1, 1, 10}}, 138 {"Nordli bed", "Wood", 339.82, (0.2, 2, 1, 28}}, 139 {"Renberget chair", "Plastic", 59.99, {i, 0.5, 0.5, 9}}, 140 {"Kolbjorn cabinet", "Metal", 99.68, (0.8, 8.4, 0.8, 17}}, 141 142 143 // Let's create an array to store the results and the number of results. 144 // At most, the number of matches is the same as the size of the array. 145 Furniture #results[CATALOG_SIZE); 146 int results_n; 147 148 149 Find material 150 151 152 results_n = find_material catalog, CATALOG_SIZE, "wood", results); 153 154 // To make sure our search was done correctly, we will print the results 155 printf("Found Nd items made of Wood: ", results_n); 156 for (int i = @; i name); 159 } 160 161 162 Find Price 163 164 165 results_n = find_price(catalog, CATALOG_SIZE, B., 58.0, results); 166 167 // To make sure our search was done correctly, we will print the results 160 printf(" Found 9d items between $8 and $50 (inclusive): ", results_n); 169 for (int i = 0; i name, results[i]->price); 172 } 161 162 Find Price 163 164 166 results_n = find_price(catalog, CATALOG_SIZE, 8.2, 58.8, results); 166 167 // To make sure our search was done correctly, we will print the results 168 printf(" Found 5d itens between $8 and $50 (inclusive): ", results_n); 169 for (int i = @; i nane, results[i]->price); 172 } 173 174 175 Biggest Ripoff Name 176 127 178 char *nate = biggest_ripoff_name(catalog, CATALOG_SIZE); 179 printf(" Name of the biggest ripoff item is: Xs ", name); 180 181 182 74 Update Material 182 184 185 // Let's create a pointer to a Furniture and the item we need to test 4 and 5 186 Furniture #ret; 187 ret = update_material(catalog, CATALOG_SIZE, "Docksta table", "Plastic"); 188 printf(" s now has material 89 ", ret->name, ret->material); 189 190 ***** 191 Update Dimensions 192 193 194 Dimensions new_dims - {9.1, 8.1, 7.1, 6.1); 195 196 ret = update_dimensions(catalog, CATALOG_SIZE, "Balkarp sofa", &new_dims); 197 printf(" s now has dinensions : {Xf, xf, xf, Xf} ", ret->name, 198 ret->dimensions.height, ret->dimensions.depth, ret->dimensions.width, 199 ret->dimensions.Neight); 200 201 return 0; 202] 203 Bendif 204

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data 29th British National Conference On Databases Bncod 2013 Oxford Uk July 2013 Proceedings Lncs 7968

Authors: Dan Olteanu ,Georg Gottlob ,Christian Schallhart

2013th Edition

3642394663, 978-3642394669

More Books

Students also viewed these Databases questions