Structured Programming Using C++ (1) : University of Kufa Collage of Mathematics and Computer Science
Structured Programming Using C++ (1) : University of Kufa Collage of Mathematics and Computer Science
9/27/2012
Introduction
C++ programming language was developed by Bjarne Stroustrup in 1983 as a better version of C. It was originally named C with Classes. It was designed to be multi-paradigm language support many different programming styles and constructs: procedural programming, data abstraction, object oriented programming, generic programming.
9/27/2012 2
Analysis
The analysis of an problem begin with determine the inputs and the outputs of the problem and then choose the right process to get the require output according to the problem
9/27/2012
Design
9/27/2012
Algorithm
Is a sequence of precise instructions that leads to a solution .the instructions of an algorithm are expressed in English, a set of instruction must completely and unambiguous.
9/27/2012
Flowchart
A flowchart is a diagram that uses graphic symbols to depict the nature and flow of the steps in a process.
Flowchart (continue)
. Start/ Stop . Processing operation
. Input/ Output
. Decision
. connection
9/27/2012
Statement last; }
9/27/2012
directive always begin with symbol #.The main() is main function, the braces { and } mark the beginning and end of the main part of the program
9
Compiler is a program that translating a high-level language program into a machine language . The way of compile and run C++ depends on the particular system using, it need to give command to compile, link and run a C++ program on your system. When you give the command to compile the program this will produce a machine language of C++ program object code, it must be linked after that you can execute the program.
9/27/2012 10
Debug
Programming is a complex process, it often leads to errors. A mistake in a program is usually called bugs and the process of eliminating and correcting them is called debugging. There are a few different kinds of errors that can occur in a program, and it is useful to distinguish between them in order to correct more quickly. 1. Compile errors(syntax) 2. Logic errors(semantic) 3. Run-time errors
9/27/2012 11
Assignment statement
Variable=expression int x=10; char x=a; float x=1.3; int x(10); x=y+z; average=(m1+m2+m3)/3;
9/27/2012
12
-=
*= /=
expense = 100;
salary*= 0.05 x/=1.9;
9/27/2012
13
9/27/2012
14
Assignment operations
1-Arithmetic operators 2-Comparison operators 3-Logical operators 4-Unary operators
9/27/2012 15
Arithmetic operators
Operator Algebraic C++ expression Example Result
+ * / %
3 1 2 1 1
9/27/2012
16
Comparison operators
Operator C++ expression
= =
a == b a!=b
9/27/2012
17
Logical operators
Operator and or not C++ expression && || !
9/27/2012
18
Unary operators
Operator C++ expression
++ --
++ --
9/27/2012
19
Precedence rules
operators ++,--,() ! *,/,% +,<,<=,>,>=,==,!= && || =,+=,=,*=,/=,%=
9/27/2012
description increment, decrement, parentheses not Multiplication, division, remainder(mod) Addition , subtraction Less than, less than or equal , greater than, greater than or equal ,equal , not equal and or Assign, add and assign, sub and assign, multiple and assign, division and assign ,mod and assign
Highest precedence
Lowest precedence
20
Comment
Comments are ignored by the compiler and thus are not an executable part of the program. Comments are very important in programming languages because they make programs understandable to any one reading the code. One line comment Average = (x+ y)/ 2; // Calculate the average of two numbers The other style is useful for multiline comments. That is you enclose your comments between two characters pairs, namely /* and */. /* Putting an asterisk at the beginning of each line of a multiline comment allow people looking at the code to determine with a quick look what a comment and what is a code. */
9/27/2012 21
Case sensitive
Rate
9/27/2012
22
Identifier
Are used as names for variable and other items in a C++ program. Variable start must start with either a letter or the underscore( _ ) symbol, and remaining characters must all be letter and digit. You can use uppercase or lowercase letters. Example: x1, Y_1, Average, area_2 Do not use special characters such as +, -, *. etc. or reserve words such as int float, main, long, char, etc.
9/27/2012
23
Variables
Every variable must be declare before using it. Variables have unique names and types and hold values that you assign them. You can initailize the variable when it declare. The type of the variable determines the size of variable that stored in computer memory. Syntax: variable_type variable_name; Example : - integer int x; - float float y=3.5; - char char x;
9/27/2012 24
Variable Types
Variable type Single Character Integer Keyword Char int Range -128 to 127 -32,786 to 32,767 Memory Requirement 1 byte 2 bytes Digits of precision __ __
Floating point
Longer integer More accurate Even more accurate floating Larger character set Positive integer Longer positive integers
float
long double long double unsigned char unsigned int unsigned long
3.4e-38 to 3.4e+38
-2,147,483,648 to 2,147,483,647 1.7e-308 to 1.7e+308 3.4e- 4932 to 1.1e+4932 0 to 255 0 0 to to 65,535
4 bytes
4 bytes 8 bytes 10 bytes 1 byte 2 bytes 4 bytes
7
__ 15 19 __ __ __
4,294,967,295
9/27/2012
25
Constant variable
When initialize a variable inside a declaration, you can make the variable is not change inside the program. This variable called constant variable. Syntax: const variable_type variable_name= constant; Example : const double pi=3.14;
9/27/2012
26
Escape sequences
Sign Meaning
9/27/2012
\n \t \v \a \b \\ \? \ \
new line tab vertical tab alert (beep) back space back slash question mark(?) single quote() double quote()
27