Skip to content

Commit da6143b

Browse files
ClémentClément
authored andcommitted
Working on solutions for reference exercises.
1 parent cccb39d commit da6143b

4 files changed

Lines changed: 134 additions & 113 deletions

File tree

source/code/snippets/referencesExercise.cs renamed to source/code/projects/ReferenceExercises/ReferenceExercises/Program.cs

File renamed without changes.

source/code/projects/ReferenceMethods/ReferenceMethods/Program.cs

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,76 @@ class Program
44
{
55
static void Main(string[] args)
66
{
7-
// Example for NameChange
8-
static void NameChange(ref string name, string newname, out string oldname){
9-
oldname = name;
10-
name = newname;
11-
}
12-
13-
string name = "Smith", oldname;
14-
Console.WriteLine("Enter new name.");
15-
NameChange(ref name, Console.ReadLine(), out oldname);
16-
Console.WriteLine($"New name: {name}\nOld name: {oldname}");
17-
18-
/*
19-
// Example for Test method
20-
21-
static void Test(int a, out int b){
22-
if (b < 0) { b = a; }
23-
else { b++;}
24-
}
25-
*/
7+
// Example for AddR
8+
static void AddR(ref int a, ref int b){
9+
int tmp = a;
10+
a = a + b;
11+
b = tmp - b;
12+
}
2613

27-
// SafeLength
28-
static int SafeLength<T>(T[] aP){
29-
int ans;
30-
if (aP == null) ans = -1;
31-
else ans = aP.Length;
32-
return ans;
33-
}
34-
35-
int[] test1 = null;
36-
Console.WriteLine(SafeLength(test1));
37-
char[] test2 = {'a', 'b'};
38-
Console.WriteLine(SafeLength(test2));
39-
40-
14+
int x = 4, y = 3;
15+
Console.WriteLine($"x is {x}, y is {y}.");
16+
AddR(ref x, ref y);
17+
Console.WriteLine($"x is {x}, y is {y}.");
18+
// End of AddR example
4119

42-
// Example for AddRev
43-
int x0 = 4,
44-
y0 = 3;
45-
AddRev(ref x0, ref y0);
46-
Console.WriteLine($"x0 is {x0}, y0 is {y0}.");
47-
// Solution for AddRev
48-
void AddRev(ref int xP0, ref int yP0)
20+
/*
21+
// This Test1 method would not compile
22+
static void Test1(int a, out int b){
23+
if (a > 0) { b = 12; }
24+
}
25+
// Uncomment Test1 to see for yourself.
26+
*/
27+
28+
/*
29+
// This Test2 method would not compile
30+
static void Test2(int a, out int b){
31+
if (b < 0) { b = a; }
32+
else { b = 10;}
33+
}
34+
// Uncomment Test2 to see for yourself.
35+
*/
36+
37+
// Example for NameChange
38+
static void NameChange(
39+
ref string nameP,
40+
string newnameP,
41+
out string oldnameP
42+
)
4943
{
50-
int temp = xP0;
51-
xP0 = xP0 + yP0;
52-
yP0 = temp - yP0;
44+
oldnameP = nameP;
45+
nameP = newnameP;
5346
}
47+
// End of NameChange example
48+
49+
string name = "Smith";
50+
// This previous string is assumed given.
51+
string oldname;
52+
Console.WriteLine("Enter new name.");
53+
NameChange(ref name, Console.ReadLine(), out oldname);
54+
Console.WriteLine(
55+
$"New name: {name}\nOld name: {oldname}"
56+
);
57+
// End of NameChange usage example
58+
5459
// Example for AddLog
5560
string log;
5661
int x1 = 4,
57-
y1 = 3;
62+
y1 = 3;
5863
int result = AddLog(x1, y1, out log);
5964
Console.WriteLine(log + "\n" + result);
65+
6066
// Solution for AddLog
6167
int AddLog(int xP1, int yP1, out string logP)
6268
{
6369
logP = xP1 + " + " + yP1 + " = " + (xP1 + yP1) + ".";
6470
return xP1 + yP1;
6571
}
66-
// Example for AddReset
72+
73+
// AddReset example
6774
int x2 = 2,
68-
y2 = 3,
69-
z2;
75+
y2 = 3,
76+
z2;
7077
AddReset(ref x2, ref y2, out z2);
7178
Console.WriteLine($"x2 = {x2}, y2 = {y2}, z2 = {z2}.");
7279
// Solution for AddReset
@@ -76,6 +83,7 @@ void AddReset(ref int xP2, ref int yP2, out int zP2)
7683
xP2 = 0;
7784
yP2 = 0;
7885
}
79-
// Done!
86+
// Done with AddReset
8087
}
88+
8189
}

source/solutions/data/1darrays.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,25 @@ For example, in an array containing 1, 2, 6, 7, 3, 9 with x being 8, the solutio
632632
```
633633
</details>
634634

635+
#. Write a `SafeLength` method that takes an input an array of *any type* and returns its length if the argument is not `null`, -1 otherwise.
636+
637+
638+
<details>
639+
<summary>Solution</summary>
640+
641+
```
642+
public static int SafeLength<T>(T[] aP)
643+
{
644+
int ans;
645+
if (aP == null)
646+
ans = -1;
647+
else
648+
ans = aP.Length;
649+
return ans;
650+
}
651+
```
652+
</details>
653+
635654

636655
## Simple Algorithms
637656

source/solutions/oop/references.md

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,8 @@ tags:
1818

1919
#. What would be displayed by the following?
2020

21-
```
22-
static void Main(){
23-
int x = 4, y = 3;
24-
Console.WriteLine($"x is {x}, y is {y}.");
25-
AddR(ref x, ref y);
26-
Console.WriteLine($"x is {x}, y is {y}.");
27-
}
28-
static void AddR(ref int a, ref int b){
29-
int tmp = a;
30-
a = a + b;
31-
b = tmp - b;
32-
}
21+
```{download="./code/projects/ReferenceMethods.zip"}
22+
!include`snippetStart="// Example for AddR",snippetEnd="// End of AddR example"` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
3323
```
3424

3525
- [ ] "x is {x}, y is {y}.", "x is {x}, y is {y}."
@@ -40,24 +30,35 @@ tags:
4030

4131
#. The following method would *not* compile. Why?
4232

43-
```
44-
public static void Test(int a, out int b){
45-
if (a > 0) { b = 12; }
46-
}
33+
```{download="./code/projects/ReferenceMethods.zip"}
34+
!include`snippetStart="// This Test1 method would not compile",snippetEnd="// Uncomment Test1 to see for yourself."` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
4735
```
4836

4937
- [ ] A method cannot be `static` and have `void` as a return type.
5038
- [x] Any `out` parameter must have its value set in the body of the method.
5139
- [ ] The `else` is missing.
40+
- [ ] C# assumes that `out` parameters do not have a value.
5241
- [ ] The keyword `out` cannot be used in the header of a method.
5342
- [ ] An `out` parameter cannot be assigned a value.
54-
43+
44+
#. The following method would *not* compile. Why?
45+
46+
```{download="./code/projects/ReferenceMethods.zip"}
47+
!include`snippetStart="// This Test2 method would not compile",snippetEnd="// Uncomment Test2 to see for yourself."` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
48+
```
49+
50+
- [ ] A method cannot be `static` and have `void` as a return type.
51+
- [ ] Any `out` parameter must have its value set in the body of the method.
52+
- [x] C# assumes that `out` parameters do not have a value.
53+
- [ ] The keyword `out` cannot be used in the header of a method.
54+
- [ ] An `out` parameter cannot be assigned a value.
55+
5556
## Warm-up Exercises
5657

5758
#. Consider the following code:
5859

59-
```
60-
!include code/snippets/referencesExercise.cs
60+
```{download="./code/projects/ReferenceExercises.zip"}
61+
!include code/projects/ReferenceExercises/ReferenceExercises/Program.cs
6162
```
6263

6364
#. What are the values of `x`, `y` and `z`
@@ -70,59 +71,45 @@ tags:
7071
#. What is the value of `d`?
7172

7273
<details><summary>Solution</summary>
73-
Before the `Foo` method is executed: 1, 2, and `z` is not set.
74-
75-
Inside the `Foo` method: 2, 1 and 3.
76-
77-
After the `Foo` method: 1, 0, and 2
78-
79-
`c` holds `'*'`, `d` holds `%`.
74+
#. We have the following:
75+
76+
When | `x` | `y` | `z`
77+
---------------------------------- | :---: | :---: | :---:
78+
Before the `Foo` method is called | 1 | 2 | not set
79+
Inside the `Foo` method | 2 | 1 | 3
80+
After the `Foo` method | 1 | 0 | 2
81+
82+
#. `c` holds `'*'`,
83+
#. `d` holds `'%'`.
8084
</details>
8185

8286
#. Consider the following method:
8387

84-
```
85-
static void NameChange(ref string nameP, string newnameP, out string oldnameP){
86-
oldnameP = nameP;
87-
nameP = newnameP;
88-
}
89-
```
90-
91-
<details><summary>Solution</summary>
92-
93-
</details>
94-
88+
```{download="./code/projects/ReferenceMethods.zip"}
89+
!include`snippetStart="// Example for NameChange",snippetEnd="// End of NameChange example"` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
90+
```
9591

9692
Assume given a `string` `name` variable containing a value. Write a short program (possibly declaring additional variables) that
93+
9794
#. Asks the user their new name,
9895
#. Calls the `NameChange` method with appropriate arguments,
9996
#. Displays the new name and the old name.
10097

101-
## Problems
102-
103-
#. Write the `AddRev` method (header included) such that the following:
104-
105-
```
106-
!include`snippetStart="// Example for AddRev",snippetEnd="// Solution for AddRev"` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
107-
```
108-
109-
would display
110-
111-
```
112-
x0 is 7, y0 is 1.
113-
```
114-
11598
<details><summary>Solution</summary>
116-
```
117-
!include code/projects/ReferenceMethods/ReferenceMethods/Program.cs
118-
```
99+
```{download="./code/projects/ReferenceMethods.zip"}
100+
!include`snippetStart="// This previous string is assumed given.",snippetEnd="// End of NameChange usage example"` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
101+
```
119102
</details>
120-
103+
104+
105+
## Problems
106+
121107
#. Write the `AddLog` method (header included) such that the following:
122108

123-
```
109+
<details><summary>Solution</summary>
110+
```{download="./code/projects/ReferenceMethods.zip"}
124111
!include`snippetStart="// Example for AddLog",snippetEnd="// Solution for AddLog"` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
125-
```
112+
```
126113

127114
would display
128115

@@ -132,15 +119,15 @@ tags:
132119
```
133120

134121
<details><summary>Solution</summary>
135-
```
136-
!include`snippetStart="// Solution for AddLog",snippetEnd="// Example for AddReset"` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
137-
```
122+
```{download="./code/projects/ReferenceMethods.zip"}
123+
!include`snippetStart="// Solution for AddLog",snippetEnd="// AddReset example"` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
124+
```
138125
</details>
139126

140127
#. Write the `AddReset` method (header included) such that the following:
141128

142-
```
143-
!include`snippetStart="// Example for AddReset",snippetEnd="// Solution for AddReset"` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
129+
```{download="./code/projects/ReferenceMethods.zip"}
130+
!include`snippetStart="AddReset example",snippetEnd="// Solution for AddReset"` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
144131
```
145132

146133
would display
@@ -150,11 +137,13 @@ tags:
150137
```
151138

152139
<details><summary>Solution</summary>
153-
```
154-
!include`snippetStart="// Solution for AddReset",snippetEnd="// Done!"` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
140+
```{download="./code/projects/ReferenceMethods.zip"}
141+
!include`snippetStart="// Solution for AddReset",snippetEnd="// Done with AddReset"` code/projects/ReferenceMethods/ReferenceMethods/Program.cs
155142
```
156143
</details>
157144

145+
146+
158147
#. Consider the "regular" implementation of the `Rectangle` class:
159148

160149
```
@@ -165,7 +154,12 @@ tags:
165154
public int Length
166155
{
167156
get { return length; }
168-
set { if (value < 0) { throw new ArgumentNullException(); } else length = value; }
157+
set {
158+
if (value < 0) {
159+
throw new ArgumentNullException();
160+
}
161+
else length = value;
162+
}
169163
}
170164

171165
private int width;

0 commit comments

Comments
 (0)