From bbafedef8eb2b11ed745e6ebfe16e387981d6a24 Mon Sep 17 00:00:00 2001 From: "Dr. Martin Goik" <goik@hdm-stuttgart.de> Date: Sun, 29 Apr 2018 19:43:34 +0200 Subject: [PATCH] Removing duplicate sum exercise --- Doc/Sd1/statements.xml | 86 ++++++------------------------------------ 1 file changed, 11 insertions(+), 75 deletions(-) diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index d524786b1..b065156e1 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -3359,21 +3359,14 @@ System.out.println("1 + ... + " + limit + " = " + sum); </programlisting> <qandaentry> <question> <para>In <xref linkend="sd1_fig_loopCalculations"/> for a - given value of limit only the first and last summands will - show up: We just see <code>1 + ... + 5 = 15</code> rather than + given value of limit only the first and last summand will show + up: We just see <code>1 + ... + 5 = 15</code> rather than <code>1 + 2 + 3 + 4 + 5 = 15</code>.</para> - <para>Even worse:</para> + <para>If <code language="java">limit</code> is equal to one + the visible result is even worse:</para> - <glosslist> - <glossentry> - <glossterm>limit = 1:</glossterm> - - <glossdef> - <screen>1 + ... + 1 = 1</screen> - </glossdef> - </glossentry> - </glosslist> + <screen>1 + ... + 1 = 1</screen> <para>Modify the code accordingly to correct these flaws / shortcomings.</para> @@ -3408,7 +3401,7 @@ System.out.println(" = " + sum);</programlisting> <programlisting language="none">for (int i = 1; i <= limit; i++) { System.out.print(i); - <emphasis role="red">if (i < limit)</emphasis> { + <emphasis role="red">if (i < limit)</emphasis> { <emphasis role="bold">// Avoid '+' for the last value</emphasis> System.out.print(" + "); } sum += i; @@ -3424,69 +3417,12 @@ System.out.println(" = " + sum);</programlisting> 1</code> as well:</para> <screen>1 = 1</screen> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_allSummands"> - <title>Showing all constituents of a sum</title> - - <qandadiv> - <qandaentry> - <question> - <para>In <xref linkend="sd1_fig_loopCalculations"/> the - resulting output was:</para> - - <screen>1 + ... + 5 = 15</screen> - - <para>Modify the code to show all contributing values - <computeroutput>1 + 2 + 3 + 4 + 5 = 15</computeroutput> - instead.</para> - </question> - - <answer> - <para>First we need to print all values of our loop variable - <code language="java">i</code>:</para> - - <programlisting language="java">for (int i = 1; i <= limit; i++) { - System.out.print(i + " + "); - sum += i; -}</programlisting> - - <para>This yields <computeroutput>1 + 2 + 3 + 4 + 5 - +</computeroutput>. Notice the last <quote><code - language="java">+</code></quote> operator: To avoid this - operator being printed we may split our print statement and - add an <code language="java">if</code> guard inside our - loop:</para> - - <programlisting language="java">for (int i = 1; i <= limit; i++) { - System.out.print(i); - <emphasis role="bold">if (i < limit) { // Do not print last value</emphasis> - System.out.print(" + "); - <emphasis role="bold">}</emphasis> - sum += i; -}</programlisting> - - <para>This leaves us with <computeroutput>1 + 2 + 3 + 4 + - 5</computeroutput>. The only bit missing is a final print - statement supplying the last operand and the sum in - question:</para> - - <programlisting language="java">for (int i = 1; i <= limit; i++) { - System.out.print(i); - if (i < limit) { // Do not print last value - System.out.print(" + "); - } - sum += i; -} -<emphasis role="bold">System.out.println(" = " + sum);</emphasis></programlisting> - <para>Instead of filtering a <quote><code + <para>Instead of filtering the very last <quote><code language="java">+</code></quote> operator we may as well - terminate our loop earlier and move the last operand to the - second print statement:</para> + terminate our loop one step earlier and move the last operand + to the second print statement leaving us with an identical + result:</para> <programlisting language="java">int limit = 5; int sum = 0; @@ -3495,7 +3431,7 @@ for (int i = 1; i < limit; i++) { System.out.print(i + " + "); sum += i; } -sum += limit; // accounting for last term missing +sum += limit; // account for last term System.out.println(limit + " = " + sum);</programlisting> </answer> -- GitLab