Methods to manipulate a collection of values under one name. PowerPoint PPT Presentation

presentation player overlay
1 / 17
About This Presentation
Transcript and Presenter's Notes

Title: Methods to manipulate a collection of values under one name.


1
Arrays and Structures
  • Methods to manipulate a collection of values
    under one name.
  • Arrays
  • Homogenous data
  • Variable quantity of data items all of the same
    type
  • Structures
  • Heterogeneous data
  • Fixed quantity of data values of different types

2
Arrays
  • Declaration
  • ltdata-typegt ltvar-namegtint-qty
  • Access
  • ltvar-namegtsubscript
  • Subscript
  • Integer
  • In the range 0 to n-1, where n is the number of
    elements

3
Structures
  • Definition
  • struct ltstruct-namegt // typically in global area
  • ltelementsgt. / composed of data types and
    element names /
  • Declaration
  • ltstruct-namegt ltvar-namegt
  • Access
  • ltvar-namegt.ltelement-expressiongt

4
Array Examples
  • int counts100 counts0 0
  • double speeds2000 speeds1999 1.23
  • string names500 cout ltlt Name ltlt
    names10

5
Structures Example
  • Structures
  • struct employee
  • string name
  • float annual_salary
  • int dept_num
  • employee boss
  • boss.name Bill
  • cout ltlt Annual Salary is ltlt boss.annual_salary
    ltlt \n

6
Nesting
  • An array may be an array of arrays or an array of
    structures.
  • A structure may have arrays as members or other
    structures as members.
  • These arrays or structures may also have arrays
    or structures as members, and so forth to any
    arbitrary depth.

7
Nesting Example
  • struct class
  • string time
  • string place
  • string students30 // structure w/ array
  • class schedule5 // array of structures
  • schedule0.place Robinson 310
  • schedule0.students0 Bill

8
Arrays and For Loops
  • For loops are typically used to process arrays.
  • Typically when using arrays, the number of
    elements that actually contain valid data is
    saved in a variable.
  • for (i0 iltNUM_ELEMENTS i)
  • countsi 0
  • for (i0 iltn i)
  • areai basei heighti

9
Sentinals
  • int SENTINEL -1
  • Sentinels may also be used
  • i 0
  • while (speedsi ! SENTINEL)
  • cout ltlt speedi ltlt \n
  • i

10
Terminology
  • Scalars a single data value
  • Vectors a one-dimensional array
  • Matrices a two-dimensional array

11
Strings
  • A string is a array of characters.
  • A string literal is delimited by double-quotation
    marks.
  • Examples
  • Computer
  • special characters in it !_at_\\\
  • one
  • 1
  • NOT strings a, 1, 1, one

12
Another method to do input
  • getline(cin, a_string, \n)
  • This will accept all input up to a newline
    character. It will store the input (NOT the
    newline) in a_string.
  • There are functions to do character-at-a-time
    input, as well as other special input functions
    (see lecture on streams and files).
  • In practice, these functions sometimes do not mix
    well with cin, with erratic results.

13
Using Strings in C
  • include ltstringgt
  • Declaring strings
  • string name
  • string account_number
  • Using strings
  • Name Bob
  • cin gtgt account_number
  • whole_name last_name , first_name //
    Concatenation
  • Name_length name.length()

14
Pointers
  • A pointer is a variable that contains the address
    of another variable, that is, a memory address.
  • Definition ltdata-typegt ltvar-namegt
  • Access ltvar-namegt for the pointer itself
    ltvar-namegt for the variable pointed to
    (de-referencing).

15
Dynamic Memory
  • Three areas of memory
  • Global defined outside any function (allocated
    once)
  • Local defined inside a function (allocated on
    the system stack)
  • Dynamic (also known as the heap)
  • Accessed through operators new and delete
  • To request a section of memory
  • ltpntr-vargt new ltdata-typegt
  • To access that memory use operators , , -gt
  • To return that memory delete ltpntr-vargt

16
Memory Examples
  • include ltiostreamgt
  • using namespace std
  • int g // global visible from here to end of
    file
  • void main()
  • float l // local visible from here to end of
    block
  • double h new double // heap allocation
  • delete h // heap de-allocation
  • int weights new intnum_weights // delete

17
Reading
  • Chapter 9
  • Chapter 11, Section 1-2
  • Chapter 13, Sections 1-2
Write a Comment
User Comments (0)
About PowerShow.com