Example 1.
Sequential Operations Compute the perimeter p and the area A
of a triangle whose sides are a, b, and c. The formulas are
Pseudocode:
1. Enter the side lengths a, b, and c.
2. Compute the perimeter p.
3. Compute the semiperimeter s.
4. Compute the area A.
5. Display the results p and A.
6. Stop.
The program is
a = input(‘Enter the value of side a: ’);
b = input(‘Enter the value of side b: ’);
c = input(‘Enter the value of side c: ’);
p = a + b + c;
s = p/2;
A = sqrt(s*(s-a)*(s-b)*(s-c));
disp(‘The perimeter is:’)
p
disp(‘The area is:’)
A
Example 2. Conditional Operations Given the (x, y) coordinates of a point,
compute its polar coordinates (r, theta ), where
Pseudocode
1. Enter the coordinates x and y.
2. Compute the hypoteneuse r.
r = sqrt(x^2+y^2)
3. Compute the angle ".
3.1 If x # 0
theta = atan(y/x)
3.2 Else
theta = atan(y/x) + pi
4. Convert the angle to degrees.
theta = theta*(180/pi)
5. Display the results r and theta.
6. Stop.
Octave code
x = input(‘Enter the value of x: ’);
y = input(‘Enter the value of y: ’);
r = sqrt(x^2+y^2);
if x >= 0
theta = atan(y/x);
else
theta = atan(y/x) + pi;
end
disp(‘The hypoteneuse is:’)
disp(r)
theta = theta*(180/pi);
disp(‘The angle is degrees is:’)
disp(theta)
Example 3. Iterative Operations Determine how many terms are required
for the sum of the series to exceed 20 000. What
is the sum for this many terms?
Pseudocode
1. Initialize the total to zero.
2. Initialize the counter to zero.
3. While the total is less than 20 000 compute the total.
3.1 Increment the counter by 1.
k=k+1
3.2 Update the total.
total = 10*k^2 - 4*k + 2 + total
4. Display the current value of the counter.
5. Display the value of the total.
6. Stop.
The following program implements the pseudocode. The statements in the
while loop are executed until the variable total equals or exceeds 2 $ 104.
Octave code
total = 0;
k = 0;
while total < 2e+4
k = k+1;
total = 10*k^2 - 4*k + 2 + total;
end
disp(‘The number of terms is:’)
disp(k)
disp(‘The sum is:’)
disp(total)