Skip to main contentCal State San Bernardino / [CNS] / [Comp Sci Dept] / [R J Botting] >> [CSci202] >> 02
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Contact] [Grading]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Sat Jun 2 11:24:26 PDT 2007

Contents


    CSci202 Computer Science II, Session 02, Review

      Previous

      [ 01.html ]

      Preparation

      Study the page below and chapters 1,2,3, and 4 and write down the questions, doubts, and surprises that you have on a piece of paper. Include your printed name!

      Assigned Work Due

      Hand in a question, doubt, or surprise. Include your name (to earn credit), a page number (to help me organize the class), and a question/surprise/doubt.

      Note: a sheet of paper with just a name earns 1 point (for participating) and with all three parts earns 2 points: assigned work+participation.

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

      Online notes on topics in survey

        Arrays

        [ vectors.html#Arrays ]

        Vectors

        [ vectors.html ]

      Questions

        Here are some special answers for the record:

        Should we use 'using namespace std'

        Yes

        Must we use 'using namespace std'

        The compiler may let it thru... and so might I.

        When should I use for and while loops

        Use while for simple iterations that repeat until some condition is false.

        Use for when counting or taking each item in a vector, array, string, etc. in turn.

        Notice that

         		for(A; B; C;) S
        does the same thing as
         		{ A;
         		  while(B)
         		  {
         			S;
         			C;
         		  }
         		}

        As a rule --
        (KISS): Keep It Simple ...

        What does break do

        The break statement in C/C++ terminates a loop. It also terminates a case in switch (see later)

        Is there a 1 or 2 page handout on the 'vi' editor

        Yes. See [ Vi in resources ]

        Are arrays important

        You need to understand them because they occur in most older programs and save storage and run time compared to vectors and strings. When we ask for extra memory we often use an array to get the data. Similarly when we read data off a disk or tape we get a fixed amount of data in a fixed layout and an array is best way to go.

        Use an array only when you know, for an absolute fact, how many items you will need or (for experts) when you can calculate the absolute maximum number of items. Otherwise use vectors or dequeues.

        They will be in the labs. I'll try to keep them out of the quizzes and final. They may be part of some projects. They have to turn up in several lectures/discussions.

        In lab02 I will try to convince you that arrays can be dangerous. We will do more with arrays in class as well.... in particular we will wrap one up (chapter 7) in a class so it can be used safely.

        Distinguish deques vectors and arrays

        An array is a fixed number of numbered items. You can only declare it with a constant size. You can not put in or remove items from it.

        A vector is like an array but it's size can change. It lets you push new items on to it at one end, and also remove (pop) them from the same end.

        A deque Is like a vector except you can push and pop at both ends.

        Here is an application of a deque -- simulating the line in the Arrowhead Post Office in North San Bernardino. People come in and join the line at one end and leave it at the other... but sometime somebody comes in joins the end (push_back) and, before any one else comes in, they give up and leave (pop_back).

        Note: There are also stacks and queues. A queue you 'push' at one end and 'pop' at the other. A stack you 'push' and 'pop' at the same end. More later on [ stl.html ] when we cover the Standard Template Library.

        What is the difference between = and ==

         		v=e		//v is a variable, and e an expression
        Evaluates e, and takes the value and stuffs it into v.

         		e1==e1		//e1 and e2 expressions
        Evaluates BOTH e1 and e2, and compares them. If they are equal it return true otherwise false.

        Why overload functions?

        An overload function has one name but operates on different data types:
         		int twice(int i) { return i+i;}
         		double twice(double i) { return 2.0*i;}
         		string twice(string i) { return i+i;}
        But they all do similar things.

        The compiler picks the right one, so it is a waste of time giving them different names.

        How to trace a complex algorithm or program

        Find a large sheet of paper or an erasable board. Divide it into columns: one for each piece of data. Keep one finger on the code/algorithm to make where you have got to. Write new values in the right column. BE CAREFUL.
         		double s=0.0;
         		for(int i=1; i<5; i++)
         			s=s+i;
        Variablessi
        Commanddoubleint
        double s=0.0;0
        i=101
        i<5?
        s=s+i;11
        i++02
        i<5?
        s=s+i;32
        i++33
        I<5?
        s=s+i;63
        i++64
        i<5?
        s=s+i;104
        i++105
        i<5? FALSE

        How do you work out what a program does?

        Step by step, very carefully, attending to every detail...

        Avoiding guessing until you've traced it, step by step,...

        What is a recursive function?

        A recursive function is defined by a set of statements that include the possibility of calling the function again on a simpler or smaller set of data.

        You can learn a lot about it from working out what a simple (useless) program does: [ 02rec.cpp ] , for example.

        If you haven't figured it out yet, try this link [ lookup.php?search=recursive ]

        Why does the program that translate American and British dates need an array of 9 chars?

        There are 6 digits (MMDDYY) + 2 slashes(//) + one null character ('\0') used to terminate the text strings. You may be able to do it with out the two slashes and so need only 7 characters, however. I'm not sure if this might end up being more confusing!

        When should use a sentinel like -1 and when a CTRL/D to terminate input.

        Use sentinels when there is more data to be sent after the sentinel.

        Use a sentinel when normal humans provide the input.

        Use CTRL/D or CTRL/Z when the data comes from a file, another program, or a three star computer geek.

        When to use ints and when to use doubles.

        Is the data counting or indexing something? Is it always a whole number? Then use int.

        Is the data a measurement? Is it approximate? Can it have fractions? Then use double.

        Is if(not(cin>>a)) new?

        No. It (or something like it) goes back to the very first book on C in the late 1970's. Neat hacks like this tend to survive forever.

        Will we be programming much graphics in the class.

        No. Unless you want to! We will use simple graphic tools like Dia to draw UML diagrams.

        Will we be doing exceptions?

        Yes in a later class..... see the schedule

        Where do 'break' statement go?

        In the middle of a loop, or at the end of a 'case' in a 'switch'(later).

        Difference between while and do while

        The while loop
         		while(C){B}
        does this
         			C; B; C; B; ...; B; !C

        The do-while loop

         		do{B}while(C)
        does this
         			B; C; B; ...; B; !C

        What are the difference between an array and a class?

        ArrayClass
        is an objectis a data type
        has numbered itemshas objects with named members
        is old fashionedis object oriented

        Lab

        Review: [ lab01.html ]

        Next

        How data is stored and retrieved on a computer (chapter 5) [ 03.html ]

        . . . . . . . . . ( 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. TBA::="To Be Announced", something I have to do.
      3. TBD::="To Be Done", something you have to do.
      4. Dia::="A free Open Source Diagramming tool for Linux, Windoze, etc. ".
      5. YAGNI::="You Ain't Gonna Need It".
      6. DRY::="Don't Repeat Yourself".

        End