Download presentation
Presentation is loading. Please wait.
Published byCornelia McBride Modified over 9 years ago
1
CSC 107 – Programming For Science
2
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
3
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
4
Problem With Variables What type can be used for this:
5
Problem With Variables Is putting an int, double, or cString
6
Problem With Variables What is the data type describing this action?
7
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
8
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
9
Creating A struct Declare struct outside of any function After #include s 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
10
Declaring A Struct
14
Using structs
15
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 same struct, fields not linked Even variables of same struct, fields not linked
16
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;
17
Using struct s variables 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 struct s can also be declared Within the array, each entry is struct variable
18
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;
19
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;
20
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;
21
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;
22
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;
23
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;
24
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;
25
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;
26
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;
27
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;
28
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;
29
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;
30
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;
31
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];
32
Your Turn Get into your groups and try this assignment
33
For Next Lecture Mix pointers & struct s 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 struct s dynamically? Angel has Weekly Assignment #12 for tomorrow Last programming assignment Last programming assignment available now
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.