+ All Categories
Home > Documents > Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of...

Structures. Parallel Arrays Inventory –One array keeps track of cost –One array keeps track of...

Date post: 21-Dec-2015
Category:
View: 215 times
Download: 0 times
Share this document with a friend
21
Structures
Transcript

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 to keep track of?

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

Structures

• Group many record items into one element

Inventory:

Item Name: Shirt

Item Number: 1232

Cost: $20.00

Num in Inventory: 10

Structures

typedef struct {char name[20];

int number;

double cost;

int num_in_inventory;

} inventory_item_t;

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;

Declaring and Initializing

• typedef does not allocate any memory

inventory_item_t shirts;

.name

.number

.cost

.num_in_inventory

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

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

Accessing

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

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);

Structures and Functions

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

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);

}

Structures and Functions

• Call the function

inventory_item_t shirts;

print_item(shirts);

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?

Structures as Output Parameters

• How would we call this function?

inventory_item_t shirts;

sell_item(shirts); //OK?

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”);}

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);

}

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);

}

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);

}

Arrays of Structures

inventory_item_t items[20];

• Access the name of item 5

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);


Recommended