Algorithm Design For Sequence Control Structure PDF
Document Details
Uploaded by WellRunEpiphany5727
Regional Science High School for Region IX
Nor Hasnul Azirah Abdul Hamid
Tags
Summary
This document is a lecture presentation on algorithm design, specifically focusing on sequence control structures. It covers topics such as data types, operators, variables, constants, and the fundamental concept of sequences in computer programming.
Full Transcript
Topic 3 : Algorithm Design For Sequence Control Structure CSC121 – Introduction to Algorithm Design and Development Nor Hasnul Azirah Abdul Hamid Table of Contents 01 Understanding about Data 04 Analysis of...
Topic 3 : Algorithm Design For Sequence Control Structure CSC121 – Introduction to Algorithm Design and Development Nor Hasnul Azirah Abdul Hamid Table of Contents 01 Understanding about Data 04 Analysis of Simple Problems Type, Data & Information 02 Operators, Identifiers 05 Algorithm Development for (Variables and Constants) Sequence Control Structure 03 Assignment Statements 01 Understanding about Data Type, Data & Information 1.0 Understanding about 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. 1.1 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 Complex or Composite Data Type Data Type Data that has a single value and Can be broken down further to a cannot be further broken into few primitive data. It means that a small values. complex or composite data are actually a collection of primitive data. 1.2 Standard Data Type Standard Data Types void int char float bool 1.2 Standard Data Type Data type Description ▪ The void type has no values and no operations. void ▪ In other words, both the set of values and the set of operations are empty. ▪ An integer type is a number without a fraction part. ▪ It is also known as an integral number. Integer ▪ C++ supports three different sizes of the integer data type: short int, int and (int) long int (defines these data types so that they can be organized from the smallest to the largest) ▪ A character is any value that can be represented in the computer’s alphabet. ▪ Most computers use ASCII alphabet. Character ▪ Most of the personal, mini-, and mainframe computers use one byte to store (char) the char data types. (Note: A byte is 8 bits. With 8 bits, there are 256 different values in the char set) 1.2 Standard Data Type Data type Description ▪ 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 float 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 values: true and false. (bool) ▪ True (1) and false (0). 02 Operators, Identifiers (Variables and Constants) 2.1 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. 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. ○ 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. 2.2 Variables Variables are memory locations, whose contents can vary or differ over time. The variable name is also called data name or identifier. A variable is capable to store a datum at a time. The content in the variable is changeable. Every computer programming language has its own set of rules for naming variables. 2.3 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. 2.4 Rules of Naming Identifier, Variable and Constant in an Algorithm The name must be one word. ○ Can contain letters, digits, underscores, or other with the exception of space. The name must be meaningful and represents what the content of the variable is. RULES OF Do not create a long name. NAMING Starts with a small letter. When the name is more than one word, suggested to use dash (-) to connect the words or use capital letter for the second word and the next word. Can use abbreviation, but make sure the name has a meaning. 2.5 Operators Operators Arithmetic Relational Logical 2.5.1 Arithmetic Operator Operator Operation One of the most important uses of a + Addition computer is its ability to - Subtraction Integral data type calculate. * Multiplication To perform arithmetic / Division operations (manipulate Floating-point integral and floating-point % Modulus (Remainder) data type data types). -- Decrement by 1 The arithmetic operators: Integral data type ++ Increment by 1 2.5.2 Relational Operator Operator Operation To compare the values < Less than of two operands. Greater than FALSE (0). >= Greater than or equal to The relational == Equal to operators: != Not equal to 2.5.3 Logical Operator When there is more than one relational expression at a time, logical operator Operator Operation are used to perform the && AND evaluation. The evaluation result is || OR either TRUE (1) or FALSE ! NOT (0). The logical operators: 2.5.4 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 && Logical OR || Lowest 03 Assignment Statements 3.0 Assignment Statements A statement is a step in algorithm. It is an instructions that tells the computer what to do. Assignment Purpose: To assign data or value to a Statements variable. It directly assigned a value to the variable or the result of a calculation in the program. 3.0 Assignment Statements We can give value to the variable that stores a numeric value using three ways: Use word set or initialize or use symbol ← or = or assign or let Assign a fixed value to Copy the value of a A result of a a variable variable to another calculation using a variable formula Example : Example : initialize count to zero Example : area = length x width count ← 0 set number2 with number1 multiply variable length count = 0 number2 ← number1 with variable width and set count to zero number1 = number2 store the result into Assign grade with ‘A’ variable area let name to “Hawa Adam” add 5 to variable sum sum = sum + 5 04 Analysis of Simple Problems 4.1 Defining the Problem Involves carefully reading and rereading the problem until you understand completely what is required. Additional information will need to be sought to help resolve any ambiguities or deficiencies in the problem specifications. The problem should be divided into three separate components: INPUT OUTPUT PROCESS A list of the source A list of actions A list of the outputs data provided to the needed to produce the required problem required outputs nouns and adjectives verbs and adverbs 4.2 Designing A Solution Algorithms Once the problem has been properly defined, you usually begin with a rough sketch of the steps required to solve the problem. The 1st attempt at designing a particular algorithm usually does not result in a finished product… ○ Steps may be left out, or some that are included may later be altered or deleted. ○ Do not hesitate to alter algorithms, or even to discard one and start again, if you are not completely satisfied with it. Note: If the algorithm is NOT correct, the program will never be 4.3 Checking The Solution Algorithm After a solution algorithm has been established, it must be tested for correctness. This step is necessary because most logic errors occur during the development of the algorithm, and if not detected, these errors can be passed on the program. It is much EASIER to detect errors in pseudocode than in the corresponding program code. 05 Algorithm Development for Sequence Control Structure 5.1 What is the Control Structure..? In simple term, control structure is the type of algorithm. The structure could be sequence, selection and repetition. Each of these is needed for a different purpose within a program. 5.2 Sequence Control Structure The most common form of control structure – a basic as well as a default program. Example (Pseudocode): Each step is carried out in order of their position and is Start StatementA only done once. StatementB A sequence is basically StatementC where the code just follows End one linear path until it reaches the end. 5.3 Selection Control Structure This control structure allows the program to choose between one of two or more alternative paths of Example (Pseudocode): instructions. Decides a particular answer Start IF condition is true THEN from a set of variable Statement A answers and carries out the ELSE StatementB steps that proceeds it. ENDIF ○ With this structure, you End ask a question, and depending on the answer, you take one of two courses of action. 5.4 Repetition Control Structure Example (Pseudocode): This is basically where a program will repeat a set of instructions until it Start reaches a certain point where an event WHILE condition is true has occurred (the condition is met) StatementA ENDWHILE In a loop, you ask a question; if the End answer requires an action, you perform the action and ask the original question again. If the question’s answer requires that the action be taken again, you take the action and then ask the original question again. 5.5 Combination of Three Structure All logic program can be solved using only these three structures Example (Pseudocode): (sequence, selection and Sequence repetition). do step A do step B The three structures, can if condition C is true then be combined in an infinite do step D Selection number of ways. For else do step E example: there can be a endif sequence of steps while condition F is true then Repetition followed by a selection or do step G repetition followed by sequence. 5.6 How to Write a Detailed Algorithm Using Pseudocode? Described based on the expected screen and a basic algorithm. The algorithm usually contains processing term such as the word calculate. detailed algorithm = basic algorithm + expected screen + calculation process 5.6 How to Write a Detailed Algorithm Using Pseudocode? Example: Start Basic pseudocode: Read length, width Calculate area of a rectangle Display area of a rectangle End Expected screen: 5.6 How to Write a Detailed Algorithm Using Pseudocode? Example: Example: Detailed pseudocode: Flowchart: Start Input: Display “Enter length: ” Read length Display “Enter width: ” Read width Process: area = length x width Output: Display “The area of a rectangle: ”, area End Thanks! Do you have any questions?