Algorithms

advertisement
Algorithms
1. Origin: The origin is the name of a town Khowarizm, a small Soviet city of Khiva. An author who lived
in the town wrote a textbook “Rules of Restoration and Reduction”.
2. Definition: It is a sequence of instruction to solve a certain task(s) with the following characteristics
a. Clear: Each instruction is precisely defined. It is a simple action or composite actions to be
executed with rigorous and unambiguous.
b. Exit: The execution of the algorithm must be terminated after a finite number of executions of
instructions.
c. Input: the information provided to some instructions before they are executed.
d. Output: The result of the task(s). If it is not expected for a given input, the algorithm has an
error. Hence programmer needs to correct it.
3. Example: Euclid’s algorithm
- Input: To positive integers m and n.
- Output: The greatest common divisor ( = GCD(m, n)). It is defined as the largest positive integer
which divided both m and n.
- Process:
i.
ii.
iii.
-
// Find the remainder: Divide m by n and save the remainder in r.
//Check the remainder with zero: If r= 0, then n is answer; Exit the algorithm
//Reset for the next iteration: Move the value of n into m and r into n, then go back to
step i.
Exit: This is an algorithm because it satisfies all the characteristics of an algorithm. However, the
exit property is not trivial. The designer must prove that the loop will eventually satisfy the
condition ‘r=0’ for ALL input, i.e., m and n are positive integer.
4. From Algorithm to Pseudo-code:
The process can be rewritten to be a pseudo-code -- a Pascal - alike or any high-level language
without a rigid of syntax.
a. Input two positive integer m and n
b. Repeat {
Set r be the remainder of m by n,
(Step i)
M  n; n  r;
(Step iii)
} until( r = 0);
( Part of Step ii)
c. Output n as the GCD of the two original inputs m and n. (Part of Step ii)
5. Java Coder:
From the above pseudo-code, Java coder might try to translate without effort it into a Java code.
Here are some notes:
- Step a. requires m and n are positive integers. Coder must know how to test and
detect the inputs that are not positive integers.
- Step b. is a Repeat-until loop. It can be changed to a do-while loop by using the
negation of the exit condition.
Do {
………………
………………
} while ( r<>0);
6. Source Code and Execution
In the source code, some comments are added such as name of author, date, and function.
Furthermore the comments might be the verbal description of the steps.
class Euclid
{
public static void main(String[] args)
{
//Read m and n
int m = 10;
int n = 5;
int r;
//Loop to find GCD
do {
r = m % n;
m = n;
n = r;
System.out.println("m = " + m + "," + "n = " + n); // for testing only
} while ( r != 0);
System.out.print("GCD = " + n);
} // main
} // class Euclid
Compile and run we get:
A:\>javac Euclid.java
A:\>java Euclid
m = 5,n = 0
GCD = 0
A:\>
NOTE: THERE IS A BUG IN THE PROGRAM. THE ANSWER SHOULD BE 5 INSTEAD OF 0.
Question 1: There is a flaw in this pseudo-code. Can you find it? Please answer this question.
Answer: _________________________________________________________________________
________________________________________________________________________________
Question 2: You notice that there is a little bit change from the original algorithm in this pseudo-code.
What is (are) the change(s)? Answer this question.
Answer: _________________________________________________________________________
________________________________________________________________________________
7. From Algorithm to Pseudo-code With a Fix:
Here is the closest translation of the algorithm:
a.
b.
c.
Input two positive integer m and n
Set Exit to be false
Repeat {
Set r be the remainder of m by n,
If (r == 0)
Then {Exit = True; Output n as GCD}
Else { M  n; n  r}
(Step i)
(Part of Step ii)
(Step iii)
} until( r = 0);
( Part of Step ii)
Output n as the GCD of the two original inputs m and n.
Java Code and Execution:
//S Pham
//Comp 110
//2/20/200
//Function: Find the GCD of two positive integers m and n
import java.io.*;
import java.lang.*;
class Euclid
{
public static void main(String[] args)
{
//Read m and n
int m = 10;
int n = 5;
boolean exit = false;
int r;
//Loop to find GCD
do {
r = m % n;
if (r == 0)
{ exit = true;System.out.print("GCD = " + n);}
else {m = n; n = r;}
} while (exit==false);
} // main
} // class Euclid
Compile and run we have:
A:\>java Euclid
GCD = 5
A:\>
8. Generalized Code:
After the main task is complete, you are going to extend the code to read m and n from keyboard.
Furthermore you want to make sure that m and n are two positive integers. Furthermore, You
must send request messages for input as well as the labels for output.
Lab 4
Lab 4 includes the following works:
-
Answer Questions 1 and 2.
Generalized Euclid Code to include input via keyboard.
Choose two of the following problems, then solve them. Follow steps 3-8 to make a report.
a. Generate a Fibbonacci Sequence of the numbers less than 1000. It is a sequence of
integers generated by the following algorithm:
 The first two numbers are both 1.
 The number afterward is the sum of the first two previous numbers.
b.
Given a real number real. Print it with a rounded two-decimal form. Here is an
algorithm to solve it.
 // Move the decimal point two places to the right.
 // Add .5 to it.
 // Drop the decimal part
 // Move the decimal point two places to the left to get the answer.
Here are two examples:
1.3333333  133.33333  133.53333  133.  1.33
1.6666666  166.66666  167.16666  167.  1.67
c. Find the least common multiple (LCM) of two positive integers m and n.
Important note: If you decide to solve all three new problems a-c, you will receive
50% bonus.
Download