[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 15
[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]
Tue Mar 3 15:16:57 PST 2009

Contents


    15 Case studies and Rules

    Previous -- Functions

    [ 14.html ]

    Prepare

      6.7 Case Study: Random Number Generation

      6.8 Case Study: Game of Chance; Introducing enum

      6.9 Storage Classes

      6.10 Scope Rules

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

    Deliver a question

    Questions and Answers

      How do you mark the beginning and end of classes

       		class Name
       		{
       			...inside the class
       		}; // match brace to start of class.

      Is there a way of keeping classes together in compilation

      You can put them all in the same file... or you can write special commands for the compiler to assemble the files. You can automate this process using a "Makefile" and the "make" command in UNIX. Something similar is possible for IDEs and MS Visual Studio...

      When to use a function or a class

      Use a class to model real world objects. Use a function when you have a simple task with all its data provided at that time and no memory. Use a function is class if there is any data that must be stored between calls.

      When to use function overloading

      When you have two similar calculations with different types of data. For example adding to ints, adding to doubles, and adding two string.... are all shown with "+".

      The compiler will match the right (usually) function definition with each call for you.

      Why function overloading

      Few different names to worry about.

      How does a program use an identifiers scope

      Inside the scope the variable has a meaning. The compiler keeps a list of variable declarations as it reads the program and looks up each variable when it used in the list.

      What is the difference between global and static local

      The global has a scope equal to the whole file and any other file that decalres an "extern" variable of the same name. The static variable is private to the function/block in which it is declared. Both have a life time equal to the program. They are just accessible in different parts of the program.

      What is the difference between static global and global

      A global variable that is declared static has a scope equal to the file in which is declared. It can not be accessed form another file of data.

      Is there a limited time for information to be stored in an identifier

      The data is stored for the while life time of the variable. Global and static variables keep their values and meaning for the whole life of the program. Local variables and parameters in functions keep their values until the end of the function or block in which they are declared. Data members (variables in objects/classes) keep their values until their object is destroyed -- and the object follows the rules of its declaration.

      Do static local variable keep their value when the function returns

      Yes.

      When to use enum

      When you need a variable to have one of a fix number of values, and you have a ready made set of names for them. Examples might include
      1. Colors of the rainbow
      2. Days of the week
      3. Months
      4. Gears in an automobile

      Notice the two requirements: a fixed set + known names. Otherwise "enum" may act as a problem not a solution.

      In arrays and vectors -- what if I start my indexes at 1 not 0

      It works, you waste a little storage. I've done this.

      Can vectors store strings etc

      Yes. You can have a vector of any kind of object: int, double, char, string, even vector!

      What do lables do to a program

      Case labels are ok. Other labels are confusing.

      What is Randomization

      Any process that gives unpredictable results -- result that are unlikely to be correlated with other things that are going on.

      What are pseudo-random numbers

      These are numbers that appear to be chosen at random. Their function is to simulate real random processes like throwing dice, tossing coins, and shuffling cards. Very useful in games, statistics, and so-called Monte Carlo numerical methods.

      Whats with srand and rand!

      You always have two functions when generating random numbers. One gives you the next number. In C++ this is called "rand" and it has no parameters. The other function sets the random number generator to a particular internal number. In C++ this is "srand". It must have an parameter to generate the starting point. In C++ this is always done like this
       		srand(time(0));
      at the start of any program that needs random numbers.

      Unless you want to spend time studying random number generators... its best to treat them as magic -- learn the spells and recite them correctly (See the Harry Potter series).

      What does an enumeration do in a program

      It saves you remembering what a set of integers mean by giving them names.

      Can enumerations skip numbers

      Yes:
       		enum Example(START=17; MEANING=42};

      Can you increment by other than 1 an enumeration variable

      Yes.

      If there is a gap in the enumeration what does ++ do

      It gives a value that you did not list in your constants!
    1. . What is scaling Scaling is multiplying a set of related numbers by a constant to change their range of values while preserving the relative sizes. Like zooming in and out.

      What is unsigned for, why not use int

      Unsigned is used for compilers, operating systems, and working close to the hardware. Other than that you might as well use signed numbers.

      How much memory is assigned to registers.

      Registers are inside the CPU. Memory is outside the CPU. So: zero. But the size of the registers in the CPU depends on the chip.

    Exercises

      In the following do not guess..... THINK it through, step by step.
    1. What does this program do: [ fun.cpp ]
    2. What does this program do: [ funfun.cpp ]
    3. What does this program do: [ fun14.cpp ]

    Next -- Advanced Functions

    [ 16.html ] [ lab08/ ]

    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