[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 14
[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]
Podcasts: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Fri Feb 22 13:30:52 PST 2008

Contents


    pages 181-196 Advanced Data

      Reading

        Introduction

        The most useful part of the reading is the material on type conversions in section 5.7 and on multidimensional array in section 5.9. Using enumerated types makes you programs more readable, however, so you should look at section 5.8 -- it is not that difficult. I'm hoping to skimp on the rest of the reading.

        5.5 Complicated declarations and typedef

        I don't expect you to use this section in labs, quizzes, classes, or the final but typedef may be used later in the part of the book we are covering. It is definitely part of CSCI202.

        Purpose: to give your own name to a new data type.

        Syntax: Just like a variable declaration.

        Effect the name becomes the short hand name for a data type and can be used to declare variables and constants later in your program

        I've must used it for horrible things like arrays of function pointers that you can forget about for now.

        5.6 The type void and void* -- voidstar

        This is the weirdest data type in C++/C. It indicates the absence of data. It is used in function definitions:
         	void name(parameters)....
         	Type name(void)....

        The text also mentions something I don't want to talk about! C++ and C allows you to have pointers that indicate a single place in memory without saying what type of data is to be found there. They are declared using 'void*' and there is not much you can do with them.

        Again let's skip this topic.

        5.7 Type conversions

          5.7.1 Automatic type conversion

          Worth putting a PostIt note on this page!

          5.7.2 Explicit Type conversions and casts

           		(typename) expression
           		typename ( expression )

        5.8 Enumeration types

        These are a useful tool for making your programs more readable. They also tend to make your programs less buggy.

        The enum statement declares a new data type. It can have any values you like!

        Syntax of an enumeration declaration:

         		enum name { list_of_values };
        Notice the squiggly braces. Parentheses and square brackets do not work.

        Output and input of enumerated values

        However, the compiler codes the values as int's anyway. This means that if you output an enumerated value you get a number. You have to convert it to a string and a clever way to do this is on page 188 by using an array of char texts. You can use an array of strings as well.

        Page 189 shows a clever solution to the problem of inputting enumerated values. Again the compiler converts values to integers. To be able to input the values you must write code like get_enum that looks up a input string in the table of values.

        5.8 Tables

          Introduction

          A very powerful idea needed in lots of projects

          5.9.1 Multidimensional arrays

          Notice the syntax
           		array[rows][columns]
          Do not write array[row, col]. It compiles, runs and gives you array[col]!.

          5.9.2 Tables with the help of vectors and matrices

        . . . . . . . . . ( end of section 5.8 Tables) <<Contents | End>>

        5.10 Pairs

        I think we can skip this data type! Used in CSCI202.

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

      Glossary

      1. cast::="to force a value in one data type to an equivalent value in a different data type".
      2. char::datatype=8 bit.
      3. enumeration::="To list a set of values and have them numbered...", a C/C++ data type.
      4. enum::lexeme, "Used to define a new type of int with constant names for values".
         	enum Glass={empty, half_empty, half_full, full};

      Questions

        What are declarations

        A declaration reserves some storage and gives it a name. It may also put data in the storage.

        Please explain the void parameter and its use in more detail

        If you declare a function like this
         .... fred (void )...
        then the compiler will object if you ever write any call of fred with data in it:
         	...fred (42);
        is now illegal.

        Similarly, if a function is declared to return "void" then you may not use it in an expression to get a value.

        How are pointers and arrays useful in computer programming

        First: arrays are a very common type of data. See below for examples, and the book.

        Second pointers provide a simple and fast way to access items in the array. In this class I refered back to [ 13.html#How do pointers and text strings work together ] in the previous class. Here is an example with an array of ints being added up by using a pointer

         int sum(int a[], const int N)
         {
         	int sum =0;
         	for( int* p = a; p < a+N; p++)
         		sum = sum + *p;
         	return sum;
         }

        What does the command char do

        The keyword char is the name of a predefined type -- the computers standard character type -- these days coded as 8 bit ASCII. Because "char" is the name of a data type you use it primarily to declare variables and constants:
         	char input;
         	const char QUOTES = '\"';
        These statements reserve a piece of primary memory and give it a temporary name. It is just big enough to contain one byte (8 bits) of information.

        You can also use "char" to declare arrays of characters and to specify parameters and returned types from functions:

         	char example ( char para ) { return para + 1; }

        What is typedef

        The typedef key word (notice the spelling) is used to give a new name to a complex data type. Giving things well chosen names makes them easier to understand.

        You use "typedef" by writing it in front of any normal looking variable declaration:

         	typedef vector <int> intvect;
        and it totally changes the meaning. In stead of creating a new variable (storage, address, ...) it defines a new data type. You can then use it to declare variables
         	intvect example_int_vector;

        Typedefs can make some declarations a lot easier to understand However I don't intend to require them in any project, use them in example, or test your knowledge of them in any quiz or final. .What is the function of enumertaion types They make programs more readable. See next Question.

        How does an enum reduce bugs

        Enumerated data makes you code more readable. It makes it harder to do stupid things like forgetting how you coded a particular value. It makes it easier to see a mistake. For example if you saw:
         		light=0;
         ...
         		light=1;
         ...
         		light=2;
        you can only guess at what is in my mind. But if first create an enumerated type with good names you know precisely what I'm writing about:
         	enum StopLight {red,yellow,green};
         ...
         	   StopLight light;
         ...
         		light=red;
         ...
         		light=yellow;
         ...
         		light=green;

        And if you find me doing somethin like

         			light = light * green;
        you can be pretty sure I've made a mistake!

        Here more examples:

         enum TicTacToeSymbol {empty, oh, cross};
         enum LifeState {dead, alive};
         enum Month{jan, feb,mar,apr,may,jun,jul,aug,sep,nov,dec};
         enum DayInWeek { mon, tue, wed,thu, fri, sat, sun };

        When using enukeration types does the compiler assign a values to the different words

        YES!

        The compiler removes the names and replaces them by numbers so you don't have to learn anything new about how enumerated data works -- it is an int in disguise.

        The normal numbers are: 0,1,2,3,....

        You can specify any int you like for an enumerated value.

        Can you explain the example on Page 188

        This shows you a common technique that is used when you have an enumerated type and want to input and output its values. The technique is to declare an array of strings that show the outside view of the data -- inside the computer enumerated values are just numbers. Then you use the array to output them.

        Example.... I declare TicTacToe to have three values: empty, oh, and cross. (see above). This does not help me output the values. So I also declare

         	const string tttout[3]={" ", "O", "X"};
        So no I can output a particular place on the board like this:
         		cout << tttout[empty];

        More below...

        What is a multidimensional array

        A one dimensional array is a row of items numbered [0], [1], [2], ..., [SIZE-1]. Examples include an array of characters "abc" and this array of ints:
         	const int DAYS_IN_MONTH[12]={31,28,31,30,31,30,31,31,30,31,30,31}; // not leap year
        You access an element like this DAYS_IN_MONTH[jan].

        Note: I used an enumerated value in the above example...

        Multidimensional arrays have two or more subscripts. They have two or more dimensions. They are arrays of arrays.

        Examples of two-dimensional arrays.

         		TicTacToe board[3][3]; // 9 cells in a square array
         		LifeState plane[100][100]; // for conway's game of life
        You access elements in these arrays like this: board[0],[1], plane[24][42], etc.

        Once you have got a TicTacToe board and a array tttout defining how to output the symbols you display a board like this

         for(int row=0; row < 3 ; row++)
         {
         	for(int col=0; col < 3; col++)
         	{
         		cout << " " << tttout[ board[row][col] ] ;
         		if(col < 2)
         			cout << " | ";
         	}
         	cout << endl;
         	if( row < 2 )
         		cout << "-----" << endl;
         }
        (This code needs combining with previous snippets and testing...)

        Another example is a single work sheet for a spreadsheet! In fact, most of the tables that users want see are two dimensional tables:

         		double sales[WEEKS][DAYS_IN_WEEK];
        You access the cells like this sales[8][fri]

        Three diemnsional arrays are also common. A modern spreadsheet has many worksheets and each of those is a two diemnsional array of formulas. Similarly, many scientific models of objects -- a body, a bridge, a block of uranium, etc. are modeled by a large three-dimensional array. The cells in the array can store the ammount of bine, the stress, the radiation, etc. in the cell... and the computer can calculat what happens. This kind of volume modelling and computation is expensive and common!

         	double bone[10][10][10];
        declares an array of 1,000 cells organized like a cube. You access a particular cell like this bone[row][col][slice].


          Be happy you have multidimensional arrays. When I started out I didn't have them in my programming languages. I will not forget in hurry a friend who need a 5><5 square and declared 5 different arrays:
           		double a[5],b[5],c[5],d[5],e[5];
          And the pages and pages of special code for each row of the array... I went in a different direction. I declared
           		double a[25];
          and had to mentally transcribe a[r,c] into
           			a[ 5 * r + c ]
          every where...

        If we want to combine 2 different arrays do the rules of matrix addition and mulitplication apply

        NO. Sadly, no. .... You can not add and multiply arrays in C++ unless you have included definitions of the operations. You'd have to hunt for these on the Internet.

        Don't get me on the topic of prgramming matrix operations. I have had to do it in two different jobs and did four years of college work on them....

      Next -- Quiz 7 + Lab 8 + Project 6

      [ projects.html#P6 ]

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. KDE::="Kommon Desktop Environment".
  3. OOP::="Object-Oriented Programming", Current paradigm for programming.
  4. SP::="Structured Programming", Previous paradigm for programming.
  5. TBA::="To Be Announced", something I should do.
  6. TBD::="To Be Done", something you have to do.
  7. UML::="Unified Modeling Language", [ 15.html ] (class notes on the UML and OOP).

End