Uploaded by Althimese Anderson

algorithms

advertisement
600.106
Name:
Pre-programming (Algorithmic Thinking)
Intersession 2007
_________________________________________ Date: ________________
Algorithm Creation I
(and programming practice)
Instructions: For each of the following exercises, elaborate the algorithm and corresponding
program in BASIC. Some examples (in pseudocode) are provided. In case of experiencing
problems while making either the algorithm or the program, write down any difficulties you
may have found.
1) Write an algorithm to display a greeting.
2) Write an algorithm to request a person’s name and greet that person.
Begin
Print “name?”
Input name
Print “hello”, name
End
3) Write an algorithm to add any two numbers.
Begin
Print “Value of X”
Input X
Print “Value of Y”
Input Y
R = X+Y
Print “X+Y=”, R
End
4) Write an algorithm to calculate the hypotenuse of a triangle with sides 3 and 4.
Begin
A=3
B=4
C = (A2 + B2 )1/2
Print C
End
5) Write an algorithm to calculate the hypotenuse of a triangle given any two sides.
6) Write an algorithm to calculate the area for a triangle given the length and height.
7) Write an algorithm to indicate if a number is positive or negative.
Begin
Read number
If number >= 0
then print “number is positive”
If number < 0
then print “number is negative”
End
8) Write an algorithm to divide any two numbers, verifying first if the division is
possible.
9) Write an algorithm to print the first 100 natural numbers.
Begin
n=1
While n <= 100
print n
n = n +1
end_while
End
10) Write an algorithm to produce the first 100 odd numbers.
11) Write an algorithm to compute the sum of the first 10 natural numbers.
Begin
n=1
sum = 0
While n <= 100
sum = sum + n
n = n +1
end_while
print sum
End
12) Write an algorithm to compute the average of several numbers.
13) Write an algorithm to print the first 15 numbers of this series: 1,1,2,3,5,8,13,21…
14) Write an algorithm raise any number to the third power
Begin
Read base
exponent = 3
power = 1
While exponent > 0
power = power * base
exponent = exponent –1
end__while
Write power
End
15) Write an algorithm to raise any number to any power.
16) Given an amount in seconds, write an algorithm to transform it into days, hours,
minutes and seconds.
17) Write an algorithm to describe what a cashier does.
Begin
total = 0
While items in the cart
Read item’s price
total = total + price
end_while
tax = 0.05*total
total = total + tax
Print total
End
18) Write an algorithm to describe what a fueling pump does.
19) Given the sides of a parallelogram, write an algorithm to determine its type.
Begin
Read base
Read height
If base = height
then Write “square”
else
Write “rectangle”
End
20) Given the sides of a triangle, write an algorithm to determine its type.
21) Write an algorithm to determine which value is the greatest of three numbers.
Begin
input A
input B
input C
if (A > B) AND (A > C)
write A
else if (B > A) AND ( B > C)
write B
else if (C > A) AND (C > B )
write C
End
22) Write an algorithm that reads 10 numbers and determines which is the greatest.
Download