0% found this document useful (0 votes)
6 views35 pages

3 Topic 3 Basic Elements Part 3

Uploaded by

2024474188
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views35 pages

3 Topic 3 Basic Elements Part 3

Uploaded by

2024474188
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

TOPIC 3

BASIC ELEMENTS OF A COMPUTER PROGRAM PART 3


Operators
• C++ has the following operators –
• Arithmetic operator
• Relational operator
• Logical operator
Arithmetic Operator
Operator Operation
• To perform arithmetic operations
+ Addition

• The only surprise in the table - Subtraction


might be the modulus operator
which is a division operation * Multiplication
where two integers are divided
and the remainder is the result / Division

% Modulus
• For example, the expression (Remainder)
• 10 % 3 results in 1, -- Decrement by 1
• 12 % 7 results in 5, and
• 20 % 5 results in 0 ++ Increment by 1
Relational Operator
• To compare the values of Operator Operation

two operands. < Less than


• The evaluation result is
either true (1) or false <= Less than and equal
to
(0). > Greater than

>= Greater than and


equal to

== Equal to

!= Not equal to
Relational Operator
(Cont…)
• Examples
• if (thisNum < minimumSoFar)
minimumSoFar = thisNum
• if (job == Teacher) salary = minimumWage
• if (numberOfLegs != 8) thisBug = insect
• if (degreeOfPolynomial < 2) polynomial = linear
• if (surf == up) myLocation = oughtaHere
Relational Operator
(Cont…)
The Truth Value of Expressions

bodyTem
bodyTemp < 100.0 bodyTemp == 100.0 bodyTemp>=100.0
p

not zero (the zero (the expression is zero (the expression is


98.7
expression is true) false) false)

zero (the expression is not zero (the not zero (the


100.0
false) expression is true) expression is true)

zero (the expression is zero (the expression is not zero (the


105.3
false) false) expression is true)
Logical Operator
• When there is more than one relational
expression at a time, logical operator are
used to perform the evaluation.
• The evaluation result is either true (1) or
false (0)
Logical Operator
(Cont…)

Logical Operators
Operator Meaning Example of Use Truth Value
&& AND (exp 1) && (exp 2) True if exp 1 and exp 2 are BOTH true.
True if EITHER (or BOTH) exp 1 or exp
|| OR (exp 1) || (exp 2)
2 are true.
Returns the opposite truth value of exp 1;
! NOT ! (exp 1) if exp 1 is true, ! (exp 1) is false; if exp 1
is false, ! (exp 1) is true.
Logical Operator
(Cont…)
• Examples of expressions which contain relational and
logical operators

• if ((bodyTemp > 100) && (tongue == red)) status =


flu;
• if ((numeratorDF == even) || (denominatorDF ==
even)) formula = 26;
• if (((clock == 9) && (sun != shining))&& (locatuion
== US)) time = pm;
Operator Precedence
Operator Category Operator (evaluated
from left to right)
Parentheses ()
Multiply, Division, * / %
Modulus
Add, Subtract + -
Relational Operators < <= >
>=
Equality Operators == !=
Logical AND &&
Logical OR ||
Arithmetic Expression
• Is a combination of operator and operands.

operand operator operand

• Is not a complete statement but it is used in a statement

• Mainly used to calculate or process data in a program (arithmetic and logical


expression)

• Examples of arithmetic expressions

• pi * radius * radius
• 0.5 * acceleration * time * time + initialVelocity * time + initialPosition
• 4.0 / 3.0 * pi * radius * radius * radius
• twentyFourHourTime % 12
• 1./2.* base * height
Assignment Statement
• It is used to assign a value to a variable
eg : num1 = 2 + num2;
area = length * width;

• Syntax :-
identifier = expression;
must be a variable can be constant, variable or any complex expression

• The assignment operator used is equal sign (=)


String input statement
• Use string predefined functions (string.h) and must
be included in the preprocessor directive
command.
• eg : #include <string.h>
• The syntax :-
• cin.getline (variable name , length); eg :
char name[20];
cin.getline (name, 20);
String input statement
#include <iostream.h>
#include <string.h>

int main ()
{
char name[20];
cout << "What's your name?";
cin.getline (name, 4);
cout << "Hello " << name << "\n";
return 0;
}

Output
If u enter Ahmad
Output will be:
Hello Ahm
String Library Function
• Use string predefined functions (cstring) and must be
included in the preprocessor directive command.
• eg : #include <cstring>

• most commonly used string functions :-


• strcpy()
- to copy (to assign) a string into another string variable.
- syntax : - strcpy(destination array, source
array);
• strcmp()
- to compare between two string variables.
- syntax :- strcmp(s1, s2);
if s1=s2 then return value is 0
String Library Function
(Cont…)
• strlen()
- will return the length of string
variable
- syntax :- strlen(string variable);
• strcat()
- will concatenate two string
variables together.
- syntax :- strcat(s1, s2);
Examples of String Library
Function
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
cout << "str1: "<< str1 << "\nstr2: " << str2 << "\nstr3: " <<str3;
return 0;
}

Output
str1: Sample string
str2: Sample string
str3: copy successful
Examples of String Library Function
#include <iostream>
#include <cstring>
using namespace std;

int main ()
{
char str1[20];
char str2[20];
strcpy (str1,"To be ");
strcpy (str2,"or not to be");
strcat (str1, str2);
cout << str1;
return 0;
}

Output
To be or not to be
Examples of String Library
Function
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char szKey[] = "apple";
char szInput[80];

cout<< "Which is my favourite fruit? ";


cin >> szInput;

if (strcmp (szKey,szInput) == 0)
cout<< "Same fruit!\n";
else
cout << "Different fruit";
return 0;
}
Examples of String Library
Function
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char name[40];

cout << "Enter a string:" << endl;


cin.getline(name,40);
cout << "Its length is: " << strlen(name) << " characters\n";
return 0;
}

Output
Enter a string : I love banana
It’s length is: 13 characters
Functions (continued)
• Functions

• Called modules

• Like miniature programs

• Can be put together to form a larger program

C++ Programming: From Problem Analysis to


21
Program Design, Third Edition
Predefined Functions
• In algebra, a function is defined as a rule or
correspondence between values, called the
function’s arguments, and the unique value of the
function associated with the arguments
• If f(x) = 2x + 5, then f(1) = 7, f(2) = 9, and f(3) = 11
• 1, 2, and 3 are arguments
• 7, 9, and 11 are the corresponding values

C++ Programming: From Problem Analysis to


22
Program Design, Third Edition
Predefined Functions
(continued)
• Some of the predefined mathematical functions are:
sqrt(x)
pow(x,y)
floor(x)
• Predefined functions are organized into separate
libraries
• I/O functions are in iostream header
• Math functions are in cmath header

C++ Programming: From Problem Analysis to


23
Program Design, Third Edition
The Power Function (pow)
• pow(x,y) calculates xy, pow(2,3) = 8.0

• pow returns a value of type double

• x and y are called the parameters (or arguments)


of the function pow

• Function pow has two parameters

C++ Programming: From Problem Analysis to


24
Program Design, Third Edition
The sqrt and floor
Functions
• The square root function sqrt(x)

• Calculates the non-negative square root of x, for x


>= 0.0

• sqrt(2.25) is 1.5

• Type double

• Has only one parameter

C++ Programming: From Problem Analysis to


25
Program Design, Third Edition
The sqrt and floor
Functions (continued)
• The floor function floor(x)

• Calculates largest whole number not greater than x

• floor(48.79) is 48.0

• Type double

• Has only one parameter

C++ Programming: From Problem Analysis to


26
Program Design, Third Edition
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double num1, sqroot, result;

cout << "Enter a number:" << endl;


cin>>num1;
sqroot = sqrt(num1);
result = pow(num1,2);
cout<<"Square Root = "<<sqroot<<endl;
cout<<"Power= "<<result<<endl;
return 0;
}
Formatting Numeric
Output
• Use stream manipulators Manipulators Action
library (iomanip) and
must be included in the setw(n) Set the field
preprocessor directive width to n

command setprecision(n) Set the decimal


• eg : #include point precision
to n places
<iomanip> setiosflags(ios::fixed) Display the
number in
• Commonly used numeric conventional
fixed-point
formatting :- decimal
notation.
setiosflags(ios::show Display a
point) decimal point of
that number
Examples of Formatting

#include <iostream.h>
#include <iomanip.h>
int main () {
cout << "*" << "Hi there!" << "*" << endl;
cout << "*" << setw(20) << "Hi there!" << "*" << endl;
cout << "*" << setw(3) << "Hi there!" << "*" << endl;
return 0;
}

Output
*Hi there!*
* Hi there!*
*Hi there!*
Examples of Formatting

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
float real = 12.3443;
cout << setprecision(2)<< real << "\n";
cout << setprecision(4)<< real;
return 0;
}

Output
12
12.34
Examples of Formatting

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float real = 12.344;
cout << setiosflags(ios::fixed)<< real << "\n";
cout << setiosflags(ios::showpoint)<< real ;
return 0;
}

Output
12.344000
12.344000

You might also like