ecs30 Winter 2012: Programming and Problem Solving # 18: Chapters 11~12, Structure and File

1 / 40

# ecs30 Winter 2012: Programming and Problem Solving # 18: Chapters 11~12, Structure and File - PowerPoint PPT Presentation

ecs30 Winter 2012: Programming and Problem Solving # 18: Chapters 11~12, Structure and File. Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/. Recursion. A function calls itself, maybe with a different set of parameter values…

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' ecs30 Winter 2012: Programming and Problem Solving # 18: Chapters 11~12, Structure and File' - red

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### ecs30 Winter 2012:Programming and Problem Solving#18: Chapters 11~12, Structure and File

Prof . S. Felix Wu

Computer Science Department

University of California, Davis

http://dsl.ucdavis.edu/~wu/ecs30/

ecs30 Winter 2012 Lecture #17

Recursion
• A function calls itself, maybe with a different set of parameter values…
• Different sets of local variables…

(n-1)!

N!

ecs30 Winter 2012 Lecture #17

A Legend

Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi.

Stacked upon the leftmost needle were 64 golden disks, each a different size, stacked in concentric order:

ecs30 Winter 2012 Lecture #17

A Legend

The priests were to transfer the disks from the first needle to the second needle, using the third as necessary.

But they could only moveone disk at a time, and could never put a larger disk on top of a smaller one.

When they completed this task, the world would end!

ecs30 Winter 2012 Lecture #17

Testing

The Hanoi Towers

Enter how many disks: 4

move a disk from A to B

move a disk from C to B

move a disk from A to C

move a disk from B to A

move a disk from B to C

move a disk from A to C

move a disk from A to B

move a disk from C to B

move a disk from C to A

move a disk from B to A

move a disk from C to B

move a disk from A to C

move a disk from A to B

move a disk from C to B

ecs30 Winter 2012 Lecture #17

void

tower

(char from_peg, char to_peg, char aux_peg,

int n)

{

}

int

main(void)

{

int n;

scanf(“%d”, &n);

tower(‘A’, ‘B’, ‘C’, n);

}

ecs30 Winter 2012 Lecture #17

HW#7
• Tower of Hanoi with FOUR Pegs

Number of Moves? (needs to be smaller than 3 pegs)

ecs30 Winter 2012 Lecture #17

http://www.exocortex.org/toh/

void

tower4P

(char from_peg, char to_peg,

char aux1_peg, char aux2_peg,

int n)

{

if (n == 1)

printf(“move disk %d from %c to %c\n”,

n, from_peg, to peg);

}

ecs30 Winter 2012 Lecture #17

structcompound data type

#define STRSIZ 10

typedefintFelix_integer_t;

typedefstruct {

char name[STRSIZ];

double diameter;

int moons;

double orbit_time,

rotation_time;

}planet_t;

planet_tcurrent_planet;

ecs30b Fall 2008 Lecture #21

structcompound data type

#define STRSIZ 10

struct planet {

char name[STRSIZ];

double diameter;

int moons;

double orbit_time,

rotation_time;

};

struct planet current_planet;

ecs30b Fall 2008 Lecture #24

Access the members (or attributes)!

ecs30b Fall 2008 Lecture #21

#define STRSIZ 10

typedef struct {

char name[STRSIZ];

double diameter;

int moons;

double orbit_time,

rotation_time;

} planet_t;

planet_t current_planet;

ecs30b Fall 2008 Lecture #21

&current_planet

#define STRSIZ 10

typedef struct {

char name[STRSIZ];

double diameter;

int moons;

double orbit_time,

rotation_time;

} planet_t;

planet_t current_planet;

ecs30b Fall 2008 Lecture #21

&current_planet

#define STRSIZ 10

typedef struct {

char name[STRSIZ];

double diameter;

int moons;

double orbit_time,

rotation_time;

} planet_t;

planet_t current_planet;

sizeof(planet_t) == ?

ecs30b Fall 2008 Lecture #21

planet_t

xyz(planet_t x)

{

strcpy(x.name, "abcdefghj");

return x;

}

int

main(void)

{

planet_t current;

planet_t next;

strcpy(current.name, "rstuvwxyz");

next = xyz(current);

printf("%s\n", next.name);

return 0;

}

ecs30b Fall 2008 Lecture #21

planet_t

xyz(planet_t x)

{

strcpy(x.name, "abcdefghj");

return x;

}

int

main(void)

{

planet_t current;

scanf(“%d”, &(current.moons));

return 0;

}

ecs30b Fall 2008 Lecture #21

planet_t *

xyz_cbr(planet_t *xp)

{

strcpy(xp->name, "abcdefghj");

return xp;

}

int

main(void)

{

planet_t current;

planet_t *next;

strcpy(current.name, "rstuvwxyz");

next = xyz_cbr(&current);

printf("%s\n", next->name);

return 0;

}

ecs30b Fall 2008 Lecture #21

struct X vs. struct X *
• struct X x; ~ x.<attrID>
• struct X *xp; ~ xp-><attrID>
• xp = &x;
• x = *xp;
• y = x;

ecs30b Fall 2008 Lecture #24

planet_t *

xyz_cbr(planet_t *xp)

{

strcpy(xp->name, "abcdefghj");

return xp;

}

int

main(void)

{

planet_t current;

planet_t *next;

strcpy(current.name, "rstuvwxyz");

next = xyz_cbr(&current);

printf("%s\n", next->name);

return 0;

}

ecs30b Fall 2008 Lecture #24

Assignment for struct

planet_t x,y;

y = x;

strcpy(y.name, x.name);

y.diameter = x.diameter;

y.moons = x.moons;

y.orbit_time = x.orbit_time;

y.rotation_time = x.rotation_time;

ecs30b Fall 2008 Lecture #24

Assignment for struct

planet_t x,y;

y = x;

strcpy(y.name, x.name);

y.diameter = x.diameter;

y.moons = x.moons;

y.orbit_time = x.orbit_time;

y.rotation_time = x.rotation_time;

Not entirely right!!

ecs30b Fall 2008 Lecture #24

Assignment for struct

planet_t x,y;

y = x;

bcopy(&x, &y, sizeof(planet_t));

ecs30b Fall 2008 Lecture #24

Assignment for struct

planet_t x,y;

y = x;

bcopy(&x, &y, sizeof(planet_t));

strncpy(&y, &x, sizeof(planet_t));

ecs30b Fall 2008 Lecture #24

Assignment for struct

planet_t x,y;

y = x;

bcopy(&x, &y, sizeof(planet_t));

strncpy(&y, &x, sizeof(planet_t));

The strncpy() function copies at most n characters from &x into &y. If &x is less than n characters long, the remainder of &y is filled with ‘\0’ characters. Otherwise, &y is not terminated.

ecs30b Fall 2008 Lecture #24

Assignment for struct

planet_t x,y;

y = x;

bcopy(&x, &y, sizeof(planet_t));

planet_t myfunc(…);

x = myfunc(…);

ecs30b Fall 2008 Lecture #24

#define STRSIZ 10

struct planet {

char name[STRSIZ];

double diameter;

int moons;

double orbit_time,

rotation_time;

struct planet next;

struct planet previous;

};

ecs30b Fall 2008 Lecture #24

No Recursive in struct!

#define STRSIZ 10

struct planet {

char name[STRSIZ];

double diameter;

int moons;

double orbit_time,

rotation_time;

struct planet next;

struct planet previous;

};

sizeof(struct planet) == ?

ecs30b Fall 2008 Lecture #24

#define STRSIZ 10

struct planet {

char name[STRSIZ];

double diameter;

int moons;

double orbit_time,

rotation_time;

struct planet *next;

struct planet *previous;

};

sizeof(struct planet) == ?

ecs30b Fall 2008 Lecture #24

struct planet {

char name[STRSIZ];

struct orbit *xyz;

};

struct orbit {

struct planet *p[12];

};

ecs30b Fall 2008 Lecture #24

struct orbit;

struct planet {

char name[STRSIZ];

struct orbit *xyz;

};

struct orbit {

struct planet *p[12];

};

ecs30b Fall 2008 Lecture #24

struct orbit;

struct planet {

char name[STRSIZ];

struct orbit *xyz;

};

struct orbit {

struct planet p[12];

};

ecs30b Fall 2008 Lecture #24

#define STRSIZ 10

typedef struct {

char name[STRSIZ];

double diameter;

int moons;

double orbit_time,

rotation_time;

} planet_t;

printf("%d\n", ((planet_t *) 0)->diameter);

printf("%d\n", &(((planet_t *) 0)->diameter));

ecs30b Fall 2008 Lecture #24

#define STRSIZ 10

typedef struct {

char name[STRSIZ];

double diameter;

int moons;

double orbit_time,

rotation_time;

} planet_t;

printf("%d\n", (int) &(((planet_t *) 0)->name));

printf("%d\n", (int) &(((planet_t *) 0)->diameter));

printf("%d\n", (int) &(((planet_t *) 0)->moons));

ecs30b Fall 2008 Lecture #24

#define STRSIZ 10

typedef struct {

char name[STRSIZ];

double diameter;

int moons;

double orbit_time,

rotation_time;

} planet_t;

printf("%d\n", (int) &(((planet_t *) 0)->name));

printf("%d\n", (int) &(((planet_t *) 0)->diameter));

printf("%d\n", (int) &(((planet_t *) 0)->moons));

Expect: 0, 10, 18

MacBook/pc16 (i386): 0, 12, 20

pc26 (x86_64): 0, 16, 24

ecs30b Fall 2008 Lecture #24

typedef struct {

char name[10];

double diameter;

int moons;

double orbit_time,

rotation_time;

} planet_t;

&((planet_t *) 0)->diameter)

“Be very careful!

It’s Machine-Dependent actually!!”

ecs30b Fall 2008 Lecture #24

I386 architecture

typedef struct {

char name[10];

double diameter;

int moons;

double orbit_time,

rotation_time;

} planet_t;

ecs30b Fall 2008 Lecture #24