[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 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]
Wed May 29 11:38:55 PDT 2013

Contents


    cs201/19

      Previous -- Vectors

      [ 18.html ]

      Study pages 280-294

        Note

        Arrays are the oldest data structures used in computing. They are simple, fast, and (in C++/C) unsafe in naive hands. You have to be extra careful to not go outside the bounds of an array. They also have a fixed size. Nothing you can do will make any array bigger or smaller. It has a defined area of memory(RAM) and your code must stay inside it... or the results are unpredicatable, insecure, and usually bad. This is a topic in my CS202 laboratories.

        Use arrays only when you know what you are doing or are told to, or when the user complains that your vector based program is too slow.

        6.5 Arrays

        An array is a fixed piece of storage divided up into a fixed number of equal sized pieces.

        6.5.1 Defining and Using Arrays

        Syntax 6.3 Array Definitions

         		type identifier [ size ];
        Defines items name[0] through to name[size-1]. The size must be a constant.

        Examples

         		string roster_name[30];
        Creates space for 30 strings called
         		roster_name[0]
         		roster_name[1]
         		...
         		roster_name[28]
         		roster_name[29]

        Another form:

         		type identifier[] = { list_of_initial_values };
        Example
         		int days_in_month[]={31,28,31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
         		string name_of_month[]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        Used like this perhaps
         		cout << name_of_month[this_month]<<"\n";
         		...
        		for( int day = 0; day < days_in_month[this_month] ; day++) ...

        Finding the size of an array

        The size of an array is fixed. We often need to know what it is. We do not want to retype the size in many places when we change the program with a different size of array.

        (1) Wise programmers define a constant with a meaningful name that equals the size of the array or indicates the last index in the array.

        (2) But suppose we are in a program and do not have such a declaration? Here is an trick to calculate the declared size of an array:

         			sizeof(array_name)/sizeof(array_name[0])
        The calculation is done by the compiler before the program starts running! This can not be used to find the size of an array parameter.

        6.5.2 Array Parameters

        Here [ salarray.cpp ] is the book's example use of an array.

        6.5.3 Character Arrays

        These are very old fashioned. It is easy to go out of bounds with these. Only use if you are forced to by an instructor, boss, or old legacy code.

        Here is the classic [ append.cpp ] from the book.

        There is an old C library of functions called <cstring>. It takes great care to use it.

        Example of arrays and enums -- cards

        [ cards.cpp ]

        6.5.4 Two-Dimensional Arrays

        Have you heard of matrices?

        Syntax 6.4 Two-dimensional Array Definition

         		type identifier [rows] [cols];
         		char tictactoe [3][3];

        Table
        -012
        0Xoo
        1XoX
        2oXX

        (Close Table)

        Quality Tip 6.3 Name the Array Size and Capacity consistently

        Use named constants! Distinguish between the space available -- CAPACITY and the space in use -- size. Always name[size-1] is the last item in the array.

        Common Error 6.2 Omitting the column size of a 2D Array Parameter

        Needed so the function can figure out how far down the array to go to skip a row or two...

        Random Fact 6.2 International Alphabets

        Unicode

        Chapter Summary

        Review this!

      Review Questions

      Hand in either a question on vectors/arrays that you want answered, or a correction to my CS201 website.

      Exercises -- to be announced

      [ a20.cpp ] [ b20.cpp ]

      Exercise -- vectors vs arrays

      1. Given an array declaration -- rewrite using a vector.
      2. Given a vector declaration -- can you rewrite as an array declaration?

      Project 9 Due Next time

      [ projects.html#P9 ] (no resubmit of P9 is allowed). Note: P9 will to star in a question in the final. Not doing this project can reduce your score on the final.

      No Quiz next time

      Lab 10 on Vectors

      [ lab10/ ]

      Next 20 Review

      [ 20.html ]

    Abbreviations

  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 value".

( End of document ) <<Contents | Top