Algorithmization and Programming
Examples from the 1-st lecture
----------------------
Example 1
Solve given quadratic equation: ax^2 + bx + c = 0
* method of completing the square for solving quadratic equations
Algorithm 1.1
Input: quadratic equation
Output: its roots
0. Divide the equation by the coefficient at x^2.
1. Keep all terms containing x on one side.
Move the constant to the right.
2. Take half of the x-term coefficient and square it.
Add this value to both sides.
3. Simplify and write the perfect square on the left.
4. Take the square root of both sides.
Be sure to allow for both plus and minus.
(source: http://www.purplemath.com/modules/sqrquad.htm)
* the quadratic formula
Algorithm 1.2
Input: a, b, c ... (real) numbers - coefficients of the equation
Output: x1, x2 ... (complex) numbers
0. Enter values of a, b, c
1. Set D to b^2 - 4*a*c
2. Compute R as square root of D
3. Set x1 to (-b+R)/(2*a), x2 to (-b-R)/(2*a)
4. Write values of x1, x2
Without using complex numbers:
Algorithm 1.3
Input: a, b, c ... (real) numbers - coefficients of the equation
Output: x1, x2 ... real roots, or "Not solvable" answer
0. Enter values of a, b, c
1. Set D to b^2 - 4*a*c
2. If D < 0
2.1 Set Solvable to false
else
2.2 Compute R as square root of D
2.3 Set x1 to (-b+R)/(2*a), x2 to (-b-R)/(2*a)
2.4 Set Solvable to true
3. If Solvable, then write x1, x2
else write "The equation has not real roots"
--------------------------------------
Example 2
John has a deposit of 8000 Kc with interest rate of 3%,
what would be the gross interest after 7 years?
(Assume the interest is assigned once a year.)
Print the table of deposits, as it changes in every year.
Algorithm 2.1:
Input: Deposit, Rate and N_Years (numbers)
Output: Gross_interest
1. Get Deposit, Rate, N_Years
2. q <-- 1 + Rate/100
3. Gross_interest <-- Deposit * q^ N_Years - Deposit
Algorithm 2.2:
Input: Deposit, Rate and N_Years
Output: Table of years' deposits
1. Get Deposit, Rate, N_Years
2. q <-- 1 + Rate/100
3. Year <-- 0
4. Repeat N_Years times:
4.1 Year <-- Year + 1
4.2 Deposit <-- Deposit * q
4.3 Print Year, Deposit
======= MATLAB ===============
Example: write some text on the computer screen
% our first program
disp('Let us start ...')
--------------------------------------------
Algorithm 2.1 rewritten in Matlab:
Algorithm 2.1:
Input: Deposit, Rate and N_Years (numbers)
Output: Gross_interest
1. Get Deposit, Rate, N_Years
2. q <-- 1 + Rate/100
3. Gross_interest <-- Deposit * q^ N_Years - Deposit
Matlab program:
% Computing of the gross interest:
Deposit = input('Give the deposit:');
Rate = input('Give the interest rate:');
N_Years = input('Give the number of years:');
q = 1 + Rate/100;
Gross_interest = Deposit * q^ N_Years - Deposit;
disp('The gross interest is');
disp(Gross_interest);
------------------------------
Conditional statement:
example: read two numbers and find if they are equal
a = input('Give two numbers');
b = input();
if (a == b)
disp('equal')
else
disp('not equal')
end
--------------------------------
Loops:
A) Number of repetitions is known in advance
------------------------------------------
Algorithm 2.2:
Input: Deposit, Rate and N_Years
Output: Table of years' deposits
1. Get Deposit, Rate, N_Years
2. q <-- 1 + Rate/100
3. Year <-- 0
4. Repeat N_Years times:
4.1 Year <-- Year + 1
4.2 Deposit <-- Deposit * q
4.3 Print Year, Deposit
Rewritten in Matlab:
% Table of years' deposits:
Deposit = input('Give the deposit:');
Rate = input('Give the interest rate:');
N_Years = input('Give the number of years:');
q = 1 + Rate/100;
for Year = 1 : N_Years
Deposit = Deposit * q;
fprintf(' %d %d \n', Year, Deposit);
end
------------------------------------
B) Number of repetitons is not known in advance
example: print the first negative number in a sequence
a = input('Give the first number: ');
while (a>=0)
a = input('Give next number: ');
end
disp(a)
-------------------------
example: find out after how many years the deposit will duplicate
Deposit = input('Give the deposit:');
Rate = input('Give the interest rate:');
q = 1 + Rate/100;
Year = 1;
Double_Dep = 2*Deposit;
while (Deposit < Double_Dep)
Deposit = Deposit * q;
Year = Year + 1;
end
disp('Deposit will duplicate after')
disp(Year)
disp('years.')
===================================