1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Introduction to C Programming
Introduction to C Programming
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Chapter 7: Arrays In this chapter, you will learn about
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
LIST PROCESSING.
ABC Technology Project
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
1 / / / /. 2 (Object) (Object) –, 10 (Class) (Class) –, –, – (Variables) [ Data member Field Attribute](, ) – (Function) [ Member function Method Operation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Programming in C Chapter 10 Structures and Unions
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
C Programming Getting Started Getting Started Hello World Hello World Data types and variables Data types and variables main() main() I/O routines I/O.
1 Chapter Eleven Arrays. 2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3);
Squares and Square Root WALK. Solve each problem REVIEW:
1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
C Language.
Addition 1’s to 20.
25 seconds left…...
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter Three Arithmetic Expressions and Assignment Statements
Week 1.
We will resume in: 25 Minutes.
WEEK 12 Class Activities Lecturer’s slides.
Chapter 13 – Introduction to Classes
Data Structures Using C++ 2E
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
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.
By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.
Structures Spring 2013Programming and Data Structure1.
C and Data Structures Baojian Hua
Programming in C Structs and Unions. No Classes in C Because C is not an OOP language, there is no way to combine data and code into a single entity.Because.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Structures.
Introduction to Programming
Presentation transcript:

1 Structures

2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members can be of different types.

3 Structure Definition struct name { variable declaration;. }; the keyword struct announces that this is a structure definition

Declaring Structures (struct) Reserves Space struct my_example { int label; char letter; char name[20]; } mystruct; Does Not Reserve Space (Definition) struct my_example { int label; char letter; char name[20]; } ; /* The name "my_example" is called a structure tag */

The C language provides a facility called typedef to form complex types from more-basic machine types and assign simpler names (alias) to such combinations. For example, the declaration: typedef int Length; makes the name Length a synonym (or alias) for the data type int. The data type name Length can now be used in declarations in exactly the same way that the data type int can be used: Length a, b, len ; Length numbers[10] ; Data type aliasing

6 Initializing Structures struct Rect /* Definition */ { double x; double y; char color; double width; double height; }; struct Rect r1 = {0,0,r,5,10}; /* Declaration */ 0 0 r x y color width height r1

7 Accessing Data Members To access a data member use Varname.membername struct Rect r1 = {0,0,r,5,10}; r1.x r1.y r1.color r1.width r1.height 0 0 r 5 10 x y color width height r1

8 Assignment operator Assignment operator is defined for structure of the same type. struct rect { double x; double y; char color; double width; double height; }; struct rect r1, r2 r1.x = 10.5; r1.y = 18.99; r1.width = 24.2; r1.height = 15.9; r1.color = 'r'; /* Copy all data from r1 to r2. */ r2 = r1;

9 Scope of a Structure Member variables are local to the structure. Member names are not known outside the structure.

10 Exercise Write a program using structures that manipulates pairs. Addition and multiplication of pairs is defined below. (a,b)+(c,d)=(a+c,b+d) (a,b)*(c,d)=(a*c,b*d)

11 Pair Structure Store two integers to represent the first and second number of pair struct pair { int first; int second; };

12 Addition struct pair add(struct pair p1, struct pair p2) { struct pair temp; temp.first = p1.first + p2.first; temp.second = p1.second + p2.second; return temp; }

13 Multiplication struct pair multiply(struct pair p1, struct pair p2) { struct pair temp; temp.first = p1.first * p2.first; temp.second = p1.second * p2.second; return temp; }

14 How to use the functions struct pair mp1,mp2,mp3,mp4; printf("Enter first pair\n"); scanf("%d %d",&mp1.first, &mp1.second); printf("Enter second pair\n"); scanf("%d %d",&mp2.first, &mp2.second); mp3 = add(mp1, mp2); printf("Addition result = (%d,%d)\n",mp3.first,mp3.second); mp4 = multiply(mp1,mp2); printf("Multiplication result = (%d,%d)\n",mp4.first,mp4.second);

15 Exercise Update the program to support the following on pairs c*(a,b) = (c*a,c*b) (a,b)^c = (a^c,b^c)

16 Arrays of Structures Arrays of structures may be declared in the same way as other C data types. struct rect rectangles[20]; rectangles[0] references first structure of rectangles array. rectangles[0].color = r; ……… x y color width height

17 Structures as Arguments to Functions When a structure is passed as an argument to a function, it is a call-by-value reference. Changes made to the formal parameters do not change the argument. A pointer to a structure may also be passed as an argument to a function. Changes made to the formal parameters also change the argument.

18 Call by Value Example struct simple { int ival; double dval; }; void fun1(struct simple s) { s.ival = s.ival+1; s.dval = s.dval + 2; } int main(void) { struct simple s1 = {10, 1.5}; fun1(s1); printf(%i %lf\n, s1.ival, s1.dval ); return 0; } ival dval s s Updated s

19 Nested Structures Structure definitions may contain data members that are other structures: struct Card { char suit; int rank; }; struct Deck { struct Card cards[52]; int next_card = 0; }; suit rank Card …... …… next_card cards 0151

Nested Structures (more examples) struct address { int hno; char street[20]; char location[20]; char city[20]; char state[20]; char country[20]; int pincode; }; struct date { int day, month, year; }; typedef address addr_t; typedef date date_t; struct name { Char fname[20], mname[20], lname[20]; }; typedef name name_t; typedef struct { Char fname[20], mname[20], lname[20]; } name_t; Both the above given code blocks do the same. struct student { char rollnum[20]; char gender; name_t sname; addr_t current_addr; addr_t permanent_addr; date_t dbirth; date_t dregistration; };

21 Initialize the Deck for (i=0; i<13; i++) { d1.cards[i].suit = 'c'; d1.cards[i].rank = i+1; } for (i=13; i<26; i++) { d1.cards[i].suit = 'd'; d1.cards[i].rank = (i-13)+1; } for (i=26; i<39; i++) { d1.cards[i].suit = 'h'; d1.cards[i].rank = (i-26)+1; } for (i=39; i<52; i++) { d1.cards[i].suit = 's'; d1.cards[i].rank = (i-39)+1; }

22 Print the Deck void print(struct Deck d1) { int i; for (i=0; i<52; i++) printf("%c %d\n", d1.cards[i].suit, d1.cards[i].rank); return; }

23 How to Shuffle the Deck? for (i=0; i<52; i++) { // pick a random card x from 0-51 // swap card x and card i } …………………………

24 BlackJack Shuffle the deck Deal first 2 cards to user Print both cards on screen Next two cards to dealer Print only the first on screen Ask user whether he/she wants to continue Highest total <= 21 wins Jack, Queen, King are 10 points Ace is 11 points Other cards represented by their number