[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 03
[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]
Mon Apr 15 06:40:55 PDT 2013

Contents


    cs201/03 -- Numbers and Arithmetic

      Previous

      [ 02.html ]

      Due in

        One Review Qn+Answer

        Agreement form + Your Key word to grading

      Study Pages 31 to 62

        2.1 Number Types

        Use int for counting and double for measurements.

        Syntax 2.1 -- Output Statement

      1. output_statement::="cout" #( "<<" expression )";".
         		cout << wages * TAX_RATE;
         		cout << first_name << " " << second_name << "\n";
        Do NOT forget the semicolon(";").

        Syntax 2.2 -- Comments

        Help you understand the code. When you write it, then others read it. The compiler ignores comments.

        Syntax 2.3 -- Variable Definition

      2. variable_definition::=type name ";" | type name "=" initial_value ";".
         		double x;
         		int count = 0;
         		int student_number = 1;

        Quality tip 2.1 -- Initialize when you declare

        Quality tip 2.2 -- choose good names

        Only in mathematical programs should you use mathematical variables -- one letter long.

        Advanced Topic 2.1 -- Numeric ranges and precision

        Come back to this later.

        Random Fact 2.1 -- The Pentium floating-point bug

        2.2 Input

        Syntax 2.4 Input Statement

      3. input_statement::= "cin" #(">>" variable) ";".
         		cin >> wages;

        Common Error 2.1 -- Buffered Input

        Common Error 2.2 -- Failed Input

        2.3 Assignment

        This is NOT mathematical equality!

        An assignment changes the value of a variable.

      4. assignment::= variable "=" expression ";".
         		b = 2*a;

        This means:

        1. Evaluate the expression using the current values for variables.
        2. Change the variable to the value of the expression

        Sequences of Assignments

         		a = a+b;
         		b = 2*a;
        Always work out even simple sequences of assignments, step by step, on a piece of paper.

        Never guess what a series of assignments do, until you have tried them out, step by step, on a simple example, using pencil and paper.

        Example. What happens to integer a and b if they both start with value 1 and this seqence is executed

         		a = a+b;
         		b = 2*a;
         		a = 3*b +2*a;

        Solution using equalities:
        Table
        StepState of memory
        Beforea = 1, b = 1
        After a=a+b;a = 2, b = 1
        After b=2*a;a = 2, b = 4
        After a=3*b+2*a;a = 16, b = 4

        (Close Table)

        Abbreviated solution using a table of values:
        Table
        Stepa's valueb's value
        Before11
        After a=a+b;21
        After b=2*a;24
        After a=3*b+2*a;164

        (Close Table)

        This process is called tracing and is very useful when trying to figure out code.

        Two exercises -- assume that a,b,c,... are ints.

        Exercise: What happens if we start with a equal to 1 and b equal to 2?

         		a=b;
         		b=a;
        [ trace0.cpp ]

        Exercise: What happens if we start with a equal to 1 and b equal to 2?

         		c=a;
         		a=b;
         		b=c;
        [ trace1.cpp ]

        Always trace simple sequences of assignments, step by step, on a piece of paper.

        Syntax 2.5 -- Assignment

      5. assignment::= variable "=" expression ";".
         		tax = TAX_RATE * wages;

        Common Error 2.3 -- Roundoff Errors

        A rounding error once stopped a probe docking with a satelite.

        Advanced Topic 2.2 -- Casts

        Come back to this later.

        Advanced Topic 2.3 -- Combining assignment with arithmetic

        Don't!

        2.4 Constants

        Do.

        Syntax 2.7 Constant Definition

      6. constant_definition::= "const" type_name constant_name "=" expression ";". ,As_is const double HALF = 0.5;
         const int MINUTES_PER_HOUR = 60;
         const double INCHES_PER_FOOT = 12;
         const double TAX_RATE = 30.0/100.0;

        Famous Constants

        If you need π, e, or another famous constant, then see [ index.html#PI ] for the standard definitions.

        Quality tip 2.3 No Magic Numbers

        The only "magic numbers" I will accept in your code are "1", "0", "-1" and possibly "2". All other constants must be given a name and the name used in place of the value.

        Advanced Topic -- enumerated types

        Cool.... but come back later.

        2.5 Arithmetic

        Syntax 2.8 Function Call

      7. function_call::= function_name "(" list_of_expressions ")".
         		sqrt(b*b-4*a*c)

        Table 3 -- Other Mathematical Functions

        Stick a sticky note on this page!

        Common Error 2.4 -- Integer division

        Try this statement in a program
         		cout << 1/2;

        +++ You will note that I automatically write a half as 0.5 in my code. I've learned to not write 1/2. Since this went wrong in languages like FORTRAN and Algol in the 1960s. Also I know that multiplying is faster than dividing and so "xxx*0.5" is faster than "xxx/2".

        Common error 2.5 -- Unbalanced Parentheses

        Most editors can show you matching parentheses. It helps.

        Common Error 2.6 -- Forgetting Header files

        [ http://www.cplusplus.com/ ]

        Advance Topic 2.5 -- Remainder of negative integers

        Even tho' this led to nasty bug in my Ph.D. code.... you can skip this topic in this course.

        Quality Tip 2.4 -- White Space

        Do this or lose points. I expect you to lay out code so that it is easy to read.

        Quality Tip 2.5 -- factor out common code

        Refactoring code is something you should do when you get it to work.

        Refactor working code one small edit at a time. No clever steps! And repeat all the tests after each small change.

      Review Questions

      Try to do as many of these questions as you have time for: R2.1, R2.2, R2.3, R2.4, R2.5, R2.6, R2.7.

      Write up and hand in one of these exercises and your solution, with your name, for me to grade, before the start of class.

      Exercises to be done in class

      [ 03ex0.cpp ] [ 03ex1.cpp ] [ 03ex2.cpp ] [ 03ex4.cpp ] [ 03ex.cpp ] [ 03exans.cpp ]

      Link to CodePad

      [ http://codepad.org/ ]

      Working remotely -- not easy but possible

      Project Due -- Next time

      What project will you be doing? [ projects.html#P1 ]

      Start by downloading [ project.cpp ] and thinking about what you have to change -- for example putting in your name.

      Ideally you should then edit the file to fit one of the programming projects in chapter 1 of the text, compile, run, debug, and hand in a print out of the program at the start of the next class.

      But if you are in the second lab you won't have met the tools... So you can (0) work out how to use them on your own, OR (1) edit it on your own machine and print the result, OR (2) print it out here and mark up the changes you will work and hand that in. In these cases resubmit the completed project next week.

      Quiz -- Next Time

      Including a question on your project and another on arithmetic/assignements in C++.

      Lab 02

      [ lab02/ ]

      Next

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