



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
What an algorithm is and its importance in computer programming. It also discusses the properties of an algorithm, including finiteness, definiteness, effectiveness, and input/output. The document also covers conditional statements and iteration in algorithms. It provides examples of algorithms and explains how they are expressed in programming languages. useful for computer science students who want to understand the basics of algorithms and their properties.
Typology: Summaries
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Text:
examination of the latest edition of Webster’s dictionary defines its meaning as “any special method of solving a certain kind of problem.” But this word has taken on a special significance in computer science, where algorithm has come to refer to a precise method useable by a computer for the solution of a problem. This is what makes the notion of an algorithm different from words such as process, technique or method. 1.1. Properties of an Algorithm 1.1.1. Finiteness : An algorithm is composed of a finite set of steps (i.e. they terminate after a finite number of operations), each of which may require one or more operations. 1.1.2. Definiteness : The possibility of a computer carrying out these operations necessitates that certain constraints be placed on the type of operations an algorithm can include. For example, each operation must be definite, meaning that it must be perfectly clear what should be done. Directions such as “compute 5/0” or “add 6 or 7 to x” are not permitted because it is not clear what the result is or which of the two possibilities should be done. 1.1.3. Effectiveness : Another important property each operation should have is that it be definite; each step must be such that it can, at least in principle, be done by a person using pencil and paper in a finite amount of time. Performing arithmetic on integers is an example of an effective operation, but arithmetic with real numbers is not, since some values may be expressible only by an infinitely long decimal expansion. Adding two such numbers would violate the effectiveness property. 1.1.4. Input & Output : An algorithm produces one or more outputs and may have zero or more inputs which are externally supplied. An algorithm is a well-ordered collection of unambiguous, effectively computable instructions that produce a result and halt in a finite amount of time. Computational Procedure There is another word for an algorithm which obeys all of the above properties except termination, and that is computational procedure. One important example of a computational procedure is the operating system of a digital computer. This procedure is designed to control the execution of jobs, such that when no jobs are available, it does not terminate, but continues in a waiting state until a new job is entered. Though computational procedures include important examples such as this one, we will restrict our study to those computational procedures which always terminate. A related consideration is that the time for termination should be reasonably short. For example, an algorithm could be devised which, for any given position in the game of chess, decides if that is a winning position. The algorithm works by examining all possible moves and countermoves that could be made from the starting position. The
When using a list of values, we may use a variable to keep track of which item in the list we are interested in. For example, we may have a variable i that keeps track of which name we want to look at. name [i] would then refer to the value in list name found at the index indicated by the value of i. 2.4. Interacting with the User After an algorithm designer writes an algorithm, a user decides to run the algorithm on a particular computing agent. The computing agent will need to interact with the user at least once, when outputting the result to the user. The agent may also need to receive information from the user. 2.4.1. Output from computing agent to user Any of the following may be used to indicate the agent is outputting information to the user: Output Print Display The command can be followed by text in quotes, which is output verbatim, and variables, whose value is output. Some examples: Output "Please enter an integer" Output "The current counter value is " count 2.4.2. Input from user to agent Input from the user is indicated by Input followed by a list of variables that will store the values input from the user. For example, “Input n” inputs a single value, storing it into the variable named n. 2.5. Changing the value of a variable Algorithms need to be able to change the value of variables. To set the value of a variable to a new value we will use the following command: Set variable To value Where variable is the name of the variable whose value the algorithm is changing and value is an expression whose value should be stored in the variable. 2.6. Expressions. Many times, the expressions set into the variable are mathematical expressions. The usual operators +, - , *, / are available. We also have two other operators, div and mod. The value “x div y” is the integer portion of x / y. The value “x mod y” is the integer remainder of x / y. For example, 7 div 2 is 3 and 7 mod 2 is 1.
Expressions can also be boolean. Boolean expressions have only two possible values, true or false. The operators used in boolean expressions include the usual comparison operators: <, ≤, >, ≥, = and logical operators, AND, OR, NOT. 2.7. Conditional statements To describe a point in the algorithm in which steps are executed only if some condition is true, we use the if statement. if ( condition ) then BEGIN statements... END “statements... ” indicates any statements may be placed between the BEGIN/END. The statements are executed only if the condition expression evaluates to true. One may also want to indicate an alternative set of statements to be executed if the condition evaluates to false. In this case the statement is written as: If ( condition ) Then BEGIN statements... END Else BEGIN statements... END 2.8. Iteration To indicate repetition of statements in an unambiguous way, the algorithm will use one of the following statements: Repeat statements... Until ( condition ) executes the statements inside the Repeat/Until block, then tests the condition. If the condition is false, the statements are executed again. Note that the statements are always executed at least once. RepeatWhile ( condition ) BEGIN statements... END tests the condition. If it is true, the statements inside the BEGIN/END block are executed, and then the condition is tested again. When the condition is false, control falls out of the loop. Example The following algorithm inputs an integer and outputs the values between 1 and that value. Output "Please enter a positive integer value"