Top Banner
1 Structures
21

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.

Mar 31, 2015

Download

Documents

Nicole Sconce
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: 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.

1

Structures

Page 2: 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.

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.

Page 3: 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.

3

Structure Definitionstruct name{

variable declaration;variable declaration;..

};

the keyword struct announces that this is a structure definition

Page 4: 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.

4

Initializing Structures

struct Rect{

double x;double y;

char color;double width;double height;

};struct Rect r1 = {0,0,’r’,5,10};

0

0

r

5.0

10.0

x

y

color

width

height

r1

Page 5: 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.

5

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

Page 6: 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.

6

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;

Page 7: 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.

7

Scope of a Structure

Member variables are local to the structure. Member names are not known outside the

structure.

Page 8: 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.

8

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)

Page 9: 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.

9

Pair Structure Store two integers to represent the first

and second number of pair

struct pair

{

int first;

int second;

};

Page 10: 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.

10

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

Page 11: 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.

11

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

Page 12: 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.

12

How to use the functionsstruct 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);

Page 13: 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.

13

Exercise Update the program to support the

following on pairs

c*(a,b) = (c*a,c*b)

(a,b)^c = (a^c,b^c)

Page 14: 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.

14

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

0 1 2 3 19

……….......

xy

colorwidth

height

Page 15: 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.

15

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.

Page 16: 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.

16

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;

}

10

1.5

ival

dval

s1

10

1.5

s

11

3.5

Updated s

Page 17: 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.

17

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

0 1 51

Page 18: 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.

18

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

Page 19: 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.

19

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

Page 20: 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.

20

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 }

0 1 2 3 51…………………………

Page 21: 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.

21

BlackJack Shuffle the deck Deal first 2 cars 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