chapter 2 wk3
TRANSCRIPT
CHAPTER 2CHAPTER 2COMPONENTS OF A COMPONENTS OF A
PROGRAMMING PROGRAMMING LANGUAGELANGUAGE
INTRODUCTION TO INTRODUCTION TO COMPUTER COMPUTER
PROGRAMMINGPROGRAMMING(CSC425)(CSC425)
CONTENTS
Arithmetic Expressions Operators
Addition, subtraction,multiplication, division, modulus
Binary and unary operator Negation operator
2CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
3
Variables and constants of integral and floating point
types can be combined into expressions using
arithmetic operators.
In C++, we have to represent the algebraic
expression by using the valid syntax.
ARITHMETIC EXPRESSION
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
4
Is the most basic C++ statement Use to store the value of an expression in a variable Use Assignment Operator (=) Syntax:variable = <variable|constant|expression>
Expression can be :i. a constantii. another variableiii. an arithmetic expressioniv. a function
Assignment statement
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
5
Examples:
length = oldLength;width = 50;area = length * width;moredata = true; //a boolean variablex = total ( );
Assignment statement
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
ARITHMETIC OPERATOR
Generally, there are three types of operator: Unary Binary Ternary
These term reflect the number of operands an operator requires.
Unary operators only require a single operand. Ex: -7 Represent the value negative 7 The literal 7 is preceded by the minus sign, is
called the negation operator. Since it only requires one operand, it is a unary
operator
6
ARITHMETIC OPERATOR CONT…
Binary operator work with two operands Ex: total = cost + tax;
Average = total / 3; Ternary operator work with three operands
Ex: total = num1 + num2 + num3;
7
8
Assignment Operation
Variations of Assignment Operation : We can have assignment statement like this :
sum = sum + 10; This statement can be written using the following
shortcut assignment operators :
+= -= /= %= *=
Hence, the equivalent “sum = sum + 10”, would be :
sum += 10;
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
9
ARITHMETIC EXPRESSION Example :
Expression Equivalent to
price = price * rate; price *= rate;
count = count + 3 count += 3
price *= rate + 1 price = price * (rate + 1)
but not
price = price * rate + 1
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
10
Assignment Operation For a variable to increase or decrease by 1, C++ provides two
unary operators :
(++) : increment operator
(--) : decrement operatoro Increment & Decrement Operators:
Expression Equivalent to
i = i + 1 i++ (postfix increment operator)
If x = 5; and y = x++;
After the second statement y is 5 and x is 6
i = i - 1 i--
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Arithmetic Operators Defined in C++ Example
Addition + 20.9 + 3 = 23.9, or 20 + 3=23
Subtraction - 50 – 10 =40, or 50.4-10= 40.4
Multiplication * 10*3=30, or 10.5*3= 31.5
Division / 10/3 = 3 , or 10.0/3= 0.3333
Modulus
(remainder)
% 10%3=1
Never use the modulus with floating-
point values.
Operators
12
Mathematical Library Functions
Header file #include <math.h> used in the following form :
function_name(argument)
Example :sqrt(x) //square root of xpow(a,2) //a2
fabs(2.3 * 4.6) //absolute value |x|tan(x) //tangent of xfloor(x) //largest integer <= x
More reference at:http://www.cplusplus.com/reference/cmath/floor/
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Arithmetic Operations Examples
#include<iostream.h>
void main (){
int number1, number2;
int sum, different, product, quotient, remainder ;
number1 = 8;
number2 = 4;
sum = number1 + number2;
different = number1 - number2;
product = number1 * number2;
quotient = number1 / number2;
remainder = number1 % number2;
cout<<"sum : " <<sum<<endl;
cout<<"different: " <<different<<endl;
cout<<"product : " <<product<<endl;
cout<<"quotient : " <<quotient<<endl;
cout<<"remainder : " <<remainder<<endl;
}
13
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Output:
Integer Division
IF two integers are divided, the result will be an integer number. Any fraction will be truncated.
int / int int
Example:
int a = 5, b = 2;float c;c = a / b; //the new value of c is 2
cout << a / b; //2 will be displayed
14
Arithmetic Operations
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
When more than one operator will be executed to determine the result, C++ executes the arithmetic operators in the following order:
Operator (s) Operation Precedence
( ) parentheses Evaluated first, inside-out if nested or left to right if same level
*,/,% Multiply, Divide, Modulus
Evaluated second, left to right
+, - Add, Subtract Evaluated last, left-to-right
Precedence & Associativity
Example:a) 3 * 7 – 6 + 2 * 5 / 4 + 6
means( 3 * 7 ) – 6 + ( ( 2 * 5 ) / 4 ) + 6
= 21 – 6 + ( 10 / 4 ) + 6 ( evaluate * )= 21 – 6 + 2 + 6 ( evaluate / )= 21 – 6 + 8 ( evaluate + )= 21 + 2 ( evaluate +)= 23 ( evaluate +=
result)
Precedence & Associativity
b) 3 + 4 * 5 means
3 + ( 4 * 5 )= 3 + 20 ( evaluate * )= 23 ( evaluate + )
c) 2 * 3 / 2 (evaluate operators from
left to right)= 6 / 2
(evaluate * )= 3 (evaluate / )
Precedence & Associativity
Write a program that does the following :
1. Find the sum, subtract and average of two integers
18
Exercise 1
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Try to write a program : Convert Length
Write a program that takes as input given lengths expressed in meters and millimeters. The program should then convert and output the length in centimeters. Assume that the given lengths in meters and milimeters are integers.
19
Exercise 2
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
20
Translate the following flow chart into source code:
Begin
End
Read total
Read count
Display average
average = total / count
Exercise 3
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING