csc 107 – programming for science. today’s goal learn what structures are & how they are...

33
LECTURE 35: STRUCTURES CSC 107 – Programming For Science

Upload: cornelia-mcbride

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

LECTURE 35:STRUCTURES

CSC 107 – Programming For Science

Page 2: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Today’s Goal

Learn what structures are & how they are used Why we need & why would use a structure What fields mean and how they are

declared How variables of struct type work and

interact

Page 3: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

All About the Variables

Variable names location to store data Memory location's initial value is unknown Assignments update memory location with

new value Value at memory location used in place of

variable Type specified for each variable

declared Type defines exactly how variable can be

used Implicitly defines memory needed for

variable Specifies how the memory location set &

interpreted

Page 4: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Problem With Variables

What type can be used for this:

Page 5: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Problem With Variables

Is putting an int, double, or cString

Page 6: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Problem With Variables

What is the data type describing this action?

Page 7: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Problems With Variables

Sometimes need to group data Pixels: red, green, blue, & alpha

components Forces: mass & vector of acceleration Atomic Particles: position & charge & spin Students: name, assignments, programs, &

tests Would like to do this within single

variable But requires multiple values defined for

each variable Normally want to do this for many variables

Page 8: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

What Is a struct?

Similar to real-world blueprint or schematic Outlines how to group items to make bigger

thing May/will have to end up making many

copies of item No change to others when single item

changed

Page 9: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Creating A struct

Declare struct outside of any function After #includes where normally find

declarations Each struct must be given name

Follows normal naming rules & conventions Within struct, must declare 1 or more

fields Name & data type required for each field Fields can be listed in any order Act like variables belonging to each copy of struct

Each struct variable gets own copy of fields

Page 10: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Declaring A Struct

struct hurricane { char name[10]; int year; int category;};

struct pixel { int red, blue, green; double red;};

struct student { char name[40]; // Need space for full name double year, gpa; // Okay, even with hurricane};

Page 11: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Declaring A Struct

struct hurricane { char name[10]; int year; int category;};

struct pixel { int red, blue, green; double red;};

struct student { char name[40]; // Need space for full name double year, gpa; // Okay, even with hurricane};

Page 12: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Declaring A Struct

struct hurricane { char name[10]; int year; int category;};

struct pixel { int red, blue, green; double red;};

struct student { char name[40]; // Need space for full name double year, gpa; // Okay, even with hurricane};

Page 13: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Declaring A Struct

struct hurricane { char name[10]; int year; int category;};

struct pixel { int red, blue, green; double red;};

struct student { char name[40]; // Need space for full name double year, gpa; // Okay, even with hurricane};

Page 14: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Using structs

Type of the variable is specific struct Fields are like variable just as array

entries are Requires having actual variable to access Each field is set & used independent of

others Code can access field as

variableName.fieldName

Page 15: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Using structs

Type of the variable is specific struct Variable gets own copy of all of the

fields Like variable or array entry, initial values

unknown No link between fields even if names are

same Even variables of same struct, fields not

linked

Page 16: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Variables of struct Type

struct color { int red, green, blue;};

int main(void) { struct color yellow = { 158, 154, 0 }; struct color purple; purple.red = 102; purple.green = 102; // Better way to initialize purple.blue = 152; cout << yellow.red << " != " << purple.red;

Page 17: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Using structs

Can assign struct variables to one another Variables must be of identical struct types Copies value of all fields but still remain

independent Locations will be same, since pointers &

arrays aliased In all other situation, must use fields

Cannot add, multiply, compare struct variables

For any legal use, individual fields always available

Arrays of structs can also be declared Within the array, each entry is struct

variable

Page 18: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 19: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 20: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 21: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 22: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 23: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 24: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 25: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 26: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 27: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 28: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 29: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 30: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Is It Legal?

struct momentum { double mass, velocityX, velocityY; };momentum bigMo;struct momentum car, truck;struct momentum van;van = 34;truck = {12, 34, 465.23};car.mass = 22;truck.mass = car.mass * 34;cout << van << " " << car.velocityY << endl;car.velocityX -= pow(truck.mass, 2);van = sin(car);van.velocityX = car.velocityY + truck.velocityX;

Page 31: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Ooohhh… tracing colors

struct color { int red, green, blue;};int main(void) { struct color paints[2]; struct color pantone; paints[0].red = 128; paints[0].blue = 0; paints[0].green = paints[0].blue * 2; paints[1].red = 0; paints[1].blue = paints[0].red; paints[1].green = (paints[0].green+paints[1].blue); paints[1].blue = 0; paints[0].red = 0; pantone = paints[1];

Page 32: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

Your Turn

Get into your groups and try this assignment

Page 33: CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields

For Next Lecture

Mix pointers & structs in 13.3 How do we get address of a struct for

pointer? Anything need to change to use field with

pointer? How can we create arrays of structs

dynamically?

Angel has Weekly Assignment #12 for tomorrow

Last programming assignment available now