Skip to content

Commit 5cd2bf3

Browse files
committed
Adding solution to first problem of exam #2.
1 parent f45cc1f commit 5cd2bf3

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

source/code/projects/FileDisplayerSolution/FileDisplayerSolution/FileDisplayer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
using System;
22
using System.IO;
33

4+
/*
5+
* Foreword: note that the project
6+
* asks the method to display an error
7+
* message if something goes wrong. A more
8+
* principled approach would require the
9+
* method to send an exception back to
10+
* the main routine, and to let it handle
11+
* the error as it sees fit.
12+
*/
13+
414
static class FileDisplayer
515
{
616
public static void Display(string exP)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
class Program
5+
{
6+
static void Main(string[] args)
7+
{
8+
// DisplayH question
9+
string DisplayH(int[] aP, int currentIndex)
10+
{
11+
string ret = "";
12+
if (aP.Length == currentIndex + 1)
13+
ret += " || " + aP[currentIndex].ToString();
14+
else ret+= DisplayH(aP, currentIndex+1) + ", " + aP[currentIndex].ToString();
15+
if (currentIndex == 0){ret += " ** "; }
16+
return ret;
17+
}
18+
string Display(int[] aP)
19+
{
20+
if (aP == null)
21+
return "";
22+
else
23+
return DisplayH(aP, 0);
24+
}
25+
// Examples
26+
int[] array0 = { 1, 2, 4};
27+
int[] array1 = { 1, 2, 4, 10, 13, 17 };
28+
int[] array2 = { 12, 98, 120, 15 };
29+
int[] array3 =
30+
{
31+
1,
32+
10,
33+
12,
34+
129,
35+
190,
36+
220,
37+
230,
38+
310,
39+
320,
40+
340,
41+
400,
42+
460,
43+
};
44+
Console.WriteLine("Example 0: " + Display(array0));
45+
Console.WriteLine("Example 1: " + Display(array1));
46+
Console.WriteLine("Example 2: " + Display(array2));
47+
Console.WriteLine(
48+
"Example 3: " + Display(array3)
49+
);
50+
51+
}
52+
}

source/solutions/control/recursion.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ tags:
160160
Those are the first 6 digits of the [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_sequence).
161161
</details>
162162

163+
#. What would the following code display?
164+
165+
```{download="./code/projects/RecursionDisplayH.zip"}
166+
!include`snippetStart="// DisplayH question",snippetEnd="// Examples"` code/projects/RecursionDisplayH/RecursionDisplayH/Program.cs
167+
```
168+
169+
<details>
170+
<summary>Solution</summary>
171+
It would display `|| 4, 2, 1 **`. You can download the solution above to execute the method and see for yourself. Note that the condition `currentIndex == 0` adds the ` ** ` string *at the very end* of the string returned.
172+
</details>
173+
163174
#. Rewrite the following `while` loop as a recursive method.
164175

165176
```{download="./code/projects/RecursionAndLoops.zip"}

0 commit comments

Comments
 (0)