Answer:
a) struct GroceryItem{
string name;
float cost; };
b) GroceryItem apple;
apple.name = "Granny Smith";
apple.cost = 0.79;
c) GroceryItem inventory[20];
d) inventory[5].name = "Cheese";
inventory[10].cost = 3.50;
Step-by-step explanation:
a) GroceryItem is the name of the structure and struct is a keyword which is used to create GroceryItem structure.
The structure has two members one is name of type string which means its holds the name strings of items. The other is cost which is a float type variable which supports floating point numbers and its holds the cost of item.
b) GroceryItem apple statement means that a variable apple is created which is of type GroceryItem
apple.name = "Granny Smith" This statement uses the apple to access the member variable name of GroceryItem structure and sets the apples's name to Granny Smith. The dot between apple variable and member variable name is basically used to access the member variable name of GroceryItem .
apple.cost = 0.79; This statement uses the apple variable to access the member variable cost of GroceryItem structure in order to set the apples's cost to 0.79. The dot between apple variable and member variable cost is basically used to access the member variable cost of structure.
c) GroceryItem inventory[20]; This statement creates an array of GroceryItem structure. The array name is inventory and [20] is basically specifies the size of this array which is 20.
d) inventory[5].name = "Cheese"; This statement uses inventory array and access the name member of the structure GroceryItem to set the name of the 6th GroceryItem in inventory array to "Cheese". .Here [5] is the index of the 6th item of the inventory array as the array locations start from 0. So inventory[20] can contains 20 elements from 0 index to 19 index. So to set the name of 6th item 5th index is specified.
inventory[10].cost = 3.50; This statement uses inventory array and access the cost member of the structure GroceryItem to set the cost of the 11th GroceryItem in inventory array to "3.50". Here [10] is the index of the 11th item of the inventory array as the array locations start from 0. So inventory[20] can contains 20 elements from 0 index to 19 index. So to set the cost of 11th item 10th index is specified