You are on page 1of 3

Computer

Programming

Bachelor in Aerospace Engineering


2012-2013

Exercises Block 3
1.

Implement the following algorithms:

Number rounder
1) Ask the user for a number n.
2) Round up n and store it in u.
3) Round down n and store it in d.
4) Round n to the closest integer and store it in r.
5) Print:
/n\ = u
\n/ = d
|n| = r

n = input('Enter a number: ');
u = ceil(n);
d = floor(n);
r = round(n);
disp(['/n\ = ' num2str(u)]);
disp(['\n/ = ' num2str(d)]);
disp(['|n| = ' num2str(r)]);


Mini poll
1) Ask the user for their name name.
2) Ask the user for their favorite number n.
3) Calculate the square root sqrtN of n: sqrtN =
4) Print:
Hello name! I also like the number n very much, even though its square root is sqrtN.

name = input('Enter your name: ', 's');
n = input('Enter your favorite number: ');
sqrtN = sqrt(n);
disp(['Hello ' name '! I also like the number ' num2str(n) ...
' very much, even though its square root is ' num2str(sqrtN) '.']);


Force calculator
1) Ask the user for a mass value m measured in Kg.
2) Ask the user for an acceleration value a measured in m/s2.
3) Calculate the force value F:
F = m*a
4) Print:
F = m*a = m*a = F

m = input('Enter mass (Kg): ');
a = input('Enter acceleration (m/s^2): ');
disp(['F = m*a = ' num2str(m) '*' num2str(a) ' = ' num2str(m*a)]);


Quadratic equation solver
1) Ask the user for a value a.
2) Ask the user for a value b.
3) Ask the user for a value c.
4) Calculate the roots x1 and x2 of the quadratic equation ax2 + bx + c = 0:
1 / 3

Computer Programming

Bachelor in Aerospace Engineering


2012-2013


+ ! 4

2
! 4
x2 =

2
5) Print on the screen the message:
x1 = x1; x2 = x2
x1 =


a = input('Enter ''a'': ');
b = input('Enter ''b'': ');
c = input('Enter ''c'': ');
x1 = (-b + sqrt(b^2 - 4*a*c))/(2*a);
x2 = (-b - sqrt(b^2 4*a*c))/(2*a);
disp(['x1 = ' num2str(x1) '; x2 = ' num2str(x2)]);


Distance between points calculator
1) Ask the user for a value x1.
2) Ask the user for a value y1.
3) Ask the user for a value x2.
4) Ask the user for a value y2.
5) Calculate the distance d between the points (x1, y1) and (x2, y2):
d = ( )! + ( )!
6) Print:
A = (x1, y1)
B = (x2, y2)
d(A, B) = d

x1 = input('Enter ''x1'': ');
y1 = input('Enter ''y1'': ');
x2 = input('Enter ''x2'': ');
y2 = input('Enter ''y2'': ');
d = sqrt((x1 - x2)^2 + (y1 - y2)^2);
disp(['A = (' num2str(x1) ', ' num2str(y1) ')']);
disp(['B = (' num2str(x2) ', ' num2str(y2) ')']);
disp(['d(A, B) = ' num2str(d)]);


2.

Write some valid code using the following functions (extracted from the Important
Mathematical Functions notes):

Y = exp(X)
R = rem(X, Y)
Y = nthroot(X, n)
G = gcd(A, B)
exprOut = int(expr, n1, n2)
Y = cos(X)
Y = cosd(X)

disp(['e^(pi) = ' num2str(exp(pi))]);
disp(['10 mod 5 = ' num2str(rem(10, 5)) '; 10 mod 4 = '
num2str(rem(10, 4))]);
cubeRoot = nthroot(27, 3)
disp(['gcd(25, 20) = ' num2str(gcd(25, 20))]);
syms x
integral = int(x^2 + 5*x -3, 2, 0)

2 / 3

Computer Programming

Bachelor in Aerospace Engineering


2012-2013


cosRadians = cos(pi)
cosDegrees = cosd(180)

3 / 3

You might also like