CS计算机代考程序代写 c# KIT506 Programming Practical 4: Repeat while something is true

KIT506 Programming Practical 4: Repeat while something is true

KIT506 Programming Practical 4: Repeat while something is true

[ ↔ Expand↶ Close all sections ] Only visible sections will be included when printing this document.

Aims:

to practise reading for loops; and
to implement a while loop.

1 Tracing loops (< 10 minutes) 1.1 Repeated multiplication Read the following C# statements. const int TIMES = 3; int n = 1; for (int i = 0; i < TIMES; i++) { n = n * 2; } Console.WriteLine("After loop, n is " + n); How many times will the for-loop be executed? Three times, for i equal to 0, 1 and 2. What would the output be if the statements were executed? The output is “After loop, n is 8”, because the loop executes three times, each time doubling the previous value of n. 1.2 Countdown Now examine the following code and determine what will be displayed at the end: const int START = 10; //countdown starts at 10 const int END = 0; //countdown ends on zero string output = ""; //output message we will construct for (int i = START; i >= END; i–)
{
//a += b; is the same as a = a + b;
output += i + “, “;
}
output += “blast off!”; //add final message

Console.WriteLine(output);

First, what’s happening with the loop? It’s counting backwards from 10, all the way down to zero (because the test >= includes the value of END). So i will be 10, 9, 8, …, 0.

What’s happening inside the loop?

Each time through the loop it appends the value of i and the text “, ” to the end of output. Here’s how the value of output changes at each iteration:

i Value of output after loop body executes
10 “10, ”
9 “10, 9, ”
8 “10, 9, 8, ”

1 “10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ”
0 “10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, ”

So what is printed in the end?

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, blast off!

Because just after the loop the code also appends the text “blast off!” to the string that it has been constructing.

2 While you’re here

We’re going to adapt the if that you created back in Programming Practical 2 to place it in a loop. Here’s the starting code, which doesn’t include a loop:

int n;

Console.Write(“Enter a positive integer: “);
n = Int32.Parse(Console.ReadLine());

if (n % 2 == 0) //n is even
{
n = n / 2;
}
else
{
n = 3 * n + 1;
}

Console.WriteLine(“The final value of n is ” + n);

The modified rules we’re going to apply are these:

Take any positive integer n (provided by the user).
If n is even, divide it by 2 to get n / 2.
If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. (Which is what you did in Week 3)
Repeat the process indefinitely, stopping if n becomes 1.

The Collatz conjecture is that no matter the starting value of n this sequence will always become 1 eventually.

Wrap the if-else is a while loop that repeats while n is not equal to 1. And add another WriteLine() as the first instruction inside the loop to display the current value of n. Run your code with various values of n to see what happens.

int n;

Console.Write(“Enter a positive integer: “);
n = Int32.Parse(Console.ReadLine());

while (n != 1)
{
Console.WriteLine(“n is ” + n);
if (n % 2 == 0) //n is even
{
n = n / 2;
}
else
{
n = 3 * n + 1;
}
}
Console.WriteLine(“The final value of n is ” + n);

3 Next week: test + foreach loops

At the start of next week’s practical class you’ll do a 20-minute online quiz. After that you’ll do some practical exercises involving an improvement on the for-loop that uses simpler syntax to iterate over all the elements of a collection, the foreach loop.