[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 19
[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]
Fri Mar 20 18:10:58 PDT 2009

Contents


    19 Vectors and Advanced Arrays

    Previous -- Arrays

    [ 18.html ]

    Prepare

      7.5 Passing Arrays to Functions

      Tricky but necessary.

      7.6 Case Study: Class GradeBook Using an Array to Store Grades

      Study.

      7.7 Searching Arrays with Linear Search

      A very common algorithm -- study in detail. Think how you might adapt it to your own programming projects.

      7.8 Sorting Arrays with Insertion Sort

      Remind me to demonstrate this in class with my pack of magician's cards.

      7.11 Introduction to C++ Standard Library Class Template vector

      Review vectors -- remember: they are more powerful but slower to use than arrays.

    . . . . . . . . . ( end of section Prepare) <<Contents | End>>

    Deliver a question

    Exercises

    Demo Insertion and other sorts

    Demo of Test First Development of a Class

    The following class will be developed in the lab.

    [UML class diagram of Sample]

    This class is intended to store a sample data: a set of real numbers and calculate as needed various statistics: min, max, mean, standard deviation (sd). Because the data and the functions are placed in a class a program can declare and use many different Samples -- and, for example, calculate correlations between them.

    See [ lab10/ ] for a starter class and test program.

    Questions and Answers

      Which companies use sorts

      All people need there data sorted and so use sort programs. This is why we study them in Computer Science. Example: A phone company lists its subscribers in a phone book that is sorted.

      Why not use insertion sort for long arrays

      Try it with a deck of cards and see:-)

      When to use Insertion sort

      A rule of Thumb: No more than 10 items!

      Because for more than 10 items there are faster ways to sort them. Covered in CS202 and CS330.

      Why is insertion sort simple but not efficient

      Like most simple things -- it is not as good as some clever algorithms -- especially when there is lots of data. More in CS202.

      Please give more details on the STL vector template class

      First check out [ samples/stl.html#Vectors ] then check out SGI's site [ http://www.sgi.com/tech/stl/ ] then look in the standard [ ../c++std/cd2/lib-containers.html ] (tough going but complete).... and then hit the library!

      Why doesn't vector at return an rvalue

      All lvalues are coerced to rvalues when an rvalue is needed.

      What is an lvalue and an rvalue

      When a variable appears on the left of an assignment
       		left = right;
      it is an lvalue -- a place where data can be put. lvalues are places to store data. On the other side (right) the variable is used as supplying a value -- this is an rvalue -- a place or expression that gives a value. Lvalues are places and rvlaues are things in those places.

      How come linear search is bad for large arrays

      It becomes very slow with lots of data to search. Remember finding the name of the person with number 123-4567 in a normal phone book. This requires a linear search and will take hours if not days. Finding "Victor Hugo" in a normal phone book is much quicker because we don't use linear search.

      [Clever algorithms work faster on big data]

      What is a template vector

      It is a long name for a vector.

      What is a template

      A template class or function is written to have some unknown types of data and adjusts to the data you supply.

      vectors are template classes.... Hence "vector <....>"

      How often are vectors used in C++

      A lot!

      The modifyArray function on page 356 has a parameter sizeOfArray -- does it change the size of the array

      Nothing can change the ammount of space you give to an array when you declare it. NOTHING!

      In this function you can input any number as the size. You can lie! But this may not be wise.

      If the parameter has a value bigger than the real size then the function can do just about anything to your program.... upto and including shutting down the computer, by way of strange bugs and errors.

      If the parameter is equal to size of the array then all should be well.

      If the parameter is less than the real size the function will probably only use part of the array. And this may be what you wanted.

      How are arrays passed by reference

      The formal parameter gets the address of the first item in the array (item [0]). In the function the items are found by cacultaion, of course:
       		item[i]
       		address of item + (i * sizeof item).
      The function gets the size from the parameter type.

      Thus it can get to any element in the original array.

      If an array is associated with a const parameter can it ever be changed

      Not by the called function. Other parts of the program can still change it.

      When to use const for local variable

      When they are constants! Good examples are π, Euler's e, Avogadro's number, Days in Week, Months in year, and so on.

      Are arrays the only things passed by reference by default

      Yes. In C++ objects are copied and elementary data are copied in unless you use the "&" in the parameter specification.

      Can we have unsigned subscripts for vectors

      Yes... but they will be converted to signed.

      What is in the final

      [ mock2009.html ]

      Questions about the Big-O notation

      Will be answered in [ cs202/ ]

      How to use elements of an array as counters

      With arrays you need a fixed number. Suppose we have some events that happen at different days.... and to count how many in each month....
       		const int MONTHS =12;
       		int counter[MONTHS]={};
       		//some kind of loop
       			//Calculate a month...
       			counter[ month ] ++;
       		//end the loop some how
      Something like the above should work.

    Next -- Review Course Content

    [ 20.html ]

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. Function::programming=A selfcontained and named piece of program that knows how to do something.
  3. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  4. KDE::="Kommon Desktop Environment".
  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".
  13. void::C++Keyword="Indicates a function that has no return".

End