6.2. While Examples
6.2.1. String Operations
The string
type represents a sequence of zero or more Unicode characters
(the char
type) with indices. We can therefore access the characters in a
string using array notation with index values. Consider the following method:
1using System;
2namespace IntroCSCS
3{
4 class Ch06WhileLoop
5 {
6
7 static void Main()
8 {
9 Console.Write("Please enter a string: ");
10 string s = Console.ReadLine();
11 OneCharPerLine(s);
12 }
13
14 static void OneCharPerLine(string s)
15 {
16 int i = 0;
17 while (i < s.Length) {
18 Console.WriteLine(s[i]);
19 i++;
20 }
21 }
22 }
23}
We are using (i < s.Length)
because we want to loop through all the characters.
Since array indexing is 0-based, the index of the last character is the
length of the string - 1. In the code here, it is s.Length - 1. To represent this
number, we use:
while (i < s.Length) {
To print out the characters, we loop through the string use the array indices s[i]
:
Console.WriteLine(s[i]);
To print out the characters one by one, we update the local loop variable i
with step of 1:
i = i+1;
or the more commonly used equivalent form:
i++;
6.2.2. Long Term Investment
The idea here is to see how many years it will take for an investment account to grow to at least a given value, assuming a compounded hypothetical annual return rate. Here’s what we will do:
Prompts the user for three numbers:
The monthly investment.
The annual percentage for return as a decimal. For index funds (mutual or ETF, Exchange Traded Funds), it seems to be reasonable to have annualized 5% return.
The final balance desired.
#. Print the initial balance, 0, and the balance each year until the desired amount is reached. Round displayed amounts to two decimal places, as usual.
The math: The amount next year is the amount now times (1 + interest fraction), so if I have $500 now and the interest rate is .04, I have $500*(1.04) = $520 after one year, and after two years I have, $520*(1.04) = $540.80. If I enter into the program a $500 starting balance, .04 interest rate and a target of $550, the program prints:
500.00
520.00
540.80
563.42
6.2.2.1. Strange Sequence Exercise
Save the example program strange_seq_stub/strange_seq.cs in a project of your own.
There are three functions to complete. Do one at a time and test.
Jump
: First complete the definitions of function Jump
.
For any integer n
, Jump(n)
is n/2
if n
is even,
and 3*n+1
if n
is odd.
In the Jump
function definition use an if
-else
statement. Hint [1]
PrintStrangeSequence
:
You can start with one number, say n = 3, and keep applying the
Jump
function to the last number given,
and see how the numbers jump around!
Jump(3) = 3*3+1 = 10; Jump(10) = 10/2 = 5;
Jump(5) = 3*5+1 = 16; Jump(16) = 16/2 = 8;
Jump(8) = 8/2 = 4; Jump(4) = 4/2 = 2;
Jump(2) = 2/2 = 1
This process of repeatedly applying the same function to the most recent result
is called function iteration. In this case you see that iterating the
Jump
function, starting from n=3, eventually reaches the value 1.
It is an open research question whether iterating the Jump function
from an integer n
will eventually reach 1,
for every starting integer n
greater than 1.
Researchers have only found examples of n
where it is true.
Still, no general argument has been made to apply to the
infinite number of possible starting integers.
In the PrintStrangeSequence you iterate the Jump
function
starting from parameter value n
, as long as the current number is not 1.
If you start with 1, stop immediately.
CountStrangeSequence
: Iterate the Jump
function as in
PrintStrangeSequence
. Instead of printing each number in the sequence,
just count them, and return the count.
6.2.2.2. Roundoff Exercise II
Write a program to complete and test the function with this heading and documentation:
/// Return the largest possible number y, so in C#: x+y = x
/// If x is Infinity return Infinity.
/// If x is -Infinity, return double.MaxValue.
/// Assume x is not NaN (which is equal to nothing).
static double Epsilon(double x)
Hint: The non-exceptional case can have some similarity
to the bisection in the best root approximation example:
start with two endpoints, a
and b
, where x+a = x
and
x+b > x
, and reduce the interval size by half….