[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci202] / 02
[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]
Wed Apr 8 14:01:07 PDT 2009

Contents


    CSci202 Computer Science II, Session 02, Review

      Previous

      [ 01.html ]

      Preparation

      Study the page below and chapters 1,2,3, 4,5,6 and write down the questions, doubts, and surprises that you have on a piece of paper.

      Reading

      1. 1 Introduction to Computers, the Internet and theWorldWideWeb
      2. 2 Introduction to C++ Programming
      3. 3 Introduction to Classes and Objects
      4. 4 Control Statements: Part 1
      5. 5 Control Statements: Part 2
      6. 6 Functions and an Introduction to Recursion
        1. 6.19 Recursion 294
        2. Example [ 02rec.cpp ] [ 20rec.cpp ]
        3. 6.20 Example Using Recursion: Fibonacci Series 298
        4. 6.21 Recursion vs. Iteration 301

        Assigned Work Due

        Submit one question before 10am by using the web form provided above to earn credit. This will tell me what topics to cover in the class.

        Exercise

        As a member of the CSci Honors committee I have to calculate a special GPA of all people who apply for honors each qusrter. This is a GPA of just the computer science classes -- and the main computer on campus runs the Student Information System (SIS+) and doesn't do this for me.

        So I need to store upto 30 floating point numbers, add them up, find the maximum and minimum, and print out these three statistics.

        Write the Simplest Possible Program that could do this...

        Chapter 3 pages 86 -- Void Functions

        I understand that void functions do not return any data, but why is it that some void functions have return (identifier) in them?

        The "return" statement has to purposes. (1) it gets you out of the function. (2) it can also give the caller a piece of data.

        A "void function" may not give the caller any data. I think it will be thrown away.

        Some void functions have no "return" because there is an implicit, invisible, "return" placed at the end of the function.

        Chapter 3 pages 88-90 -- CSCI 202

        What are arguments and parameters?

        They are the same things. Mathematicians talk about parameters and C program talked about arguments. BUT they both ment the data that is give to a function to calculate with. For example

         		double sin( double x)
        (in <cmath>) is a function with a single double parameter called x.

        It is called like this

         		whatever = a * sin (omega) + b*sin(gamma);
        and in each call it is given the actual value that to initialize x.

        Chapter 3 pages 88-90 -- Functions

        How do you define a function's peramiters?

        (1) Good spelling is important for wizards.

        (2) In the header of the function:

         	ReturnedType name( parameters )....

        Chapter 3 pages 89-93 -- Classes

        Can you give a better explanation on how classes work and what they're used for?

        Classes pay off when organizing a complex project -- it splits a thousand chaotic lines of code into half-a-dozen well organized files with 100 lines of code in each...

        A class describes a rules for a collection of similar objects. An object contains data value -- named data values, called attributes. These are declared in a class. So, if we needed to talk about Points in a Cartesian grid we would expect two coordinates called x and y. A class that looks like this:

         	class Point {
         		double x;
         		double y;
         	...
         	};
        Describes objects with 2 attributes called x and y.

        A class describes data in an object and the functions that can apply to these objects.

        A class also defines rules for constructing new objects. These are special functions that have the same name as the class itself:

         	class Point {
         		double x;
         		double y;
         	  public:
         		Point(){ x=y=0; }
         		Point(double x0, double y0){ x=x0; y=y0; }
         	  ...
         	};

        You can add functions that do things to Points.... for example to move one. And you can add functions to calculate properties of a Point:

         	  public:
         		Point(){ x=y=0; }
         		Point(double x0, double y0){ x=x0; y=y0; }
         		void move(double dx, double dy){ x+= dx; y+=dy; }
         		double length(){ return x*x + y*y; }
         	  ...
         	};

        Object-oriented design is the diffcult task of discovering the best set of classes to fit the user's needs.

        Here are my [ objects.html ] rough notes on classes.

        Chapter 3 pages 91 - 95 -- Class member functions

        How do you know when you should and shouldn't use the get and set functions? Are these functions necessary in a Class when you can directly just call the function. (i.e, data()).

        Access to the private data in an object must me on a need to know basis.

        Only add a "get" function when another type of object needs the value.

        Only add a "set" function to a class when a different class needs to change the value.

        In a complex program it heps to draw interaction diagrams -- pages 386 thru to 391 before you write the code. The arrows tell you the minimum set of funtions in each class needed to meet the user's needs.

        Beware the temptation to add "get" and "set" for every variable "Just In Case" -- You Ain't Gonna Need It.

        Chapter 3 pages 99-101 -- classes/constructors

        what is the difference between a constructor and a Default Constructor?

        The Default constructor needs to data and provides suitable default values for the private data in the newly constructed object. It is the constructor used for declarations like this

        		Whatever foo;
        However, non-default constructors have arguments and are used for declarations like this:
        		Whatever foo(bar, mumble, 42, 3.14159, "ying tong iddle I po");

        Chapter 4 pages 139 -- BOOL or bool

        What is BOOL and when exactly is it used?

        The correct spell is

         		bool
        and it is the name of the "Boolean data type" invented by George Boole to model "The Laws of Thought". Boolean data have no more than 2 values
         		false
         		true

        They have a dozen operators:

         		and	&&
         		or 	||
         		not	!
        But, the "English" form means the same thing as the older "C" symbolic form.

        All tests and conditions become a Boolean value before the if/while/for... gets to them.

        You will use them -- perhaps without knowing it in all interesting programs.

        You can also declare bool variables:

         		bool tooBig;
        and store values in them for later use:
         		tooBig = (x >1000000 or y >100000);

        Chapter 4 pages 173 -- boolean

        what is boolean attribute?

        A boolean attribute is a property of an object that have two values: true and false.

        Chapter 5 pages 194 -- Essentials of Counter-Controlled Fepetition

        which counter-controlled is more usefull when writting a complex code, the FOR repetition statement or the WHILE statement? and what are the benefits of using that statement?

        Use counter controled loops for counting and scanning across data strutcures! Use the while for simple loops.

        Chapter 5 pages 197 -- For Loop

        What exactly is a "for loop" and how do you implement them?

        It is short hand for a very comoon kind of loop.

         		for( Student s = class.first(); not s.end(); s.next) ....

        The syntax is

         		for (A; B; C ) D
        where A is statement, B is a condition, C an statement and D any statement.

        The compiler converts it into this code

         		{
         			A;
         			while(B)
         			{
         				D;
          			C;
         			}
         		}

        Using "for" in this saves a lot of typing...

        The vast majority of for-loops look like

         		for( int i =1; i<=n; i++) ....
        or
         		for( int i =0; i<n; i++) ....

        Chapter 5 pages 206-213 -- Case- Swicth

        Is there a limit to how many cases in a case switch and can you call a void function into a case switch?

        There is no limit to the number of cases in a switch.

        THis have nothing to do with calling void-functions.

        Chapter 3 pages 216-217 -- Break and continue statements

        What would be a good example of when we would need to use these? Are they used a lot?

        IMHO: dont' use "continue".

        Use "break" at the end of each "case" in a switch.

        Sometimes you may find a "break" helpful in a complex loop.

        Chapter 6 pages 245-305 -- fucntions and vectors

        I would like you to explain more about vectors and functions and some examples on how they work or are used.

        If we have time.....

        Chapter 6 pages 245 -- recursion

        How much more will we talk about and use recursion?

        I might give a working recursive function to evaluate in a quiz or in the final. Example [ 02rec.cpp ] [ 20rec.cpp ]

        The topic does tend to appear in Computer Job interviews..... See [ Recursion ] on the wikipedia for an introduction.

        Some of the most beautiful structures in Nature (Fractals) are the result of recursive processes. See [ Fractal ] on the wikipedia for more details.

        Rule: Use recursion when the problem has a recursive structure.... the classic divide-and-conquer algorithms for searching and sorting. If an important recursive algorithm comes up we will mention it. We will test half a dozen in one of the labs.

        Second fact: when you call a function it gets a new set of variables to play with and if you don't allow for this then you won't be able to figure out what it does.

        Key fact: recursion works -- but like all programming -- it needs thought, to work well.

        Chapter 6 pages 245-332 -- pass-by-reference

        There is quite a bit of information that wasn't gone over in this chapter from my last class but in particular I was wondering if you could elaborate on the functionality and implementation of the idea of passing-by-reference and passing-by-value?

        Pass by value: the actual parameter is an expression. It is evaluated before the function is called. The result (the value) is passed to the formal parameter as its initial value.

        Pass by reference: The actual parameter must be a variable. The actual parameter must be a variable. The actual parameter must be a variable. Its address is passed to the function. All operations on the formal parameter are doene to actual parameter. (scary!)(but fast).

        Also I was wondering what is Function Overloading?

        When two functions have the same name but different typ[es of parameter.

        Chapter 6 pages 255 -- function prototypes and argument coercion

        I would like to know what the purpose of a void function prototype is. Why do we need it and what does it do?

        Void functions do things for use. non-void functions give us values.

        Chapter 6 pages 258-259 -- Rand()

        What, if anything, happens if you put an argument into the function rand()? meaning you enter something between the parentheses.

        I don't know.


        (rule1): Read the Famous Manual
        (rule2): Try it and see.

        Chapter 6 pages 279 -- Inline Functions

        When is it better to use an inline function?

        When it is short. Ideally one line long.

         		int min(int a, int b) { return (a<b ? a: a); }

        Chapter 6 pages 281 -- pass by reference

        When using '&' for pass by reference, what are the effects of the const keyword. What limitations does it place on the handling of the variable?

        When you pass a parameter by reference you normally give permission for the function to do things to the actual parameter. It can assign a new value, it can apply and operator like "++", it can send it a message:

         		formalParameter.message(data);
        that might change it in some way.

        If you add the "const" then the function can not assign a new value or apply an operation to it. The only message that it can sent are those that are also declared const. And the compiler will check to see if these message will do things to their object or not.

        Chapter 6 pages 294-298 -- Recursion

        In a recursion step, other than incorrectly writing the recursion step, would their be a situation where the function does infinite recursion where it can't break down a problem to a base case

        Yes. Bad designs tend to generate infinite processes -- whether they are recursive or iterative.

        Chapter 6 pages 302 -- recursion

        When is it better to use recursion vs iteration type repetition?

        When the problem is recursive then you'd be wise to code it using recursion. Example: searching fo something what you can divide up the possibilities into two nearly equal pieces and can pick the right half to search:

        Find_something_in(Here) (

        1. Divide Here into Part1 and Part2;
        2. Pick Part that contains the target;
        3. Find_something_in(Part)
        )

        But an iterative problem..... I have one hundred random numbers.... add them up is probably best done with an iteration.

        Chapter 6 pages 294 -- recursion

        Is recursion truly nesscary to be able to code at a high level?

        Its another tool that a competent programmer uses when needed.

        It is also a useful way of thought to have available when solving problems: If I could solve this subproblem then....

        Chapter 6 pages 281-285 -- References and Reference Parameters

        Can you explain what pass-by-reference does and what it means?

        Chapter number pages ppp-ppp -- Reading question

        Message When using an array are you able to make it into a function then call it as an if statement ??

        Chapter 6.21 pages 301 -- iomanip?

        What does this allow:
         #include <iomanip>
        [ iomanip.html ]

        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?

        Lab

        Review: [ lab01.html ]

        Next -- Arrays + Vectors + Project

        Topics: [ 03.html ]

        The first project [ p1.html ] is due in the fourth class. It is on Arrays (Chapter 7). Select one and start it as soon as you can. Hand in what ever you've got, however bad, at the start of the 4th class.

      . . . . . . . . . ( end of section CSci202 Computer Science II, Session 02, Review) <<Contents | End>>

    1. FAQ::="Frequently Asked Questions", see [ http://www.faqs.org/faqs/ ] for more FAQ.

      Abbreviations

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

    End