week 6 - Sheet1 Page 1 EECS-280: Discussion notes - week 6...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Sheet1 Page 1 EECS-280: Discussion notes - week 6 Topics: 1. Structures 2. Enumerators 3. Arguments to programs: argc and argv 4. I/O operators------------------------------------------------------------------------------------------------------------------- 1. Structures. We have discussed the standard datatypes: int, double, char, etc. Also, we have looked at how to group data of "same" type together: using arrays. Next we look at how to create "user-defined" data types. First let us discuss the need of user-defined datatype through an example. Lets say in a program, I want to store 3-D vectors. As we know, in 3-dimensional space, a vector is represented by its 3 components along x-, y- and z-axis respectively. So for complete representation of a vector, we need 3 variables, and maybe one more, to store the name. We can do it using standard datatypes: string vectorName = "Vector A" int xA = 3 int yA = 4 int zA = 5 where xA, yA, zA represent x, y, z component of a vector named "Vector A". A more neat way of representing the same "set" of data is by using a structure: struct vect_t { string vectorName double xComponent double yComponent double zComponent } Before proceeding any further lets look at the syntax for defining structures: struct <struct-name> { <data-type-1> <variable-name-1> <data-type-2> <variable-name-2> <data-type-3> <variable-name-3> : : <data-type-n> <variable-name-n> } This defines a structure by name <struct-name>. We can use <struct-name> as a data-type, just like we use int, double, etc. Sheet1 Page 2 In our function, we can declare an example of a 3-D vector as: vect_t A // Compare how it is similar to int A We can also use the more traditional way: struct vect_t A When we define A of type "struct vect", it creates space in memory that can hold: one string and 3 integers A :------------ vectorName |<string> |----------- xComponent |<double>|----------- yComponent |<double>|----------- zComponent |<double>|----------- Next we see how to access these values. <struct variable name>DOT<variable name inside struct> Eg: A . vectorName ^ ^ ^ struct variable name | sub-part of the structure dot A.vectorName = "Vector A" A.xComponent = 3 A.yComponent = 4 A.zComponent = 5 A simple example to see how "grouping" makes coding simple and neat: Lets write a function to calculate the magnitude of the vector. If we do not use structures: double calcMagnitude(double xComp, double yComp, double zComp) { double mag = xComp*xComp + yComp*yComp + zComp*zComp return sqrt(mag) } Call: cout << calcMagnitude(xA, yA, zA) Now, if we use structures: Sheet1...
View Full Document

This note was uploaded on 04/04/2008 for the course EECS 215 taught by Professor Phillips during the Winter '08 term at University of Michigan.

Page1 / 16

week 6 - Sheet1 Page 1 EECS-280: Discussion notes - week 6...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online