[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 17
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] <17> [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Wed Jun 6 14:16:32 PDT 2012


    cs201/17 Vectors I

      Previous 16 UML

      [ 16.html ]

      Study Pages 265-275

        This chapter introduces a very important set of ideas. A computer keeps data in memory and we often have to store varying amounts of data in this memory. For example if I handle grades then I will need to store similar data about each person in a section of a course. The generic term in C++ for this kind of storage is a container and this chapter introduces the two simplest containers.

        An array contains a fixed number of similar items. They are numbered. The program can not add a new item to the container. It can not take an item out of the container. A container with 100 items when created has 100 items in it when we are finished with it. All we can do is change the data in each item.

        A vector is like an array but it can grow at one end. You can "push" new items onto it at the back. Your can also "pop" them off the back.

        Vectors are slower but more versatile than arrays.

        6.1 Using vectors to collect data items

        A vector is a set of numbered cells of data inside the main memory of the computer. For example a vector of twelve months called month is stored and accessed like this:
        NumberContentAccessed as
        0Januarymonth[0], or month.front()
        11Decembermonth[11] or month.back()

        (Close Table)
      1. month.size() == 12
      2. month.empty() == false

        Syntax 6.1 Vector variable definition

      3. vector< type_name > variable_name;
      4. vector< type_name > variable_name ( initial_size );

      5. type_name::=any name of a type or class.
      6. variable_name::=any valid identifier.
      7. initial_size::=any int expression.

        If the initial size is missing then the vector is initially empty.

        Syntax 6.2 Vector Subscript

      8. vector_name [ integer_expression ]

      9. vector_name::=an identifier declared as a vector.
      10. integer_expression::=any expression that returns a number that will be rounded to an int.

        The program can only work if the value of the integer expression is greater than or equal to 0, and is less than the size of the vector.

        6.2 Working with Vectors

        [ salvect.cpp ]

        + Vector as a parameter to a procedure

        [ raisesalaries.cpp ]

        Operations on vector

        [ vectors.html ]

        Common Error 6.1 Bound Errors

        Skip Productivity Hint 6.1

        Quality tip 6.1 Keep subscripts simple

        Advanced Topic 6.1 Strings are vectors of characters

        Random Fact 6.1 An Early Internet Worm

      Review Questions

      R6.1, R6.2, R6.3, R6.4, R6.5, R6.8

      Demonstration with cards

      Exercise in class -- find the errors

      [ bad.vector.cpp ] [ better.vector.cpp ] [ best.vector.cpp ]

      Demo and exercises using pencil paper board

      Exercises on vectors

      1. What does [ 17ex1.cpp ] do?
      2. Can you find the bug?
      3. Can you fix the bug?
      4. What does [ 17ex2.cpp ] do?
      5. Can you find the bug?
      6. Can you fix the bug?

      Project 8 Due next time

      [ projects.html#P8 ]

      Quiz 8 next time on Vectors

      Lab 9 on classes and the UML

      [ lab09/ ]

      Next 18 More Vectors

      [ 18.html ]


  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. Class::=A description of a type of object that includes the data it knows and the functions it can execute.
  3. Function::programming=A selfcontained and named piece of program that knows how to do something.
  4. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  5. OOP::="Object-Oriented Programming", Current paradigm for programming.
  6. Semantics::=Rules determining the meaning of correct statements in a language.
  7. SP::="Structured Programming", a previous paradigm for programming.
  8. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  9. Q::software="A program I wrote to make software easier to develop",
  10. TBA::="To Be Announced", something I should do.
  11. TBD::="To Be Done", something you have to do.
  12. UML::="Unified Modeling Language", industry standard design and documentation diagrams.
  13. void::C++Keyword="Indicates a function that has no return".