Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
25e4fd70
Commit
25e4fd70
authored
10 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Loop Exercises
parent
58d93920
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/Sd1/statements.xml
+144
-7
144 additions, 7 deletions
Doc/Sd1/statements.xml
with
144 additions
and
7 deletions
Doc/Sd1/statements.xml
+
144
−
7
View file @
25e4fd70
...
...
@@ -149,16 +149,154 @@
</qandaset>
</section>
<section
xml:id=
"sd1SectIntermediateMultiplication"
>
<title>
Intermediate Example, part 1: A multiplication table
</title>
<qandaset
defaultlabel=
"qanda"
xml:id=
"sd1QandaIntermediateMultiplyTable1"
>
<qandadiv>
<qandaentry>
<question>
<para>
In order to prepare a more sophisticated square number
table we supply a slightly different exercise producing a
multiplication table:
</para>
<programlisting
language=
"none"
>
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
---+----------------------------------------------------------------------------------------------------
1| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
2| 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
3| 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60
4| 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80
5| 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
6| 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120
7| 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140
8| 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152 160
9| 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144 153 162 171 180
10| 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200
11| 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 176 187 198 209 220
12| 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 192 204 216 228 240
13| 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195 208 221 234 247 260
14| 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210 224 238 252 266 280
15| 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225 240 255 270 285 300
16| 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256 272 288 304 320
17| 17 34 51 68 85 102 119 136 153 170 187 204 221 238 255 272 289 306 323 340
18| 18 36 54 72 90 108 126 144 162 180 198 216 234 252 270 288 306 324 342 360
19| 19 38 57 76 95 114 133 152 171 190 209 228 247 266 285 304 323 342 361 380
20| 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400
</programlisting>
<para>
The number of rows and columns are equal. Provide an
appropriate parameter so that your implementation works for
different values just by changing tis single value.
</para>
<tip>
<para>
You'll need a loop nested within an outer one like:
</para>
<programlisting
language=
"none"
>
for (int y=0;
<
limit; y++){
for (int x=0; x
<
limit; x++) {
...
}
}
</programlisting>
</tip>
</question>
<answer>
<programlisting
language=
"none"
>
public static void main(String[] args) {
int limit = 10;
System.out.print(" | ");
for (int col = 1; col
<
= limit; col++) { // Header line of columus from 1 to limit
System.out.format("%3d ", col);
}
System.out.println();
System.out.print("---+"); // Header / table body separator
for (int col = 1; col
<
= limit; col++) {
System.out.print("-----");
}
System.out.println();
for (int row = 1; row
<
= limit; row ++) { // Printing rows
System.out.format("%3d| ", row);
for (int col = 1; col
<
= limit; col++) { // Printing columns
System.out.format("%3d ", row * col);
}
System.out.println();
}
}
</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
<section
xml:id=
"sd1SectIntermediateMultiplication2"
>
<title>
Intermediate Example, part 2: Avoiding redundant entries
</title>
<qandaset
defaultlabel=
"qanda"
xml:id=
"sd1QandaIntermediateMultiplyTable2"
>
<qandadiv>
<qandaentry>
<question>
<para>
It does not make sense to supply both results like e.g. 3
* 4 and 4 * 3. Modify your application to generate:
</para>
<programlisting
language=
"none"
>
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
---+----------------------------------------------------------------------------------------------------
1| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
2| 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
3| 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60
4| 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80
5| 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
6| 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120
7| 49 56 63 70 77 84 91 98 105 112 119 126 133 140
8| 64 72 80 88 96 104 112 120 128 136 144 152 160
9| 81 90 99 108 117 126 135 144 153 162 171 180
10| 100 110 120 130 140 150 160 170 180 190 200
11| 121 132 143 154 165 176 187 198 209 220
12| 144 156 168 180 192 204 216 228 240
13| 169 182 195 208 221 234 247 260
14| 196 210 224 238 252 266 280
15| 225 240 255 270 285 300
16| 256 272 288 304 320
17| 289 306 323 340
18| 324 342 360
19| 361 380
20| 400
</programlisting>
</question>
<answer>
<para>
The required change with respect to the previous exercise
is actually very small. We need
<code>
col = row
</code>
rather
than
<code>
col = 1
</code>
:
</para>
<programlisting
language=
"none"
>
for (int row = 1; row
<
= limit; row ++) { // Printing rows
System.out.format("%3d| ", row);
for (int
<emphasis
role=
"bold"
>
col = row;
</emphasis>
col
<
= limit; col++) { // Printing columns
System.out.format("%3d ", row * col);
}
System.out.println();
}
</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
<section
xml:id=
"sd1SectSquareTableBlocks"
>
<title>
Creating a
<quote>
real
</quote>
table
</title>
<title>
Creating a
<quote>
real
</quote>
square
table
</title>
<qandaset
defaultlabel=
"qanda"
xml:id=
"sd1QandaSquareTableBlocks"
>
<qandadiv>
<qandaentry>
<question>
<para>
The last solution is only appropriate
for smaller amounts
of data. Letting the number of elements grow
requires
rearranging values in blocks:
</para>
<para>
The last
square number table
solution is only appropriate
for smaller amounts
of data. Letting the number of elements grow
requires
rearranging values in blocks:
</para>
<programlisting
language=
"none"
>
n | n*n n | n*n n | n*n n | n*n n | n*n
----+--------------+--------------+--------------+--------------+----------
...
...
@@ -184,8 +322,7 @@
18 | 324 38 | 1444 58 | 3364 78 | 6084 98 | 9604
19 | 361 39 | 1521 59 | 3481 79 | 6241 99 | 9801
</programlisting>
<para>
Building a table this way will require some
parameters:
</para>
<para>
Building a table this way requires some parameters:
</para>
<itemizedlist>
<listitem>
...
...
@@ -204,7 +341,7 @@
</listitem>
</itemizedlist>
<para>
You may have to nest loops within each other.
</para>
<para>
You may have to nest
three
loops within each other.
</para>
</question>
<answer>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment