Structures.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return.
Structures Spring 2013Programming and Data Structure1.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
Linked Lists. Array Limitations Arrays have a fixed size that cannot be changed at run time What if your program had an array to store info regarding.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Multidimensional Arrays. Example Write a program to keep track of all warmup scores for all students. Need a list of a list of scores Student – score.
1 Passing Array Array’s element can be passed individually to a function; copying value exist during passing process. An entire array can be passed to.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
Pointers, Dynamic Memory Allocation and Structures.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
…WHAT YOU SHOULD HAVE LEARNED IN ETEC1101… IN JAVA 2. C => JAVA.
User Defined Data Types - updated Chapter 10: User Defined Data Types Objectives: In this chapter you will learn about, Introduction Declaring.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
1 C Programming Week 2 Variables, flow control and the Debugger.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Functions with Input/Ouput. Assignments Due – Lab 2 Reading – Chapter 4 –
Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
How to design and code functions Chapter 4 (ctd).
 Complex data types  Structures  Defined types  Structures and functions  Structures and pointers.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
chap11 Chapter 11 Structure and Union Types.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Introduction We will study how to broaden the modeling facilities of C by defining our own data types that represent structured collections of data pertaining.
LINKED LISTS.
CSE 220 – C Programming Pointers.
Programmazione I a.a. 2017/2018.
הגדרת משתנים יום שלישי 27 נובמבר 2018
INC 161 , CPE 100 Computer Programming
EKT150 : Computer Programming
Programming in C Pointer Basics.
Structures vol2.
Programming in C Pointer Basics.
Structure and Union Types
Week 2 Variables, flow control and the Debugger
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Programming in C Pointer Basics.
Chapter 10 C Structures and Unions
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Chapter 11 Structure and Union Types.
EECE.2160 ECE Application Programming
Structures, Unions, and Enumerations
Presentation 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 type typedef 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 5 items[4].name; items[4].num_in_inventory = items[4].num_in_inventory – 1; items[5].cost = 105.99; scan_item(&items[10);