week 1 algorithmization and programming languages

30
Week 1 Algorithmization and Programming Languages

Upload: grant-cooper

Post on 03-Jan-2016

231 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Week 1 Algorithmization and Programming Languages

Week 1

Algorithmization and Programming Languages

Page 2: Week 1 Algorithmization and Programming Languages

Introduction

About C++ Facilitates a disciplined approach to program

design C++ programs consist of pieces called classes

and functions Provides C++ Standard Library

Page 3: Week 1 Algorithmization and Programming Languages

First Program in C++: Printing a Line of Text

Page 4: Week 1 Algorithmization and Programming Languages

Comments

- document program

- improve program readability

- ignored by compiler

- single-line comment starts with // (it terminates at the end of the current line)

- multiple-line comments starts with /* and ends with */

Page 5: Week 1 Algorithmization and Programming Languages

Preprocessor directive

- begins with a hash sign (#)

- processed before the program is compiled

- includes in the program the contents of the input/output stream header file <iostream>

- preprocessor directives (like #include) do not end with a semicolon

Page 6: Week 1 Algorithmization and Programming Languages

int main ()

- a part of every C++ program

- is a program building block called a function

- exactly one function in every program must be main

- C++ programs begin executing at function main, independently of its location within the source code

- the keyword* int that main "returns" an integer

*A keyword is a word in code that is reserved by C++ for a specific use

Page 7: Week 1 Algorithmization and Programming Languages

BODY

- the left brace, {, must begin the body of every function

- the right brace, }, must end each function's body

- the entire line is called a statement

- instructs the computer to perform an action

- C++ statement must end with a semicolon(;)

- output and input are accomplished with streams of characters.

Page 8: Week 1 Algorithmization and Programming Languages

INPUT/OUTPUT

cin- Standard input stream- Normally keyboard

cout- Standard output stream- Normally computer screen

cerr- Standard error stream- Display error messages

Page 9: Week 1 Algorithmization and Programming Languages

Standard output stream objectstd::cout - “Connected” to screen

- Stream insertion operator (<<)

- Value to right (right operand) inserted into output stream

Namespacestd:: specifies using name that belongs to

“namespace” std

std:: removed through use of using statements

Escape characters (\) - Indicates “special” character output

Page 10: Week 1 Algorithmization and Programming Languages

return 0;

The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.

Page 11: Week 1 Algorithmization and Programming Languages

Escape sequence

Description

\nNewline. Position the screen cursor to the beginning of the next line.

\tHorizontal tab. Move the screen cursor to the next tab stop.

\rCarriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line.

\aAlert. Sound the system bell.

\\Backslash. Used to print a backslash character.

\'Single quote. Use to print a single quote character.

\''Double quote. Used to print a double quote character.

Escape sequences

Page 12: Week 1 Algorithmization and Programming Languages

1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements. 3 #include <iostream> 4 5 // function main begins program execution 6 int main() 7 { 8 std::cout << "Welcome "; 9 std::cout << "to C++!\n"; 10 11 return 0; // indicate that program ended successfully 12 13 } // end function main

1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements. 3 #include <iostream> 4 5 // function main begins program execution 6 int main() 7 { 8 std::cout << "Welcome "; 9 std::cout << "to C++!\n"; 10 11 return 0; // indicate that program ended successfully 12 13 } // end function main

Welcome to C++! Welcome to C++!

Multiple stream insertion statements produce one line of output.

Comments

Preprocessor directive

BODY

Page 13: Week 1 Algorithmization and Programming Languages

1 // Fig. 1.5: fig01_05.cpp 2 // Printing multiple lines with a single statement 3 #include <iostream> 4 5 // function main begins program execution 6 int main() 7 { 8 std::cout << "Welcome\nto\n\nC++!\n"; 9 10 return 0; // indicate that program ended successfully 11 12 } // end function main

1 // Fig. 1.5: fig01_05.cpp 2 // Printing multiple lines with a single statement 3 #include <iostream> 4 5 // function main begins program execution 6 int main() 7 { 8 std::cout << "Welcome\nto\n\nC++!\n"; 9 10 return 0; // indicate that program ended successfully 11 12 } // end function main

Welcometo C++!

Welcometo C++!

Using newline characters to print on multiple lines.

Page 14: Week 1 Algorithmization and Programming Languages

Adding Integers

Output:

Page 15: Week 1 Algorithmization and Programming Languages

Standard input stream object:

std::cin

- obtain values typed at the keyboard

- stream extraction operator (>>)

Example:

int number1;

int number2;

cin>>number1>>number2;

Page 16: Week 1 Algorithmization and Programming Languages

Variables

- Location in memory where value can be stored

- Common data types

int – integer numbers (0, 7, 876, 1998)

char – characters ('a', 'G', '#')

double – floating point numbers (0.2, 3.667, 8.65)

Declare variables with name and data type before use

int integer1;

int integer2;

int sum;

Can declare several variables of same type in one declaration

Comma-separated list

int integer1, integer2, sum;

Page 17: Week 1 Algorithmization and Programming Languages

Variables names

Valid identifier

- Series of characters (letters, digits, underscores) that begin with a letter or underscore

- Neither spaces nor punctuation marks or symbols can be part of an identifier

- Cannot match any keyword of the C++

Example:

char abc, _abc, abc3, a3bc, a_3b_c;

- Cannot begin with digit

- Case sensitive

Page 18: Week 1 Algorithmization and Programming Languages

Initialization of variables

When declaring a regular local variable, its value is by default undetermined.

There are two ways to initialize variables in C++:

1. c-like initialization: type identifier = initial_value ;

Example:

int a = 0;

2. constructor initialization: type identifier (initial_value);

Example:

int a(0);

Both ways of initializing variables are valid and equivalent in C++

Page 19: Week 1 Algorithmization and Programming Languages

66

Page 20: Week 1 Algorithmization and Programming Languages

Memory Concepts

- Variable names correspond to locations in the computer's memory.

- Every variable has a name, a type, a size and a value

- When new value placed into variable, overwrites previous value

- Reading variables from memory nondestructive

- Placing a new value into a memory location is said to be destructive

Page 21: Week 1 Algorithmization and Programming Languages

Memory ConceptsMemory Concepts

std::cin >> integer1;Assume user entered 45

std::cin >> integer2;Assume user entered 72

sum = integer1 + integer2;

Page 22: Week 1 Algorithmization and Programming Languages

Fundamental data types

When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way.

The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++

Page 23: Week 1 Algorithmization and Programming Languages

Specifiers

Specifiers modify the meanings of the basic built-in types and expand them to a much larger set. There are four specifiers:

Long

Short

Signed

Unsigned

modify the maximum and

minimum values that a data type

will hold.

modify the maximum and

minimum values that a data type

will hold.tell the compiler how to use the

sign bit with integral types and characters (floating-point

numbers always contain a sign).

tell the compiler how to use the

sign bit with integral types and characters (floating-point

numbers always contain a sign).

Page 24: Week 1 Algorithmization and Programming Languages

Fundamental data types in C++

* The values of the columns Size and Range depend on the system the program is compiled for. The values shown above are those found on most 32-bit systems.

Page 25: Week 1 Algorithmization and Programming Languages

Arithmetic operators

Page 26: Week 1 Algorithmization and Programming Languages

Arithmetic operators Integer division

- yields an integer quotient

- fractional part is discarded (i.e., truncated) no rounding occurs

Example:

int a = 7, b = 4;

cout<<a/b;

Modulus operator (%)

- yields the remainder after integer division

- can be used only with integer operands *

Example:

int a = 7, b = 4;

cout<<a%b;

*Attempting to use the modulus operator (%) with noninteger operands is a compilation error.

1

3

Page 27: Week 1 Algorithmization and Programming Languages

Rules of operator precedence Operators in parentheses evaluated first

Nested/embedded parentheses

Operators in innermost pair first

Multiplication, division, modulus applied next

Operators applied from left to right

Addition, subtraction applied last

Operators applied from left to rightOperator(s) Operation(s) Order of evaluation (precedence)

() Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they are evaluated left to right.

*, /, or % Multiplication Division Modulus

Evaluated second. If there are several, they re evaluated left to right.

+ or - Addition Subtraction

Evaluated last. If there are several, they are evaluated left to right.

Page 28: Week 1 Algorithmization and Programming Languages
Page 29: Week 1 Algorithmization and Programming Languages

REFERENCES:

1. C++ How to Program, By H. M. Deitel

Chapter 1. Introduction to Computers, the Internet and World Wide Web

Chapter 2. Introduction to C++ Programming

2. http://www.cplusplus.com

Page 30: Week 1 Algorithmization and Programming Languages

THANK YOU FOR YOUR ATTENTION