LEC3

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 36

Arithmetic

 C++ arithmetic operators:

C++ operator Arithmetic Algebraic C++


operator expression expression
Addition + f+7 f+7

Subtraction - p-c p–c

Multiplication * bm b*m

Division / x/y or x÷y x/y

Modulus % r mod s r%s


Arithmetic cont..
 Parentheses are used in C++ expressions in
much the same manner as in algebraic
expressions. For example, to multiply a times
the quantity b+c we write: a * (b + c)

 C++ applies the operators in arithmetic


expressions in a precise sequence determined by
the following rules of precedence, which are
generally the same as those followed in algebra:
1. Operators in expressions contained within
parentheses are evaluated first.
Parentheses are said to be at the highest
level of precedence. In case of nested or
embedded parentheses, the operators in
the innermost pair of parentheses are
applied first.
Note
 Not all expressions with several pairs of
parentheses contain nested parentheses.

 Ex: a*(b + c) + c*(d + e)

 In the previous example, the parentheses are


said to be on “the same level”.
2. Multiplication, Division and modulus
operations are applied next. If an
expression contains several multiplication,
division and modulus, operators are
applied from left to right.
3. Addition and subtraction operations are
applied last.
Example:
Algebra: z = pr%q + w/x - y
C++: z = p * r % q + w / x – y;
 6 1 2 4 3 5  the
numbers indicate the order in which C++
applies the operators.
Decision making: Equality and
Relational Operators
 introduces: the if structure
– if condition  (True or False)
 Statements

 If statements can be formed using the


equality and relational operators.
Equality and Relational Operators
 All relational Operators same level of
precedence

 Both equality Operators same level of


precedence lower precedence
Standard C++ Equality or Example of C++ Meaning of C++
Algebraic relational condition condition
equality operator Operator
or relational
operator
Relational
Operators
X is greater than
> > X>Y Y
X is less than Y
< < X<Y
X is greater than
≥ >= X >= Y or equal to Y
X is less than or
≤ <= X <= Y equal to Y
Equality
Operators
X is equal to Y
= == X==Y
X is not Equal to
≠ != X != Y Y
Common Programming Errors
 = =, !=, >=, <= with a space in between 
syntax error

 Reversing the order: =! Or =<  normally


a syntax error or certainly a logic error

 Confusing the Equality operator == and the


assignment operator =
Code Example
 The following Example uses 6 if statements
to compare 2 numbers input by the user

 Code Example.

 The using Statement


Code Example
 int num1, num2  multiple declaration

 Cascaded stream extraction or insertion


operators.
– cin>> num1>>num2;

 if structures with single statements


– Multiple statements (more in chap2)
Good Programming Practice
 Indenting the body of an if structure
readability

 No more than one statement per line

 Lengthy statements may be spread over


multiple lines  compilers ignores with
spaces- tabs- and new lines
– Choosing logically the breaking points.
Common programming Errors
 if (condition) ;logic error- not syntax
– 2 consequences

» The if statement’s body considered empty


 As if the if statement does nothing whether the
condition is true or false
» The original if statement’s body statement in
sequence with the if structure always executed.

 Splitting identifiers: ex: main as ma in


Precedence and Associativity
Operators Associativity Type
() Left to right Parentheses
* / % Left to right Multiplicative
+ - Left to right Additive
<< >> Left to right Stream
insertion/extraction
< <= > >= Left to right Relational
== != Left to right Equality
= Right to left Assignment
Summary
 Computer

 Input Unit: Receiving section

 Output Unit: Shipping Unit


 Memory unit: Warehouse section (volatile-
high cost access- limited capacity)
– primary memory

 ALU- Calculations and logical decisions

 Secondary storage: long term storage- High


capacities
 CPU: Administrative section

 A Machine language is directly understood


by a given type of machines

 Machine language codes are constituted of


strings of umbers (ultimately reduced to 0s
and 1s)
 English like abbreviations form the basis of
assembly languages.

 Load- store- add

 Machine language is not directly


understood by a machine
– Need of a translator: the assembler
 Interpreters: programs that directly executes high
level language programs. (run time)

– Utility: development environments

 Compilers:

 A compiled version of a code run more efficiently

 High level languages: English words +


conventional mathematical notions
 C++ environments: edit- preprocess-
compile- link- load and execute

 C++ files once edited must saved with an


extension .c (Unix systems) or .cpp (Dos
systems)

 Compiler: translates C++ code into machine


language (object code)
 Preprocessor: processes specific directives
(preprocessor directives) starting with #.

 Linker: links the object code with the code


for missing functions result: an
executable image

 On Unix systems: compile command: CC


 Loader: takes an executable image from
disk and transfers it to memory

 CPU: Controls the execution

 Errors:
– Execution time or runtime error (divide by 0)
 Divide by 0  fatal error

 Non fatal error

 Cin (standard input stream)


– Usually linked to the keyboard

 Cout (standard output stream)


– Usually linked to the monitor
 Cerr (standard error stream)
– Linked to the monitor
– Prints error messages

 Single line comments //

 Mutli-line comments /* …. */
 #include <iostream>
– Include the contents of the io stream header file

 C++ programs and main function

 << concatenating stream insertion operator

 >> concatenating stream extraction operator


 Variable declarations

 Valid identifiers

 C++ case sensitive

 Variables  memory locations


 Modifying the content of a variable 
destruction

 The Using statement.


Applications
 What does the following prints if any:
– Assume x=2, y=3, the using statement is used.

– cout <<x;
– cout <<x+x;
– cout << “x=:”;
– cout << “x= “<<x;
– cout <<x+y<<“=“<<“x+y”;
– z=x+y;
– cin >>x>>y;
– //cout <<“x+y =“<<x+y;
– cout<<“\n;”;
applications
 Given the algebraic equation y=ax3 + 7,
which of the following if any, are correct
C++ statements for this equations
– y=a*x*x*x+7
– y=a*x*x*(x+7);
– y=(a*x)*x*(x+7);
– y=(a*x)*x*x+7;
– y=a*(x*x*x)+7;
– y=a*x*(x*x+7);
Applications
 Write a program that inputs 3 integers from
the keyboard and prints the sum, average,
product, smallest and largest of these
numbers. The screen dialogue should
appear as follows:
Input three different integers: 13 27 14
Sum is 54
Average is 18
Product is 4914
Smallest is 3
Largest is 27
Applications
 Write a program that reads in the radius of a
circle and prints the circle’s diameter,
circumference and area. Use the constant
value 3.14159 for П. Do these calculations
in output statements. (note use only integer
values for the radius for the moment)
 What does the following code print?

– cout <<“*\n**\n***\n****\n*****\n”;
 Write a program that reads an integer and
determines and prints whether it is odd or
even.
 Write a program that reads in two integers
and determines and prints if the first is a
multiple of the second or not.
 Using only the techniques we learned until
now, write a program that calculates the
squares and the cubes of the numbers from
0 to 6 and uses tabs to print the following.
 Number square cube
 0 0 0
 1 1 1
 2 4 8
 3 9 27 Try to use 1 var only
 4 16 64
 5 25 125
 6 36 216

You might also like