Game Development Reference
In-Depth Information
4.
Cuneiform
(a) Write a method
Stripes
with a number as a parameter (you may assume that
this parameter is 0 or larger). The method should give as a result a string
with as many vertical dashes as the parameter indicates. For example, the
call
this
.Stripes(5)
results in
"|||||"
.
(b) Write a method
Cuneiform
with a number as a parameter. You may assume
that this parameter will always be 1 or bigger. The method should give as a
result a string containing the number in a cuneiform notation. In that nota-
tion, every number is represented by vertical dashes and the digits are sepa-
rated by a horizontal dash. Horizontal dashes are also placed at the beginning
and at the end of the string. Here are a few examples:
•
this
.Cuneiform(25)
results in
"
−
||
−
|||||
−
"
•
this
.Cuneiform(12345)
results in
"
−
|
−
||
−
|||
−
||||
−
|||||
−
"
•
this
.Cuneiform(7)
results in
"
−
|||||||
−
"
•
this
.Cuneiform(203)
results in
"
−
||
−−
|||
−
"
Hint: deal with the last digit first and then repeat for the rest of the digits.
5.
Sequences
(a) Write a method
To t a l
with a number
n
as a parameter that returns the total of
the numbers from 0 until
n
as a result. If
n
has a value smaller than or equal
to 0, the method should return 0.
(b) The factorial of a natural number is the result of the multiplication of all
the numbers smaller than that number. For example, the factorial of 3 equals
1
×
×
=
6. Write a method
Factorial
which calculates the factorial of its
parameter. You may assume that the parameter always is larger than or equal
to 1.
(c) Write a method
Power
that has two parameters: a number
x
and an expo-
nent
n
. The result should be
x
n
,so
x
is multiplied
n
times with itself. You
may assume that
n
is a positive integer. The method should also work if
n
equals 0, and if
x
isn't an integer number.
Hint: use a variable for calculating the result, and don't forget to give that
variable an initial value!
(Note: many programming languages have the power operator
^
. C# doesn't
have this operator, although there is a method
Math.Pow
. You are not allowed
to use that method here otherwise this exercise would be too easy :) ).
(d) We can approximate the hyperbolic cosine of a real number
x
as follows:
2
3
x
2
2
x
4
4
x
6
6
x
8
8
x
10
10
1
+
+
+
+
+
!
+···
!
!
!
!
In this case, the notation 6
means factorial of 6. Write a method
Coshyp
that
calculates this approximation by summing 20 of these terms and returning
that value as a result.
!