[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci Dept] / [R J Botting] / [CSci201] / 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]
Podcasts: [01] [02] [03] [04]
Labs: [01] [02] [03]
Thu Jan 17 11:45:34 PST 2008

Contents


    Chapter 2: pages 26-42 Variables and Arithmetic

      Podcast 3

      [ 03.mp3 ]

      Straight Forward Programs

      I define a straight forward program as one with the following algorithm:
      1. Get some input from the user (common but optional)
      2. Do some calculations
      3. Send some output to the user

      For example:
      InputOutput
      A positive real numberIts square root
      [ 02ex0.cpp ]

      The program structure for a straight forward program is, therefore

       		/* Name and Project
       		   Algorithm
       		   Status
       		*/
       		#include ...
       		using namespace std;
       		declare global constants like PI.
       		main()
       		{
       			Declare variables
       			Get values for input variables
       			Do some calculations
       			Send some output to the user
      		}

      Reading

    1. *** 2.1 Variables and Reading/writing
    2. ++ must reserve space for vairables and give them a name -- use a declaration.
    3. *** 2.2 Arithmetic
    4. ++ How to get π (PI) into a C++ Program -- -- --(PI)
       #include <cmath>
       using namespace std;
       const double PI = 4 * atan(1.0);
    5. ++++ Programs that use functions in <cmath> will compile but they will not link unless you compiler command looks like this:
       		g++ -lm -o pythagoras pythagoras.cpp
      (the "-lm" adds the compiled 'm' library to the program). However, Q understands this and does it for you.
    6. ++++ A common error is to guess that the '^' symbol is for powers. It isn't! It is an advanced bitwise logical operator covered in CSCI202. So to caculate the square of x write "x * x", and to cube it "x*x*x". For higher and unknown powers use the pow function from the <cmath> library:
       		double nth_power_of_x = pow(x, n);

      Syntax

      1. Elementary steps
        • Reserve storage to hold values in variables -- (declarations)
           		int v;
          (for counting)
           		double v;
          (for measurements)
        • Get input from user
           		cin >> v >> v...;
        • Send output to the user
           		cout << e << e ... ;
           		cout << "string" << endl ;
        • Do a calculation and store the answer.
           		v = e;
          where v is any variable name and e is any expression.
      2. Structures
        • Sequence
           		{
           			steps
           		}
        • Selection -- Later
        • Iteration -- Later

      ++ Tracing a Program

      You can not understand a computer program just by reading it. The only way to be sure you know what it will do is to pretend to be a computer and execute the program by hand. You need a workspace to simulate RAM. You need a calculator to simulate the CPU/processor. You then go thru the program, step by step, and use a paper and pencil to trace each step and what it does to the variables. Since variables change values a lot it helps if you reserve a column to record the values. It helps if you keep one finger on the code, and record each step and what RAM looks like.

      For example, if you see this bit of code:

       		int i;
      you will need to record a column like this
      int ii
      ?

      After these two steps:

       		int i;
       		i = 10;
      you will need to record a column like this
      int ii
      ?
      i=1010
      After these three steps:
       		int i;
       		i = 10;
       		i = 2 * i;
      you will need to record a column like this
      int ii
      ?
      i=1010
      i=2*20

      Questions

        Were we supposed to read all the sections or just the first two like in

        the syllabus

        For this class just pages 26-42.

        I don't have any questions today can I ask one one on Thursday

        You need to ask a question every class to get credit... even if you think you know the answer.

        Which is the most important part of the reading.

        Please look at the web page for the "***" ratings.

        Note: this question is too general to prove you did the reading.... next time no credit.

        How can I remember all the special words and rules

        Write them down on a single sheet of paper.

        You can bring a single such sheet into all quizzes and the final.

        Who figured out how to write expressions

        Jim Backus in FORTRAN I...... which lead to the Algols,....B,...C,...C++.

        Who figures out what to write in expressions

        Programmers.

        What is the precedence for oprators in C++

        It is close to "Please Excuse My Dear Aunt Sally" or the British "BODMAS" mnemonic.
        1. Parenthese (and Brackets)
        2. Multiply and Divide (left to right)
        3. Add and subtract (left to right)

        Do these precedence change in a program

        No.

        Are there any confusing expressions

        Warning: look out for "^" it is not exponentiation.

        Warning: Look out for expressions like

         		2/3
        because the computer sees two integers (2) and (3) and so does integer division giving the answer
         		0
        To get "two-thirds" you need to force the compile to use double length calclations like this
         		2.0/3.0
        See [ 03ex.cpp ] for a demo.

        What are real numbers

        Talk to the math department!

        Can you have a double constant and how do you do it

        Yes.
         		const double NAME = value;
        Example
         		const double PI = 4*atan(1);

        Note -- you can have constants of any type you want. When ever you learn about a new type you'll find you can construct constants for that type of object.

        Does C++ have sigle length vaariables like BASIC

        Yes. They are called 'float' vaariables and declared like this
         		float x;
         		float CONST = value;

        In this class don't use them -- our computers are fast and big enough to handle full precision double length calculations.

        What do double and const double do

        These reserve space for a double-length number -- ready to do calculations. Good for holding measurements. Vital when you want to do work with fractions. They also give the space a name.

        How do you use double in a program

        It's used to declare variables that hold measurements and fractions:
         		double name;
         		double name=initial value;

        You should always declare a variable before you use it.

        Is writing the declaration first like backwards

        Possibly -- but it is needed for the compiler to know what each symbol means the first time it sees it.

        Is there a limit to the number of variable you can have

        No.

        What is the int main()

        In some books (and my old examples) the main function is always written like this:
         		int main()
        rather than Jan's
         		main()
        Both are ok. The first is very precise. The second use a rule that, by default, functions return 'int's.

        Why is the final return not required by the standard

        Because if you had to have it then thousands of C and old C++ programs would by non-standard and not compile. The standards people (and most compilers) left a loop hole.

        Why aren't complicated math functions included automatically

        The take up time to compile and make the compiled program bigger.

        When C++ was C it was used mainly for "system programming" and there was no need for most program to do complex math.

        This is even more true about non-elementary functions that can be found on the Internet... but are not part of the standard. Most programs don't need them.

        How is input entered

        The program starts to run and stops. The user types in the input data and taps the "Enter" or "Return" key. The program takes what the user typed and makes it fit (if possible) what is in the cin statement.

        Can you solve and math problem in C++

        You can solve any solvable problem in C++.

        One of the big discoveries made in the beginning of computer science was the discovery that some problems can not be solved by a computer. These are called unsolvable problems and Turing was able to show that no computer would be able to solve them. This is a fascinating and tricky topic that is covered in the CSci500 level theory classes.

        What we can say is that if any machine can do it then C++ can program it.

        Can we make the the input and output look nice like Visual Basic

        Yes -- if you get a copy and #include the right library.

        Sadly I don't think we'll have time to do this in CSCI201.

        On the other hand it is very easy to do once you've got the library. For example both our KDE linux and MS Windows have nice user interfaces that let you draw forms and generate code for them.

        I didnt fully understand ++ and -- on page 39

        (1) It is a subtle bit of C++.

        (2) The book seems to have a misprint.

        C++ lets you put ++ and -- operations inside expressions. They always add (subtract) one from a variable, but they also return a value. Suppose we have
        ijk
        2417
        Then after

         		k = ++ i;
        we have
        ijk
        343

        If we again start with
        ijk
        2417
        Then after

         		k = i ++;
        we have
        ijk
        342

        Summary of the rules
        ExpressionEffect on iReturned value
        i+1Noneold i + 1
        i++add 1 to iold i
        ++iadd 1 to iold i + 1

        Why is tracing a program helpful

        No other method gives you an understanding of what the computer is going to do.

        It shows that you understand the program and so can earn you points in a quiz.

        What does cout mean

        This variable is pronounced "C out" and it means: the place where C output is sent.

      Exercise -- Tracing a very simple program

      What does this program output: [ 02ex1.cpp ]

      Exercise -- Tracing a straight forward program

      I will show you a straight forward program you predict what it outputs. [ 02ex2.cpp ]

      Quiz 1

      I will give you some straight forward programs and you work out what they output.

      I will also ask you to describe a straight forward program that you'd like to do as a project: inputs and output. See [ projects.html#P1 ]

      Lab 02 -- Straight forward programs

      Note

      The first project is also a straight forward program. But not one in the labs or in class!

      Next -- Selections

    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. TBA::="To Be Announced", something I should do.
  4. TBD::="To Be Done", something you have to do.
  5. UML::="Unified Modeling Language", [ uml.html ] (beginner's introduction to the UML).

End