2007 pearson education, inc. all rights reserved. structs as function arguments and results arrays...

37
2007 Pearson Education, Inc. All rights rese Structs as Function Arguments and Results Arrays Pass by referance Struts the same way as the basic types in C - char, int, float - passed as arguments - returned as results 1

Upload: lorenzo-lavine

Post on 15-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

Structs as Function Arguments and Results

Arrays– Pass by referance

Struts– the same way as the basic types in C - char, int, float

- passed as arguments

- returned as results

1

Page 2: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

typedef struct Person{

int age;

char gnd;

float weight;

} PERSON;

2

Page 3: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

the use of the members of a struct.

int adjustAge( int oldAge ){

if ( oldAge < 39 ) return( ++oldAge );

else return( oldAge );

/*return (oldAge<39) ? ++oldage : oldage;*/

}

void main(){

PERSON Jim;

:

Jim.age = adjustAge( Jim.age );

:

}

3

Page 4: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

typedef struct Book{

int edition;

int pages;

double weight;

char type;

int WC; /*weight classification 1 heavy, 0 is not */

} BOOK;

Write a function tack an argument double to test the weight of a book and return 1 if the weight greater than 2 and 0 otherwise. The value to be assign to WC member.

4

Page 5: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

int weightC(double w){

if(w > 2) return 1;

else return 0;

}

void main(){

BOOK myBok;

:

myBoK.wc = weightC(myBok.weight);

:

}

5

int weightC(double w){

return (w > 2)? 1 : 0;

}

Page 6: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

ASS

Consider the student struct: add a new member name it GL /*Grades Later*/

Write a function tack a double to test the average of the student and return a later match the average to be assign to the new member

Test the function

6

Page 7: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

The whole of a struct is returned by a function

PERSON get_details() {

PERSON temp;

printf("Please enter Age, Gender and Weight (Kg): ");

scanf("%d %c%f", &temp.age, &temp.gnd, &temp.weight );

return temp;

}

void main(){

PERSON Jim, Mary, Sid;

:

Jim = get_details();

:

}

7

Page 8: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

Write a function allow the user to input the details of a BOOK and return it back

typedef struct Book{

int edition;

int pages;

double weight;

char type;

} BOOK;

8

Page 9: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

BOOK GetBook(){

BOOK tmp;

printf(“\nEnter the BOOK information:”);

printf(“ [edition, pages, weight, and type]”);

scanf(“%d%d%f%c”, &tmp.edition, &tmp.pages. &tmp.weight. &tmp.type);

retrun tmp;

}

void main(){

BOOK myBook;

:

myBook = GetBook();

:

}

9

Page 10: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

ASS

Write a function allow the user to input the details of a STUDENTand return it back

Test the function

10

Page 11: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

Passing a struct as a function argument

void show_details( PERSON who ){

printf( "Person's age was: %d\n", who.age );

printf( " Gender was: %c\n", who.gnd );

/* Kg -> lb */

who.weight = who.weight * 2.2;

printf( " weight (lb) was: %5.1f\n", who.weight );

}

void main(){

PERSON Jim, Mary, Sid;

:

Jim = get_details();

:

show_details(Jim);

}

11

Page 12: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

Write a function tack a BOOK and print the content out

12

Page 13: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

void PrintBook( const BOOK b){

printf(“The BOOK information:\n”);

printf(“\n\tedition: %d\n\tpages: %d”, b.edition, b.pages);

printf(“\n\tweight: %f \n\ttype: %c”, b.weight, b.type);

}

void main(){

BOOK myBook = {2, 120, 2.5, ‘A’};

:

PrintBook(myBook);

:

}

13

Page 14: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

ASS

Write a function tack a STUDENT and print the content out

Test the function

14

Page 15: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

Arrays of structs

to store the same package of information for a number of different entities

typedef struct Person{

int age;

char gnd;

float weight;

} PERSON;

15

Page 16: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

Arrays of structs

PERSON employee[12];

We have an array of 12 elements each of which is a structure with 3 members. We can access any member of any element,

employee[9].age = 45;

Also we can treat each element as a whole structshow_details(employee[5]);

employee[3] = get_details();

16

Page 17: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

Consider the following struct BOOK, Create an array of three BOOK s

typedef struct Book{

int edition;

double weight;

char type;

} BOOK;

17

Page 18: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

BOOK academic[3];

BOOK academic[3] = {

{1,2.5,’a’},

{2,3.5,’n’},

{2,1.5,’a’}

};

Read in the information of the second

18

Page 19: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

academic[1] = GetBook();

Printout the information of the first

19

Page 20: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

PrintBook(academic[0]);

Printout the content of the array

20

Page 21: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

int i;

for(i=0; i<3; i++)

PrintBook(academic[i]);

21

Page 22: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

ASS

1. Create an array of four STUDNTs with initial values

2. – Create another array of five STUDNTs

– Read in the information of the whole student

– Printout the content

– Assign the second the information of the second of the array created at 1

– Printout the information of the second

22

Page 23: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

Structs Containing Arrays

typedef struct{

int age;

char gnd;

float weight;

char name[20];

} HUMAN;

HUMAN ahm = { 45, ‘M’, 95.1, “Ahmad Ali”};HUMAN ahm = { 45, ‘M’, 95.1, {‘A’, ‘h’, ‘m’, ‘a’, ‘d’, ‘ ‘, ‘A’, ‘l’, ‘i’, ‘\0’}};

23

Page 24: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

char initial = ahm.name[0];

putchar(initial);

Printout the 3rd char of ahm’s name

24

Page 25: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

char third = ahm.name[2];

printf(“%c”, third); /*putchar(third)*/

allow the user to input the 6th char of ahm’s name

25

Page 26: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

scanf(“%c”, &ahm.name[5]);

print the name char by char

26

Page 27: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

int i;

for(i=0; i<20; i++)

putchar(ahm.name[i]);

i=0;

while( ahm.name[i] != ‘\0’)

putchar(ahm.name[i++]);

Print out the name at once

27

Page 28: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

printf(“%s\n”, ahm.name);

puts(ahm.name);

allow the user to input the ahm’s name

28

Page 29: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

scanf(“%s”, ahm.name);

gets(ahm.name);

Which is better?

29

Page 30: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

typedef struct Book{

int edition;

double weight;

char type;

} BOOK;

Rewrite the BOOK with a new member title

30

Page 31: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

typedef struct Book{

int edition;

double weight;

char type;

char title[30];

} BOOK;

Create a variable of book with initial values

31

Page 32: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

BOOK myBook = {1,2.5,’a’, “C programming” }

change the title to “c how to program”

32

Page 33: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

gets(myBook.title);

scanf(“%s”, myBook.title);

33

Page 34: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

ASS

Create a Time struct with classification member (“PM”, “AM”)

Write a function to print out the content of the Time

Create a STUDENT struct with a name member up to 30 char

Write two functions to read the information of a student and the second to print out the information.

34

Page 35: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

HUMAN client[2] = {

35, ‘M’, 95.1, “Ahmad Ali”,

25, ‘F’, 50.9, "Salma Zakarea”

};

printf( "%s\n", client[1].name );

printf( "%c\n", client[1].name[0] );

35

Page 36: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

HUMAN client[2] = {

35, ‘M’, 95.1, “Ahmad Ali”,

25, ‘F’, 50.9, "Salma Zakarea”

};

HUMAN temp;

Assign the values of first client to temptypedef struct{

int age;

char gnd;

float weight;

char name[20];

} HUMAN;

36

Page 37: 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the

2007 Pearson Education, Inc. All rights reserved.

temp = client[0];

temp.age = client[0].age;

temp.gnd = client[0].gnd;

Temp.weight = client[0].weight;

strcpy(temp.name, client[0].name);

37