Extra Lecture: Project 2 Structs - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Extra Lecture: Project 2 Structs

Description:

Part 1 (8 Points) This part should be completed after Lecture9.ppt ... The output data is just displayed to the screen, ... Trenton Reba 27. Masengill Fortuna 24 ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 18
Provided by: solo50
Category:

less

Transcript and Presenter's Notes

Title: Extra Lecture: Project 2 Structs


1
Extra LectureProject 2 Structs
2
Did you do Project 2 Part 1 yet?
  • Part 1 (8 Points) This part should be completed
    after Lecture9.ppt
  • Start by writing a program, Proj2v1.cpp that
    reads and calculates drug doses for all the
    patients in file PatientData.dat. The output data
    is just displayed to the screen, using proper
    output formatting for evenly spaced, justified
    columns. (Note, strings need to be converted to
    c_strings for the setw( ) format command to work
    on them in Bloodshed, for example,
    coutltltsetw(15)ltltname.c_str( ) )

3
Part1 Read File, calc/display info
  • Sample output (first 2 lines out of 25)
  • Trenton Reba 27
  • Masengill Fortuna 24
  • You'll need to use a function to calculate and
    return the drug dose based on a patient's data.
    (Note you are only calculating one drug dose per
    patient). This function, CalcDose( ) will use a
    basic dose rate for adults of 0.2 mg per pound of
    body weight for patients over 120 pounds.
    Patients under 120 pounds will use a dose rate of
    1200.2 or 24 mg. In addition, girls aged 12 and
    under will only be given 25 of the adult dose
    calculated above, while boys 16 and under will be
    given 75 of the adult dose. The final drug dose
    will be rounded using the Round function from
    Lecture6.ppt.

4
Review of Data Types
  • float floating point numbers
  • int integer numbers
  • char single ASCII character
  • string a class representing a sequence of
    characters
  • ifstream, ofstream classes representing pathways
    to files

5
These are great but somewhat limited
  • How would you represent an automobile in a
    computer?
  • What about a textbook?
  • An employee entry in a database?
  • And so on
  • We need a type that goes beyond simple one type
    data.
  • Enterthe structure!!

6
Structured Data Types
  • A structured data type is one in which each value
    is a collection of components and whose
    organization is characterized by the method used
    to access individual components (i.e. the dot .
    operator)
  • See p 296 section 12.9 (inserted after classes)

7
Structs
  • A record or a struct in C, is a structured data
    type with a fixed number of components that are
    accessed by name. The components may be
    heterogeneous (mixed types).
  • A field or component is a piece of the record.

8
Syntax of struct definition
  • struct TypeName
  • DataType MemberName
  • DataType MemberName

9
Example StudentRec
  • struct StudentRec
  • string firstName
  • string lastName
  • float GPA
  • int ID

This would go above main()
10
Creating StudentRec variables
  • Once you declare a struct StudentRec, it becomes
    a new data type you can use
  • StudentRec Bill, Tamara

Bill
Tamara
firstName
ID
ID
firstName
GPA
lastName
GPA
lastName
11
Accessing a field
  • To assign a value to one of the fields, you use
    the member selector operator .
  • StudentRec Bill // create a variable of type
    StudentRec
  • Bill.firstName William
  • Bill.lastName Borroughs
  • Bill.ID333
  • Bill.GPA3.87
  • coutltltBill.ID
  • cingtgtBill.lastName

Bill
ID
firstName
William
333
GPA
lastName
3.87
Borroughs
12
Your turn
  • Declare a student record for a variable x
  • Make x store the data for Kalina Noori, whos ID
    is 456 and GPA is 3.97
  • Display the first and last names of x on the same
    line
  • Input the ID and GPA of x

x
ID
firstName
GPA
lastName
13
Aggregate Operations (performed on a struct as a
whole)
  • Aggregate Operation
  • I/O
  • Assignment
  • Arithmetic
  • Comparison
  • Passing Arguments
  • Return from a function
  • Allowed on Structs?
  • No
  • Yes
  • No
  • No
  • Yes, value and reference
  • Yes

Operations labelled no can still be performed
on a field by field basis (see next slide)
14
Example How to do the operationson StudentRec x,
y
  • Operation
  • I/O
  • Assignment
  • Arithmetic
  • Comparison
  • Passing to Fn
  • Function return
  • Correct Example
  • cingtgtx.firstNamegtgtx.lastName
  • gtgtx.IDgtgtx.GPA (Wrong cingtgtx)
  • yx (copies all fields) or x.IDy.ID
  • y.GPAx.GPAy.GPA (Wrong xxy)
  • if (x.ID lt y.ID) (Wrong if (xlty))
  • Display(x) OR y.GPAsqrt(x.GPA)
  • return x OR return x.GPA

15
Initializing Structs
  • When a struct is created you can initialize it
    with a list of values (initialization
    list)StudentRec sIsa, Corwin,3.5, 555
  • The sequence must match the order of declaration
    in the
  • struct (see slide 7)
  • NO StudentRec sIsa,Corwin,555,3.5?OOPS
  • You can only do this on declaring
  • StudentRec t
  • NO tBert,Lance,3.5, 555 ?ERROR

16
Data Abstraction
  • Data abstraction is the separation of a datas
    logical properties from its implementation, for
    exampleLogical Properties of a real number
  • addition, multiplication, positive, negative
  • Implementation of a real number
  • Hardware, limited bits, mantissa, exponent
  • how these can be used to provide adding,
    multiplication
  • An Abstract Data Type is a data type whose
    properties are specified independently of any
    particular implementation. How we represent real
    things on a computer
  • This leads us to Classes (next week!)

17
TRY IT NOW!!
  • Work on Project 2
  • Part 1 doesnt need structs at all
  • Part 2 needs to use a struct for Patient data
  • If you need help understanding structs, try doing
    exercises in structs.doc and structs.cpp
Write a Comment
User Comments (0)
About PowerShow.com