Algorithmization and Programming
Examples from the 1-st lecture
----------------------
Example 1
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 1.1:
Input: Deposit, Rate and N_Years (numbers)
Output: Gross_interest
1. Get Deposit, Rate, N_Years
2. Set q = 1 + Rate/100
3. Set Gross_interest = Deposit * q^ N_Years - Deposit
Algorithm 1.2:
Input: Deposit, Rate and N_Years
Output: Table of years' deposits
1. Get Deposit, Rate, N_Years
2. Set q = 1 + Rate/100
3. Repeat N_Years times:
3.1 Set Deposit = Deposit * q
3.2 Print Deposit
--------------------------------------
Example 2
Solve given quadratic equation: ax^2 + bx + c = 0 (ex.: x^2 - 2x - 1 = 0)
* method of completing the square for solving quadratic equations
Algorithm 2.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 2.2
Input: a, b, c ... numbers
Output: x1, x2 ... (complex) numbers
0. Read a, b, c
1. Set D = b*b - 4*a*c
2. Compute R as square root of D
3. Set x1 = (-b+R)/(2*a), x2 = (-b-R)/(2*a)
4. Write x1, x2
Without using complex numbers:
Algorithm 2.3
Input: a, b, c ... numbers
Output: x1, x2 ... numbers, or "Not solvable" answer
0. Read a, b, c
1. Compute D = b*b - 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 = (-b+R)/(2*a), x2 = (-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"
======= MATLAB ===============
Example: write some text on the computer screen
% our first program
disp('Let us start ...')
--------------------------------------------
Algorithm 1.1 rewritten in Matlab:
Algorithm 1.1:
Input: Deposit, Rate and N_Years (numbers)
Output: Gross_interest
1. Get Deposit, Rate, N_Years
2. Set q = 1 + Rate/100
3. Set 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);
------------------------------
Arithmetic operations
+ add
- subtract
* multiply
/ divide
^ power
Logical operations
== equal to ( a == 3, 3 == a )
~= not equal to
< less than ( a < 3 )
<= less than or equal to
> greater than
>= greather than or equal to
Relations
~ not
& and
| or
-------------------------------------------------------------
example:
( a < 3 ) & ( a >= 1 ) ... a is from the interval < 1, 3)
-----------------------
Functions
mathematical:
sin(x), sqrt(D), ...
Input/Output:
n = input('Give a number: ')
disp(n)
--------------------------------
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 1.2:
Input: Deposit, Rate and N_Years
Output: Table of years' deposits
1. Get Deposit, Rate, N_Years
2. Set q = 1 + Rate/100
3. Repeat N_Years times:
3.1 Set Deposit = Deposit * q
3.2 Print 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;
disp(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.')
===================================