Sie sind auf Seite 1von 20

ENGR 1200U Introduction to Programming Lecture 5 Simple C++ Programs (Chapter 2) Dr.

Eyhab Al-Masri

1992-2012 by Pearson Education, Inc. & John Wiley & Sons Some portions are adopted from C++ for Everyone by Horstmann

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; // Compute sides of a right triangle. side1 = x2 - x1; side2 = y2 - y1; distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the two points is " << distance << endl; // Exit program. return 0; } //-------------------------------------------------------ENGR 1200U Winter 2013 - UOIT

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; // Compute sides of a right triangle. side1 = x2 - x1; side2 = y2 - y1; distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the two points is " << distance << endl; // Exit program. return 0; } //-------------------------------------------------------ENGR 1200U Winter 2013 - UOIT

Comments: Document the programs purpose Help the human reader understand the program Are ignored by the compiler // comments to end-of line /* starts a comment block ending with */

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; // Compute sides of a right triangle. side1 = x2 - x1; side2 = y2 - y1; distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the two points is " << distance << endl; // Exit program. return 0; }

Preprocessor Directives Give instructions to the preprocessor before the program is compiled. Begin with # #include directives add or insert the named files (and the functionality defined in the files) into the program

ENGR 1200U Winter 2013 - UOIT

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; // Compute sides of a right triangle. side1 = x2 - x1; side2 = y2 - y1; distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the two points is " << distance << endl; // Exit program. return 0; } //-------------------------------------------------------ENGR 1200U Winter 2013 - UOIT

using Directives: Tell the compiler to use the library names declared in the namespace. The std, or standard namespace contains C++ language-defined components.

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. Main function header double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; Defines the starting point // Compute sides of a right triangle. (i.e. entry point) for a C++ side1 = x2 - x1; program side2 = y2 - y1; The keyword int indicates distance = sqrt(side1*side1 + side2*side2); that the function will return // Print distance. an integer value to the cout << "The distance between the two points is " system when the function << distance << endl; completes // Exit program. return 0; Every program has exactly } one main function //-------------------------------------------------------ENGR 1200U Winter 2013 - UOIT

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), Code Blocks side1, side2, distance; // Compute sides of a right triangle. Are zero or more C++ declarations or side1 = x2 - x1; statements enclosed by side2 = y2 - y1; curly brackets { } distance = sqrt(side1*side1 + side2*side2); // Print distance. The code that defines cout << "The distance between the two points is " what a function does (in << distance << endl; this case, the main // Exit program. function of the program) is return 0; often defined in a code } block following the header. //-------------------------------------------------------ENGR 1200U Winter 2013 - UOIT

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; // Compute sides of a right triangle. side1 = x2 - x1; side2 = y2 - y1; distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the two points is " << distance << endl; // Exit program. return 0; } //-------------------------------------------------------ENGR 1200U Winter 2013 - UOIT

Declarations
Define identifiers (e.g. variables and objects) and allocate memory. May also provide initial values for variables. Must be made before actions can be performed on variables / objects.

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. Statements double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; Specify the operations to // Compute sides of a right triangle. be performed. side1 = x2 - x1; side2 = y2 - y1; cout is the output distance = sqrt(side1*side1 + side2*side2); statement // Print distance. To display output on the cout << "The distance between the two points is " screen, you send them to << distance << endl; an entity called cout // Exit program. return 0; The << operator denotes } the send to command //-------------------------------------------------------ENGR 1200U Winter 2013 - UOIT

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), Common error side1, side2, distance; Omitting a semicolon (or two) // Compute sides of a right triangle. side1 = x2 - x1 Oh No! side2 = y2 - y1; distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the two points is " << distance << endl; // Exit program. return 0; } //-------------------------------------------------------ENGR 1200U Winter 2013 - UOIT

Errors

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; // Compute sides of a right triangle. Without semicolon, these side1 = x2 - x1 side2 = y2 - y1; statements are equivalent to distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the two points is " << distance << endl; // Exit program. which thoroughly confuses the compiler! return 0; This is a compile-time error or syntax error } //--------------------------------------------------------

Errors

ENGR 1200U Winter 2013 - UOIT

A syntax error is a part of a program that does not conform to the rules of the programming language

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; // Compute sides of a right triangle. side1 = x2 - x1; side2 = y2 - y1; distance = sqrt(side1*side1 + side2*side2); // Print distance. cot << "The distance between the two points is " << distance << endl; // Exit program. This will cause a compiler error return 0; Compiler has no clue what is meant by cot } //-------------------------------------------------------ENGR 1200U Winter 2013 - UOIT

Errors

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), The distance between the two points is is called side1, side2, distance; a string // Compute sides of a right triangle. You must put those double-quotes around strings side1 = x2 - x1; side2 = y2 - y1; distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the two points is " << distance << endl; // Exit program. return 0; } The endl symbol denotes an end of line //-------------------------------------------------------marker which causes the cursor to move to

Errors

the next screen row


ENGR 1200U Winter 2013 - UOIT

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; // Compute sides of a right triangle. side1 = x2 - x1; side2 = y2 - y1; distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the tuo points is " << distance << endl; // Exit program. return 0; } Logic errors or run-time errors are errors //-------------------------------------------------------in a program that compiles (the syntax is

Errors

ENGR 1200U Winter 2013 - UOIT

correct), but executes without performing the intended action

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), Some kinds of run-time errors are so severe side1, side2, distance; that they generate an exception: a signal // Compute sides of a right triangle. from the processor that aborts the program side1 = x2 - x1; with an error message side2 = y2 / 0; distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the two points is " << distance << endl; // Exit program. return 0; } //--------------------------------------------------------

Errors

ENGR 1200U Winter 2013 - UOIT

/*-------------------------------------------------------* Program chapter1_1 * This program computes the distance between two points. */ #include <iostream> // Required for cout, endl. #include <cmath> // Required for sqrt() using namespace std; Every C++ program must have one and int main() only one main function { C++ is case sensitive. Typing: // Declare and initialize objects. int Main() double x1(1), y1(5), x2(4), y2(7), will compile but will not link side1, side2, distance; // Compute sides of a right triangle. side1 = x2 - x1; side2 = y2 / 0; distance = sqrt(side1*side1 + side2*side2); // Print distance. A link-time error occurs here cout << "The distance between the two points is " when the linker cannot find the << distance << endl; main function // Exit program. return 0; } //--------------------------------------------------------

Errors

ENGR 1200U Winter 2013 - UOIT

The characters \n are not printed on the screen The backslash (\) is called an escape character. It indicates that a special character is to be output The escape sequence \n means newline Causes the cursor to move to the beginning of the next line on the screen When the return statement is used at the end of main the value 0 indicates that the program has terminated successfully

ENGR 1200U Winter 2013 - UOIT

ENGR 1200U Winter 2013 - UOIT

For each problem the programmer goes through these steps Algorithms
No! An algorithm is a RECIPE

ENGR 1200U Winter 2013 - UOIT

Any solvable computing problem can be solved by the execution of a series of actions in a specific order An algorithm is a procedure for solving a problem in terms of Specifying the order in which statements (actions) execute in a computer program is called program control
the actions to execute and the order in which the actions execute

ENGR 1200U Winter 2013 - UOIT

10

Pseudocode is an artificial and informal language that helps you develop algorithms.
Similar to everyday English Convenient and user friendly.

Helps you think out a program before attempting to write it. Carefully prepared pseudocode can easily be converted to a corresponding C++ program.

ENGR 1200U Winter 2013 - UOIT

C++ Standard Library C++ programs consist of pieces called classes and functions

Most C++ programmers take advantage of the rich collections of classes and functions in the C++ Standard Library Two parts to learn the C++ world The C++ language itself, How to use the classes and functions in the C++ Standard Library

ENGR 1200U Winter 2013 - UOIT

11

C++ systems consist of three parts:


Program development environment Language C++ Standard Library Edit Preprocess Compile Link Load, and Execute

Typically, there are six phases in which C++ programs go through

ENGR 1200U Winter 2013 - UOIT

Data type double is for specifying real numbers, and data type char for specifying character data. Real numbers are numbers with decimal points, such as 3.4, 0.0 and 11.19 A char variable may hold only a single lowercase letter, a single uppercase letter, a single digit or a single special character (e.g., $ or *) Types such as int, double and char are called fundamental data types

ENGR 1200U Winter 2013 - UOIT

12

A number written by a programmer is called a number literal.

There are rules for writing literal values:

ENGR 1200U Winter 2013 - UOIT

Fundamental Data Types (contd)

Keyword bool char int double string

Example of a constant true '5' 25 25.0 "hello" //#include<string>

ENGR 1200U Winter 2013 - UOIT

13

Constants and Variables

1992-2012 by Pearson Education, Inc. & John Wiley & Sons Some portions are adopted from C++ for Everyone by Horstmann

Constants and variables represent memory locations that are used to store information Constants are objects that store specific data that can not be modified.
10 is an integer constant 4.5 is a floating point constant "The distance between the two points is" is a string constant 'a' is a character constant

Variables are named memory locations that store values that can be modified.
double x1(1.0), x2(4.5), side1; side1 = x2 - x1; x1, x2 and side1 are examples of variables that can be modified.

ENGR 1200U Winter 2013 - UOIT

14

Must begin with an alphabetic character or the underscore character _ You cannot use other symbols such as $ or %. Spaces are not permitted inside names; Alphabetic characters may be either upper or lower case
C++ is CASE SENSITIVE, so a != A, etc

May contain digits, but not as the first character May NOT be C++ keywords

ENGR 1200U Winter 2013 - UOIT

ENGR 1200U Winter 2013 - UOIT

15

The following statement defines a variable. cans_per_pack is the variables name.


int cans_per_pack = 6;

int indicates that the variable cans_per_pack will be used to hold integers. = 6
ENGR 1200U Winter 2013 - UOIT

indicates that the variable cans_per_pack will initially contain the value 6.

ENGR 1200U Winter 2013 - UOIT

16

You should pick a variable name that explains its purpose


A good name describes the contents of the variable or what the variable will be used for For example, it is better to use a descriptive name, such as can_volume, than a terse name, such as cv.

Must be declared (and therefore typed) before they may be used


C++ is a strongly typed programming language
Every variable must be declared before usage

ENGR 1200U Winter 2013 - UOIT

When creating variables, the programmer specifies the type of information to be stored (data type) C++ does not provide initial values for variables

Initialization is putting a value into a variable when it is created Initialization is not required

Using the value of a variable before it is initialized may result in garbage

ENGR 1200U Winter 2013 - UOIT

17

Bits: smallest data item takes value of 0 or 1 Characters: decimal digits (0-9), letters (A-Z), and special characters (i.e. $, %, @, #, &, *, (, ), , :,;,-,/) Fields: Group of characters or bytes that convey meaning (i.e. persons name, car model, etc) Records: Group of related fields

File: a group of related records (contains arbitrary data and arbitrary format) Some OSs view files as sequence of bytes
ENGR 1200U Winter 2013 - UOIT

ENGR 1200U Winter 2013 - UOIT

18

Memory snapshots are diagrams that show the types and contents of variables at a particular point in time.

ENGR 1200U Winter 2013 - UOIT

ENGR 1200U Winter 2013 - UOIT

19

A type declaration statement defines new identifiers and allocates memory. An initial value may be assigned to a memory location at the time an identifier is defined.
Syntax [modifier] type specifier identifier [= initial value]; [modifier] type specifier identifier[(initial value)]; Examples double x1, y1(0); int counter=0; const int MIN_SIZE=0; bool error(false); char comma(',');

ENGR 1200U Winter 2013 - UOIT

20

Das könnte Ihnen auch gefallen