1

first programs.doc 1-

Intro—See my sheets.

4 parts to course-- See my sheets.

Programs are written to solve problems. This is done in several steps:

  • Clearly define and understand the problem.
  • Outline a solution to the problem.
  • Implement the solution by writing a program for the computer.

The machine really understands only machine language (0’s and 1’s). We write in high level language. Compiler translates. Diagram – See my sheets.

First program

Seehello.cpp. conventional first program in any language.

The line numbers are not part of the program. They are there so I can refer to them in class. You should not print them.

The skipped lines make it easier for the human to read the program. As far as the compiler is concerned, the whole program could be written on one line (called format-free).

A comment is ignored by the computer. It is there for the benefit of the human reading the program. C++ has 2 kinds of comments:

  • single line compiler ignores // and everything that follows on the line, preferred style
  • multi-line compiler ignores /* */ and everything between them. Doesn’t nest. Often used to temporarily ignore sections of code.

One should be able to read the program without the comments and read the comments without the program. The comments should explain how you are solving the problem. Assume your reader knows C++. Some of my comments explain C++ to you because I am teaching you.

Every program should have a comment stating the name of the file so that you can locate it at some future time. Standard to use .cpp for C++.

There should be a comment saying what the program does.

Any line in a program that starts with # is preprocessor directive, not an actual statement in the C++ language. The preprocessor modifies the code before it is sent to the compiler. # include says to insert the header file at this point. < > says to look for the header file in the usual place. Just copy it for now. More in chapter 5. [Note to me: System file in system directory]

The line using namespace std; allows us to use certain shortcuts, for example to shorten the name of the header file from iostream.h to the simpler iostream. E.g. don’t have to write std::endl it understands endl.

A function is a named set of statements. We build our programs using functions. Initially we’ll have only one function named main(). In Chap. 5 we’ll add more.Typically each function performs one particular task. int main(), is called the main program header. It tells the compiler that we are starting to work on the main function of this program. Every program must have a main function since this is where the computer begins to execute the program. The word int says that this program will return an integer (See chap 5), and the empty set of parentheses indicates that the main function will not be sent any information from the outside. (Not expected to understand yet)

Inside every function is a set of curly braces {} containing a series of statements which comprise the body. The braces are aligned. The body is indented. It is standard to use the tab key to indent uniformly. Advice: Indenting helps you write the program properly. Don’t wait till the end to indent to please me.

A semicolon terminates every complete statement in C++. A comment does not need a semicolon since it is not a statement. Neither does a compiler directive. The main program header does not need a comma because it is not a complete statement.

A string literalis a string of 0 or more characters enclosed in double quotes. What you are used to calling “quotation marks”In Chapter 4, we’ll learn to use single quotes, too.

The simplest way to print in C++ is to use the standard output stream (whatever that means) cout (pronounced C-out).Cout is declared iniostream>.The printed or displayed result of running a program is called the output. Sending output to the screen is the default output method. Default means that this is what the computer assumes you want to do unless you instruct it otherwise.

The insertion operatoris used to insert text into the output stream. Remember which way it points.

endl says to go to a new line.

system(PAUSE); is needed to keep your output from disappearing in Dev C++. Run your program once without it to see what I mean. #include <stdlib.h> used to be needed for the compiler to understand it.

return(0); will be uderstood in chap 5. just copy it for now. this statement is the logical end of the program; its execution terminates the program. Returning 0 is the conventional indicator of normal termination. It indicates that the program ran successfully. Usually, returning 1 signals an error. After the return0; statement, we mark the physical end of the program with a closing brace to match the opening brace that appeared immediately after the main program header.

Put on board Format of program, from purple sheet.

LAB: Ide.doc Output2paper.doc Modify hello.cpp to print your name.

A variable is a name for a physical location within the computer that can hold one value at a time. As the name implies, the value may change as the program is running. Give your variables meaningful names that suggest how the variables are being used; these are called mnemonic names.

Adeclaration statement tells the computer which variables will be used in the program and what type of values they will hold. A variable of type intwill hold an integer. We could declare each variable in a different statement, But we tend to group variables of one type, separating them with commas.

Draw boxes.

The symbol is the assignment operator. It is used inan assignment statement.

Keeping track of the value stored in each variable is called tracing a program, or producing a Memory Trace. You must do this for the programs you submit. It is the best way to figure out what a program that you are given will do.

Show example.

We can usecout to print the value of a variable or an expression. The expression is evaluated (the value of the variable is looked up if necessary) and the resultis sent to the output stream; in other words, it prints the value.

Let’s look at a simple C++ program that adds two numbers and prints the result.

See add.cpp

General Form of a Statement Using the Output Stream cout

cout < ... < ... < ... ;

↑ ↑ ↑

list of literals or

expressions to be printed

Guidelines for using cout

• The statement starts with the name of the output stream cout.

• The output streamcout is followed by the insertion operator < and then by the first item whose value we want printed.

•This can be followed by additional items to be printed, each one preceded by the insertion operator.

• A literal string consisting of a single blank within quotation marks (" ") means that a single blank will be printed. This blank serves to separate other values being printed.

• The stream manipulator endl tells the machine to go to a new line. The cursor (the _ which blinks on the screen) marks the current print position on the screen or the printer. Sending endl to the output stream cout moves the cursor to the beginning of a new line.

• If endl is not printed, the next item to be printed will start on the same line--we will discuss this in more detail in Chapter 2.

• With just a few other exceptions to be covered later, other symbols in a literal string (e.g., letters or punctuation marks) will print exactly as they appear.

• The entire statement ends in a semicolon.

LAB: Variables.doc

Pseudocode as a Problem-Solving Tool

In our first few programs, it will be relatively easy to go directly from the English-language description of the problem to the C++ program. However, most actual programming problems are too long and/or complex to be translated directly. Programmers usually introduce at least one intermediate step between English and C++. The one that we will use is called pseudocode. It consists of statements which are a combination of English and C. As we shall see, pseudocode can be refined and made more precise gradually. In fact, the pseudocode for one stage of the development process will often be used as a comment at the next stage. Pseudocode is a restatement of the problem as a list of steps that describe the solution. Using the pseudocode, a programmer then writes the actual program. Although the main use of pseudocode is to help us to translate from English to C++, it is also an outline of the basic structure or logic of the program and a key part of what we will later call the documentation.

Pseudocode

Add two numbers

Print the sum

Assignment operator

Evaluate the expression on the right-hand side of the assignment operator and assign the result to the variable on the left-hand side of the assignment operator.

This is not equality: x = x+1;

In math, what value would make this true? Trace!

For Loops etc See my sheets

Increment Operator, Decrement Operator See my sheets

[Note to me: This was in chap 2. should have been in chap 1. I didn’t copy it because I expected to use my own notes.]

It is not possible to increment or decrement an expression or a constant. Both of the following examples are illegal in C++:

(x + y)++; // illegal

4--; // illegal

Only a variable can be incremented or decremented.