.Open Chapter 2: pages 26-42 Variables and Arithmetic . Podcast 3 .See ./03.mp3 . Straight Forward Programs I define a .Key straight forward program as one with the following algorithm: .List Get some input from the user (common but optional) Do some calculations Send some output to the user .Close.List For example: .Table Input Output .Row A positive real number Its square root .Close.Table .See ./02ex0.cpp The program structure for a straight forward program is, therefore .As_is /* Name and Project .As_is Algorithm .As_is Status .As_is */ .As_is #include ... .As_is using namespace std; .As_is declare global constants like PI. .As_is main() .As_is { .As_is Declare variables .As_is Get values for input variables .As_is Do some calculations .As_is Send some output to the user .As_is } . Reading *** 2.1 Variables and Reading/writing ++ must reserve space for vairables and give them a name -- use a declaration. *** 2.2 Arithmetic ++ How to get \pi (PI) into a C++ Program --(PI) .As_is #include .As_is using namespace std; .As_is const double PI = 4 * atan(1.0); ++++ Programs that use functions in will compile but they will not link unless you compiler command looks like this: .As_is 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. ++++ 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 library: .As_is double nth_power_of_x = pow(x, n); .Open Syntax Elementary steps .Set Reserve storage to hold values in variables -- (declarations) .As_is int v; (for counting) .As_is double v; (for measurements) Get input from user .As_is cin >> v >> v...; Send output to the user .As_is cout << e << e ... ; .As_is cout << "string" << endl ; Do a calculation and store the answer. .As_is v = e; where `v` is any variable name and `e` is any expression. .Close.Set Structures .Set Sequence .As_is { .As_is steps .As_is } Selection -- Later Iteration -- Later .Close.Set .Close . ++ 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: .As_is int i; you will need to record a column like this .Table int i i .Row ? .Close.Table After these two steps: .As_is int i; .As_is i = 10; you will need to record a column like this .Table int i i .Row ? .Row i=10 10 .Close.Table After these three steps: .As_is int i; .As_is i = 10; .As_is i = 2 * i; you will need to record a column like this .Table int i i .Row ? .Row i=10 10 .Row i=2* 20 .Close.Table .Open 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. .List Parenthese (and Brackets) Multiply and Divide (left to right) Add and subtract (left to right) .Close.List . 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 .As_is 2/3 because the computer sees two integers (2) and (3) and so does integer division giving the answer .As_is 0 To get "two-thirds" you need to force the compile to use double length calclations like this .As_is 2.0/3.0 See .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. .As_is const double NAME = value; Example .As_is 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 .As_is float x; .As_is 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: .As_is double name; .As_is 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: .As_is int main() rather than Jan's .As_is 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 .Key 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 .Table i j k .Row 2 4 17 .Close.Table Then after .As_is k = ++ i; we have .Table i j k .Row 3 4 3 .Close.Table If we again start with .Table i j k .Row 2 4 17 .Close.Table Then after .As_is k = i ++; we have .Table i j k .Row 3 4 2 .Close.Table Summary of the rules .Table Expression Effect on i Returned value .Row i+1 None old i + 1 .Row i++ add 1 to i old i .Row ++i add 1 to i old i + 1 .Close.Table . 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. .Close . Exercise -- Tracing a very simple program What does this program output: .See ./02ex1.cpp . Exercise -- Tracing a straight forward program I will show you a straight forward program you predict what it outputs. .See ./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 .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 .Close