CSci 152: Programming II Fall 2004 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

CSci 152: Programming II Fall 2004

Description:

Define a struct named Cars that will contain all of these members. ... the familyCar so that its type is a 'Toyota', it seats 4, it costs $18,000, it ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 19
Provided by: DerekH71
Category:

less

Transcript and Presenter's Notes

Title: CSci 152: Programming II Fall 2004


1
CSci 152 Programming IIFall 2004
  • Records in C structs and arrays

2
(No Transcript)
3
  • Arrays in Structs
  • The two things that are associated with a list
    are the values (that is, elements) and the length
    of the list.
  • In the searching and sorting functions that we
    discussed, we needed to pass these two things
    separately to the function.
  • The values and the length are both related to a
    list we can define a struct containing both the
    items.
  • We need to pass only one parameter not two.

4
  • const arraySize 1000
  • struct listType
  • int elementsarraySize //array containing
    the list
  • int listLength //length of the list

5
  • int seqSearch(const listType list, int
    searchItem)
  • int loc
  • bool found false
  • for(loc 0 loc lt list.listLength loc)
  • if(list.elementsloc searchItem)
  • found true
  • break
  • if(found)
  • return loc
  • else
  • return 1

6
  • Structs in Arrays
  • Suppose there are 50 full time employees in a
    company.
  • Print their monthly pay check and also keep track
    of how much money has been paid year to date.
  • Define an employees record.
  • struct employeeType
  • string firstName
  • string lastName
  • int personID
  • string deptID
  • double yearlySalary
  • double monthlySalary
  • double yearToDatePaid
  • double monthlyBonus

7
  • employeeType employees50

8
  • The following C code loads the data into the
    employees array.
  • We assume that initially yearToDatePaid is 0 and
    that the monthly bonus is determined each month
    based on performance.
  • ifstream infile //input stream variable assume
  • //that employee.dat file has been opened
  • for(counter 0 counter lt 50 counter)
  • infilegtgtemployeescounter.firstName
  • gtgtemployeescounter.lastName
  • gtgtemployeescounter.SSN
  • gtgtemployeescounter.deptID
  • gtgtemployeescounter.yearlySalary
  • employeescounter.monthlySalary
  • employeescounter.yearlySalary/1
    2
  • employeescounter.yearToDatePaid 0.0
  • employeescounter.monthlyBonus 0.0

9
  • double payCheck //variable to calculate the
    paycheck
  • for(counter 0 counter lt 50 counter)
  • coutltltemployeescounter.firstNameltlt" "
  • ltltemployeescounter.lastNameltlt" "
  • payCheck employeescounter.monthlySalary
  • employeescounter.monthlyBonus
  • employeescounter.yearToDatePaid
  • employeescounter.yearToDatePaid
  • payCheck
  • coutltltsetprecision(2)ltltpayCheckltltendl

10
  • Structs within a struct
  • Let us consider the following record of an
    employee
  • struct employeeType
  • string firstname
  • string middlename
  • string lastname
  • string emplID
  • string address1
  • string address2
  • string city
  • string state
  • string zip
  • string hiremonth
  • string hireday
  • string hireyear

11
  • string quitmonth
  • string quitday
  • string quityear
  • string phone
  • string cellphone
  • string fax
  • string pager
  • string email
  • string deptID
  • double salary

12
  • struct nameType
  • string firstname
  • string middlename
  • string lastname
  • struct addressType
  • string address1
  • string address2
  • string city
  • string state
  • string zip

13
  • struct dateType
  • string month
  • string day
  • string year
  • struct contactType
  • string phone
  • string cellphone
  • string fax
  • string pager
  • string email

14
  • struct employeeType
  • nameType name
  • string emplID
  • addressType address
  • dateType hiredate
  • dateType quitdate
  • contactType contact
  • string deptID
  • double salary
  • // variable declaration
  • employeeType newEmployee
  • employeeType employees100

15
  • newEmployee.salary 45678.00
  • newEmployee.name.first "Mary"
  • newEmployee.name.middle "Beth"
  • newEmployee.name.last "Simmons"
  • cingtgtnewEmployee.name.first
  • newEmployee.salary newEmployee.salary 1.05
  • for(j 0 j lt 100 j)
  • cingtgtemployeesj.name.first
  • gtgtemployeesj.name.middle
  • gtgtemployeesj.name.last

16
Quick Self-Quiz
  • A car salesperson needs to keep track of all cars
    on a used car lot. The information she needs
    includes the type, seating capacity, price, miles
    per gallon, and color of each car. Define a
    struct named Cars that will contain all of these
    members.
  • Declare a struct variable named familyCar of the
    data type Cars.
  • Declare an array of 20 Cars.
  • Initialize the familyCar so that its type is a
    Toyota, it seats 4, it costs 18,000, it get 30
    miles per gallon and it is Red in color.
  • Put the familyCar into the 5th index of your
    array declared in 3.

17
Answers Self-Quiz
  • A car salesperson needs to keep track of all cars
    on a used car lot. The information she needs
    includes the type, seating capacity, price, miles
    per gallon, and color of each car. Define a
    struct named Cars that will contain all of these
    members.
  • struct Cars
  • string type
  • int seatingCapacity
  • float price
  • int mpg
  • string color
  • Declare a struct variable named familyCar of the
    data type Cars.
  • Cars familyCar
  • Declare an array of 20 Cars.
  • Cars inventory20

18
Answers Self-Quiz
  • Initialize the familyCar so that its type is a
    Toyota, it seats 4, it costs 18,000, it get 30
    miles per gallon and it is Red in color.
  • familyCar.type Toyota
  • familyCar.seatingCapacity 4
  • familyCar.price 18000.00
  • familyCar.mpg 30
  • familyCar.color Red
  • Put the familyCar into the 5th index of your
    array declared in 3.
  • inventory5 familyCar
Write a Comment
User Comments (0)
About PowerShow.com