Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Csc121 - Topic 3 Algorithm Design For Sequence Control Structure - PPTM

Uploaded by

cxs5kn5277
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Csc121 - Topic 3 Algorithm Design For Sequence Control Structure - PPTM

Uploaded by

cxs5kn5277
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

TOPIC 3 : ALGORITHM

DESIGN FOR SEQUENCE


CONTROL STRUCTURE
Data Type, Data & Information

🞂 Data are raw materials entered into a computer to be processed in order


to produce meaningful information.

Data is a collection of unprocessed


items, which can include text,
numbers, images, audio, and video.

Information conveys meaning


and is useful to people.
Data type

🞂 Textual data type consists of number or numeric, letter (small or capital


letter) and special symbols or special characters.
🞂 Textual data can be further divided into two types:
🞂 Simple or primitive data type
🞂 Data that has a single value and cannot be further broken into small values.
🞂 Complex or composite data type
🞂 Can be broken down further to a few primitive data. It means that a complex
or composite data are actually a collection of primitive data.
Data type

🠶 Numeric data can be used in an arithmetic calculation.


🠶 Can be divided into:
🠶 Integer
🠶 A whole number, not involving decimal places.
🠶 Example: 123, 45, 765
🠶 Real number
🠶 Involves decimal places.
🠶 Examples: 1.23, 45.0, 76.5
Data Type

🞂 Alphanumeric data cannot be used in arithmetic calculation.


🞂 Can be divided to a:
🞂 Single character
🞂 A primitive data type that can be used to store one character only such as a
letter, number, punctuation mark or other symbol.
🞂 A string
🞂 A composite data type, a sequence of characters.
🞂 Can be broken down into characters.
🞂 Example data of string is “DM52” and it can be broken down to separate
characters of ‘D’, ‘M’, ‘5’ and ‘2’.
Operators and identifiers
Identifier
• One feature present in all computer languages is the identifier – allow us to name data and
other objects in the program.
• Identifier is used to define names to represent variables, constant and name of
function.
• Each piece of data in the computer is stored at a unique address.
• If we didn’t have identifiers that we could use to symbolically represent data locations, we
would have to know and use data addresses to manipulate them.
• Instead, we simply give data identifier names and let the compiler keep track of where they
are physically located.
• Different programming language use different rules to form identifier.
• In C++, consists of letters (capital letter A through Z, the lowercase letter a to z), digits (0
through 9), and the underscore (no other symbols are permitted to form a identifier.
• Besides, the 1st character cannot be a digit - must be begin with a letter or underscore.
• Good identifier names are descriptive (by combining two or more words) but short.

7
Variables
🠶 Variables are memory locations, whose contents can vary or differ over time.
🠶 Names that refer to memory locations, that can holds values
🠶 It is the ability of memory variable to change in value that makes computers and
programming worthwhile.
🠶 Because one memory location can be used over and over again with different
values, you can write program instructions once and then use them for thousands
of separate calculations.
🠶 Every computer programming language has its own set of rules for naming
variables.
▪ Most language allow both letters and digits within variable names.
▪ Different languages put different limits on the length of variable names,
although in general, newer languages allow longer names.
▪ Many modern languages, such as C++, C# and Java allow more than 200
characters in a variable name. Variable names in these languages usually
consist of
lowercase letters, don’t allow hyphens, but do allow underscores.
▪ Naming variables is case sensitive, so HOURLYWAGE, hourlywage, and
hourlyWage are considered three separate variable names.
▪ Most programmers who use the more modern language employ the format in
which multiple-word variable names are run together, and each new word within
the variable name begins with an uppercase letter (camel casing).
Constant
• Constant are memory location, whose content is not allowed to
change during program execution.
• Holds data that remains the same as the program runs.
• Allow us to give a name to a value that is used several times in a
program.
• Once the variable is declared constant, the value cannot be
changed and the variable cannot be assigned to another value.
10
Rules of naming identifier, variable
and constant
🠶 Even though every language has its own rules for naming variables, when designing the
logic of a computer program, you should not concern yourself with the specific syntax of
any particular computer language
🡪 The logic, after all, works with any language.
🠶 The names follow only two rules:
1. Names must be one word.
▪ The name can contain letters, digits, underscores, or other with the exception of
space.
2. Names should have some appropriate meaning.
▪ This is not a rule of any programming language.
▪ As long as the correct numeric result is placed in the variable, its actual name doesn’t
really matter.
▪ However, it’s much easier to follow the logic of a program if you use appropriate
meaning of variable name.
▪ You might think that you will remember how you intended to use a cryptic variable
name within a program, but six years later when a program requires changes, you and
other programmers working with you, will appreciate clear as well as descriptive
variable names.
11 Standard data types

Standard
Data Types

void int char float bool


12 … Standard data types
Data type Description
void ▪ The void type has no values and no operations.
▪ In other words, both the set of values and the
set of operations are empty.
Integer (int) ▪ An integer type is a number without a fraction
part.
▪ It is also known as an integral number.
▪ C++ supports three different sizes of the integer
data type: short int, int and long int (defines
these data types so that they can be organized
from the smallest to the largest)
Character ▪ A character is any value that can be represented
(char) in the computer’s alphabet.
▪ Most computers use ASCII alphabet.
▪ Most of the personal, mini-, and mainframe
computers use one byte to store the char data
types.
(Note: A byte is 8 bits. With 8 bits, there are 256
different values in the char set)
float ▪ A floating-point type is a number with a
fractional part.
13 … Standard data types
Data type Description
float ▪ A floating-point type is a number with a
fractional part.
▪ The C++ language support three different sizes
of floating-point data types: float, double and
long double (so that can be organized from
smallest to largest).
▪ Although the physical size of floating-point type
is machine dependent, many computer support
the sizes float (4 bytes), double (8 bytes) and
long double (10 bytes).
Boolean ▪ Logical or Boolean data consists of only two
(bool)
A final point tovalues:
remembertrue
about and
all thefalse.
data types that have been discussed is
that each type▪ hasTrue (1)internal
its own and false
format. (0).
Therefore, when the same number
value is stored in different types, the internal bit configuration will be different.
For example: the ASCII character plus (+) has a value of 43, but its bit
configuration is different from the integer 43, which is also different from the float
43.0.
*** One of the your jobs as a programmer is to use these different types
consistently in your programs.
14 Operators

Operators

Arithmetic Relational Logical


15 Arithmetic operator
🞂 One of the most important uses of a computer is its ability to calculate.
🞂 To perform arithmetic operations (manipulate integral and floating-point data
types).
🞂 The arithmetic operators:

Operat Operation
or
+ Addition
- Subtraction Integral data type
* Multiplication
/ Division
Floating-point data type
% Modulus (Remainder)
-- Decrement by 1
Integral data type
++ Increment by 1
16 Relational operator
🠶 To compare the values of two operands.
🠶 The evaluation result is either TRUE (1) or FALSE (0).
🠶 The relational operators:

Operator Operation
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to
17 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).
🠶 The logical operators:

Operator Operation
&& AND
|| OR
! NOT
18 OPERATOR PRECEDENCE
◎ When more than one arithmetic operator is used in an expression, C++
uses the operator precedence rules to evaluate the expression.
Operator Category Operator
(evaluated from left to
right) Highest
Parentheses ()
Multiply, Division, Modulus * / %
Add, Subtract + -
Relational Operators < <= >
>=
Equality Operators == !=
Logical AND && Lowest

Logical OR ||
The multiplication, division, and modulus operators are evaluated
before addition and subtraction operators. Operators having the
same level of precedence are evaluated from left to right.
Grouping is allowed for clarity.
Simple Statements
Simple Statements

🠶 A statement is a step in algorithm.


🠶 It is an instructions that tells the computer what to do.
🠶 Simple statements consist of:
🠶 Input statement
🠶 Output statement
🠶 Assignment statement
Input Statement

🞂 Purpose: To read the data entered by the user and then store
the value(s) into the variable(s) mentioned.
🞂 A variable gets a value using input statement or using
assignment statement.
🞂 Output statement is usually used together with input statement
to guide the user as to what kind of data she/he has to enter.
🞂 The word read or input or get or prompt is used to implement
input statement.
Input Statement (cont.)

🞂 The format to write input statement are as follows:


i. To read one data: Read variable
ii. To read many data: Read variable1, variable2, …
🞂 For example:
i. Display “Enter your name: ”
Read name
ii. Display “Enter the mark: ”
Read mark
Output Statement

🠶 Purpose: To display or convey information to the user through


the computer screen.
🠶 The form to write output statement are as follows:
i. Display a message
• Display any message that we want the user to see on the screen
ii. Display the value of variable(s)
• Display the content of a variable on the screen.
Output Statement (cont.)

🞂 The format to write output statement:


i. Display “what ever message to write”
ii. Display variable
🞂 For example:
Display “The average is: ”, average
Display “My name is: ”, name
Display “Enter your metric number: ”
🞂 The above output statements will display the following information
on the screen:
The average is: 70.00
My name is: Siti Alia
Enter your metric number:
Output Statement (cont.)
🞂 A statement Display newline is a special output statement to
instruct a computer to go to the next line before the content of the
next output statement gets executed.
🞂 For example:
Display “The average is: ”, average
Display newline
Display “My name is: ”, name
Display newline
Display “Enter your metric number: ”
🞂 The above output statements will display the following information
on the screen:
The average is: 70.00
My name is: Siti Alia
Enter your metric number:
Assignment Statement
🠶 Purpose: To assign data or value to a variable.
🠶 It directly assigned a value to the variable or the result of a
calculation in the program.
🠶 We can give value to the variable that stores a numeric value
using three ways:
i. Assign a fixed value to a variable
ii. Copy the value of a variable to another variable
iii. A result of a calculation using a formula
Assignment Statement (cont.)
🠶 Words that represent assignment statement: set or initialize
🠶 Symbols that represent assignment statement: = or
🠶 For example:
i. Initialize count to zero
ii. count 🡨 0
iii.count = 0
iv. Set count to zero
v. Set grade with ‘A’
vi. Set name to “Hawa Adam”
Assignment Statement (cont.)

🠶 We can copy the value of a variable to another variable by


writing these statements:
i. Set number2 with number1
ii. number2 🡨 number1 It will cause the content at
variable number1 to be copied to
location number2
iii.number1 = number2 It will cause the content at
variable number2 to be copied
to variable number1
Assignment Statement (cont.)

🞂 Examples of the assignment statement involving calculation:


i. area = length x width
ii. Multiply variable length with width and store the result into
variable area
iii.Add 5 to variable cnt
iv. cnt = cnt + 5
v. Divide sum with 5 and set the result to average
vi. y =

You might also like