Top Banner
Structures
21

Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Structures

Page 2: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Parallel Arrays

• Inventory– One array keeps track of cost– One array keeps track of number in inventory

• What if there were many parallel items to keep track of?

• Would you want to keep track of 10, 20, 30 parallel arrays? Why not?

Page 3: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Structures

• Group many record items into one element

Inventory:

Item Name: Shirt

Item Number: 1232

Cost: $20.00

Num in Inventory: 10

Page 4: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Structures

typedef struct {char name[20];

int number;

double cost;

int num_in_inventory;

} inventory_item_t;

Page 5: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Structures

typedef struct {type name;…type name;

} struct_type;

• goes after #include and #define – not inside of main• type can be another structure data typetypedef struct {

char first[20];char last[20];

} name_t;typedef struct {

name_t name;int id;

} student_t;

Page 6: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Declaring and Initializing

• typedef does not allocate any memory

inventory_item_t shirts;

.name

.number

.cost

.num_in_inventory

Page 7: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Declaring and Initializing

shirts.number = 1232;

shirts.cost = 20.00;

shirts.num_in_inventory = 10;

//name???

.name

.number

.cost

.num_in_inventory

1232

20.00

10

Page 8: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Declaring and Initializing

shirts.number = 1232;

shirts.cost = 20.00;

shirts.num_in_inventory = 10;

strcpy(shirts.name, “Shirt”);

.name

.number

.cost

.num_in_inventory

1232

20.00

10

Page 9: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Accessing

• Print “There are 10 of item: shirt left in the inventory.”

Page 10: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Accessing

• Print “There are 10 of item: shirt left in the inventory.”

printf(“There are %d of item: %s left in the inventory”, shirts.num_in_inventory, shirts.name);

Page 11: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Structures and Functions

• Write a function to print all information about an inventory item

Page 12: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Structures and Functions

• Write a function to print all information about an inventory item

void print_item(inventory_item_t item) {

printf(“Item: %s\n”, item.name);

printf(“Item Number: %d\n”, item.number);

printf(“Cost: %lf\n”, item.cost);

printf(“Number Remaining: %d\n”, item.num_in_inventory);

}

Page 13: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Structures and Functions

• Call the function

inventory_item_t shirts;

print_item(shirts);

Page 14: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Structures as Output Parameters

• Write a function to “sell” an item by deducting 1 from the number of the item left in the inventory

• Return 1 if the sell was successful – 0 otherwise

• How would we call this function?

Page 15: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Structures as Output Parameters

• How would we call this function?

inventory_item_t shirts;

sell_item(shirts); //OK?

Page 16: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Structures as Output Parameters

• How would we call this function?

inventory_item_t shirts;int sell_ok;…sell_ok = sell_item(&shirts); if(sell_ok) {

printf(“Item sold\n”);} else {

printf(“Problem encountered! Item not sold.\n”);}

Page 17: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Structures as Output Parameters

int sell_item(inventory_item_t *to_sell) {int sell_ok;if((*to_sell).num_in_inventory > 0) {

//(*to_sell) is important!(*to_sell).num_in_inventory = (*to_sell).num_in_inventory –

1;sell_ok = 1;

} else {sell_ok = 0;

}return (sell_ok);

}

Page 18: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Component Selection Operator

• (*to_sell).num_in_inventory is the same as to_sell->num_in_inventory

int sell_item(inventory_item_t *to_sell) {int sell_ok;if(to_sell->num_in_inventory > 0) {

to_sell->num_in_inventory = to_sell->num_in_inventory – 1;sell_ok = 1;

} else {sell_ok = 0;

}return (sell_ok);

}

Page 19: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Returning a Structure

inventory_item_t getItem() {inventory_item_t new_item;printf(“Enter name:”);scanf(“%s”, new_item.name);printf(“Enter number:”);scanf(“%d”, &new_item.number);printf(“Enter cost:”);scanf(“%lf”, &new_item.cost):printf(“Enter number in inventory:”);scanf(“%d”, %new_item.num_in_inventory);return (new_item);

}

Page 20: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Arrays of Structures

inventory_item_t items[20];

• Access the name of item 5

Page 21: Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of number in inventory What if there were many parallel items.

Arrays of Structures

inventory_item_t items[20];

• Access the name of item 5items[4].name;

items[4].num_in_inventory = items[4].num_in_inventory – 1;

items[5].cost = 105.99;

scan_item(&items[10);