[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci202] / 03
[Text Version] [Syllabus] [Schedule] [Glossary] [Resources] [Grading] [Contact] [Question] [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]
Mon Apr 13 10:31:08 PDT 2009

Contents


    CSci202 Computer Science II, Session 03 -- Arrays and Vectors

      Previous

      [ 02.html ]

      Preparation

      Study this page and chapter 7.
      1. 7 Arrays and Vectors 333
      2. 7.1 Introduction 334
      3. 7.2 Arrays 335
      4. 7.3 Declaring Arrays 337
      5. 7.4 Examples Using Arrays 337
      6. 7.5 Passing Arrays to Functions 354
      7. 7.6 Case Study: Class GradeBook Using an Array to Store Grades 358
      8. 7.7 Searching Arrays with Linear Search 365
      9. 7.8 Sorting Arrays with Insertion Sort 366
      10. 7.9 Multidimensional Arrays 369
      11. 7.10 Case Study: Class GradeBook Using a Two-Dimensional Array 372
      12. 7.11 Introduction to C++ Standard Library Class Template vector 379
      13. Vectors [ vectors.html ] are much safer and more versatile than arrays. Use them whenever possible. Only use arrays when you know the maximum size of the array before you start coding.

      Assigned Work Due

      Submit a Question...

      Quick Exercise

      Write a C++ program to read in 7 characters into an array and then print them out again. You can use the book if you wish. Style: the simplest thing that works.

      [ 03.cpp ]

      Exercise on RAM

      Either
      Case
        Draw a picture of the RAM in a Computer

      (End of Case)
      Case
        Write a paragraph describing the RAM in a Computer

      (Close Case )

      Chapter 7 pages 269 -- Multidimensional arrays

      Could you please give a simple example of a multidimensional array?

      A Tic-Tac-Toe Board.

      Any mathemtaical matrix.

      The computers screen is a large, 2-dimensional array of pixels.

      Ninety-nine bottles of beer on 11 shelves -- 9 bottles a shelf.... (a poster I have at home).

      A keyboard.

      The apartments in a high-rise building.

      Any more?

      Chapter 7 pages 334-335 -- Arrays

      Do arrays make coding a program any easier?

      No.... it just makes soem programs possible that are impossible without them.

      Nothing makes programming easy:-(

      Thinking ahead, being very careful, knowing the language, and knowing computer science can mke it easier.

      We use arrays because a lot of data is in arrays. It is rare to find an application with a single item... it s nearly always about a number of similar ones.

      Chapter 7.5 pages 356 -- passing arrays to functions

      How bad can logic errors mess up your program?

      Totally. Worst case scenario: dead operating system.... or worse a hacker running your computer remotely.

      Chapter 7 pages 369 -- multidimensional array

      Is there a certain sequence numbers have to be input in a multidimensional array to have the highest value in first?

      No. All arrays start with subscript 0. The values in the arrays are what you put there. If you need the larger numbers first then you must do it yourself -- either in the initial values, or (more common) by sorting the data.

      Chapter 7 pages 337-339 -- Using Arrays

      In Figure 7.4 Line 15, where it cout's an Element what is the use of the "13" in setw(13) is it supposed to be the specified with?

      The setw is a input-output manipulator that sets the width of the next input or output. In this case setw(13) it means that out put data is padded or trncated to fit in 13 characters on the display.

      Chapter 7 pages 334-335 -- Subscript

      Do other languages use c[i] for c subscript of i?

      Yes. Nearly al of them. It started with Algol 60, in 1960.

      Chapter Chapter 7 pages 335-338 -- Arrays

      Can you use any other operators besides brackets to enclose the subscript of an array ?

      No.

      Chapter 7 pages 345 -- Arrays

      Could you explain lines 25 & 26 of Fig. 7.10, why does the author use "int roll"

      The variable roll is a counting variable. It's purpose is to count the number of times the die has been rolled. They have been careful to use a good name -- "roll" -- so we can figure this out. A bad programmer might have written

       for( int foo=1; foo < 6000000; foo++)
      which would make the purpose less clear.

      Chapter 7 pages 337 -- rewrite array

      Can this be rewritten in a different way
       	type arrayName[arraySize];

      I don't think so. This is that standard way to declare an array for the last 30 years...

      Chapter 7 pages 369-371 -- Multidimensional Arrays

      Can you elaborate on multidimensional arrays? What exactly are the "for" statements used for in page 371?

      For loops are used to scan acrross arrays so that the statements inside the loops can do something to each element in turn.

      The lines 30-37 have exactly this form (on page 370). If we have time we should try to work out what these nested loops do, step by step.

      Handling a 2-dimensional array often has code like this

        	For each row in turn
      		{
       			handle start of row;
       			For each item in row ....
       			handle end of row;
       		}
      Notice the loop inside the loop.

      Chapter 7 pages 379 - 384 -- Arrays and Vectors

      Simply put how do you know when you should use a Vector instead of an array and vice versa?

      As a rule use vectors always -- they are safer and have ferwer nasty surprises.

      Chapter 7 pages 334-392 -- Arrays and Vectors

      When is it beneficial to use vectors rather than arrays or vice versa?

      Normally.

      Chapter 7 pages 334-380 -- Arrays

      Are arrays ever useful?

      When you know the number of items in that you need. When you need to make the program faster. When you want to initialize a fixed number of items.

      Chapter 7 pages 379 -- vectors vs. arrays

      What are the performance differences between vectors and arrays?I know they would be neglible on small things but what about something computer intensive(3-d games,encoding,rendering)?

      Arrays are used for High-Performance Computing -- especially rendering, games, etc. Special chips can work in parallel on arrays. The major cost is that you have to be a lot more careful of the programming with arrays.

      Chapter 7 pages 354-358 -- Passing Arrays to Functions

      Can you explain how passing array arguments to a function works?

      An array is a block of storage. When an array name is put in a function header then the address of the block of storage is given to the function. Inside the function operations on the array are actually done to the original block of storage.

      Chapter 7.7 pages 365-366 -- Arrays/Linear Search

      can you elaborate on how to use "Linear Search" using vectors and explain more in depth on what "Linear Search" does and how to implement it?

      Linear search is used any time we have a pretty random set of items of data and need to find one of them. Exactly the same code is used for arrays, vectors, files, etc.

    1. to_find_an_item::algorithm=following, (
      1. For each item in turn (
        1. if it matches the key stop
        )
      )

      Chapter 7 pages 379-381 -- Vectors

      I understand that arrays are able to process faster than vectors, but is there a different way to search through vectors to make them just as fast as arrays?

      No.

      Chapter 7 pages 370 -- Multidimensional Arrays

      In a function header, why isn't the first subscript required, but all following subscripts required.

      The purpose of the first subscript in a declaration is to tell the compiler how much space to allocate to the array. The number is not used for calculations or checking subscripts inC/C++.

      In a function header arrays are passed by reference and so no data is allocated. The address of the first item ([0]) is passed of an existing block of RAM. Thus the function header does not need to be given the size of the storage. It is therefore omitted.

      The rest of the numbers are used to calculate the position of elements in RAM and so are essential.

      Chapter number7 pages 379-383 -- vectors

      how to declare vectors?

      Like this

       		vector < T > v;
      where the vector v is empty but can hold items of type T. or
       		vector < T > v (s);
      where the vector named v will have space for s elements of type T.

      Vectors -- declare size first or grow

      In general, is it better to declare the size of a vector initially or is it better to just use pushback or popback?

      The is no general rule. Sometimes the program has no items to start with and so grows the vector by adding items as needed. Sometimes we know the first 5 items (say) and set the size in the declaration and use subscripts to load the five values. This is rare in my experience. It s also more confusing but may be a little bit faster. It is confusing because you switch from using existing elements to pushing and popping. You have to make the transition at the write time. It is faster because the compiler can pre-allocate the block of space.

      Chapter 7 pages 380-383? -- v.pop_back()

      Is it possible to "pop" multiple vector elements using v.pop_back(4), or must I use a loop?

      You have to do it with a loop. This

       		vector::pop_back( int n);
      is not defined in <vector>.

      Chapter 8 pages 414 -- Passing Arguments to functions by reference with pointers

      In the book it claims that there are three ways in c++ to pass arguments to a function 1. pass by reference with reference arguments, pass by reference with pointer arguments, as well as pass by value...is their any other more complicated way to pass an argument to a function?

      The more complex ways of passing data to and from functions are really combinations of the above.

      Lab -- Drawing UML Diagrams

      [ lab02.html ]

      A Quick Introduction to the UML

      [ uml0.html ]

      Next Class

      [ 04.html ]

      Project 1 due start of Next class

    . . . . . . . . . ( end of section CSci202 Computer Science II, Session 03 -- Arrays and Vectors) <<Contents | End>>

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. class::="A description of a set of similar objects that have similar data plus the functions needed to manipulate the data".
  3. Data_Structure::=A small data base.
  4. Function::programming=A selfcontained and named piece of program that knows how to do something.
  5. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  6. KDE::="Kommon Desktop Environment".
  7. object::="A little bit of knowledge -- some data and some know how", and instance of a class".
  8. OOP::="Object-Oriented Programming", Current paradigm for programming.
  9. Semantics::=Rules determining the meaning of correct statements in a language.
  10. SP::="Structured Programming", a previous paradigm for programming.
  11. STL::="The standard C++ library of classes and functions" -- also called the "Standard Template Library" because many of the classes and functions will work with any kind of data.
  12. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  13. Q::software="A program I wrote to make software easier to develop",
  14. TBA::="To Be Announced", something I should do.
  15. TBD::="To Be Done", something you have to do.
  16. UML::="Unified Modeling Language".
  17. void::C++Keyword="Indicates a function that has no return".

End