<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Funktion die bestimmt ob Elemente Gerade sind]]></title><description><![CDATA[<p>Hallo Leute!</p>
<p>Ich soll eine Funktion <strong>bool</strong> <em>alleven</em>(<strong>int</strong> <em>feld</em>[],<strong>int</strong> <em>anzahl</em>) schreiben die für ein übergebenes (<em>feld</em>) in dem <em>anzahl</em> Elemente gespeichert sind, bestimmt, ob alle Elemente gerade sind. Falls sich keine Elemente darin befinden so ist das Ergebnis true.</p>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    bool ergebnis
	if (anzahl==0)
	{
	return true;
	}
	else
	{
		ergebnis= alleven(feld, anzahl-1);
		if (ergebnis==true)
		{
			if (feld[anzahl-1]%2==0)
			{
				return true;
			}
		      else 
			{
				return false;
			}
		}
		return false;
	}
}
</code></pre>
<p>Ist das so korrekt?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/291886/funktion-die-bestimmt-ob-elemente-gerade-sind</link><generator>RSS for Node</generator><lastBuildDate>Mon, 17 Aug 2026 13:03:40 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/291886.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 29 Aug 2011 11:40:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 11:40:47 GMT]]></title><description><![CDATA[<p>Hallo Leute!</p>
<p>Ich soll eine Funktion <strong>bool</strong> <em>alleven</em>(<strong>int</strong> <em>feld</em>[],<strong>int</strong> <em>anzahl</em>) schreiben die für ein übergebenes (<em>feld</em>) in dem <em>anzahl</em> Elemente gespeichert sind, bestimmt, ob alle Elemente gerade sind. Falls sich keine Elemente darin befinden so ist das Ergebnis true.</p>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    bool ergebnis
	if (anzahl==0)
	{
	return true;
	}
	else
	{
		ergebnis= alleven(feld, anzahl-1);
		if (ergebnis==true)
		{
			if (feld[anzahl-1]%2==0)
			{
				return true;
			}
		      else 
			{
				return false;
			}
		}
		return false;
	}
}
</code></pre>
<p>Ist das so korrekt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2111993</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2111993</guid><dc:creator><![CDATA[TylerDurden0903]]></dc:creator><pubDate>Mon, 29 Aug 2011 11:40:47 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 11:52:25 GMT]]></title><description><![CDATA[<p>Ja, wobei sehr umstaendlich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2112002</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2112002</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Mon, 29 Aug 2011 11:52:25 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 11:54:27 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">bool alleven(int feld[], int anzahl)
{
    return std::find_if(feld, feld + anzahl, [](int n) { return n % 2; }) == feld + anzahl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2112005</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2112005</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 29 Aug 2011 11:54:27 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 11:55:04 GMT]]></title><description><![CDATA[<p>Das sieht korrekt aus, aber extrem umständlich. Wie wäre es mit einer kleinen Schleife? Dann wird das zu einem Dreizeiler.</p>
<p>edit: Zu langsam.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2112006</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2112006</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 29 Aug 2011 11:55:04 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 12:00:48 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Das sieht korrekt aus, aber extrem umständlich. Wie wäre es mit einer kleinen Schleife? Dann wird das zu einem Dreizeiler.</p>
</blockquote>
<p>Ist das ein Ein- oder ein Vierzeiler?</p>
<pre><code class="language-cpp">bool alleven(int feld[], int anzahl)
{
  return anzahl == 0 || feld[0] % 2 == 0 &amp;&amp; alleven(feld + 1, anzahl - 1);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2112013</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2112013</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Mon, 29 Aug 2011 12:00:48 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 12:02:45 GMT]]></title><description><![CDATA[<p>Leere Felder gibts übrigens nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2112015</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2112015</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 29 Aug 2011 12:02:45 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 12:09:14 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/23275">@314159265358979</a>:<br />
Ja, ist natürlich immer gut, Spezialfälle nicht zu berücksichtigen, von denen man meint sie wären sowieso verboten. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2112022</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2112022</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 29 Aug 2011 12:09:14 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 12:09:41 GMT]]></title><description><![CDATA[<p>Bashar schrieb:</p>
<blockquote>
<pre><code class="language-cpp">bool alleven(int feld[], int anzahl)
{
  return anzahl == 0 || feld[0] % 2 == 0 &amp;&amp; alleven(feld + 1, anzahl - 1);
}
</code></pre>
</blockquote>
<p>Etwas Offtopic, aber das frage ich mich schon länger.</p>
<p>Wo steht im C++ Standard, dass der linke Ausdruck &quot;anzahl == 0&quot; als erstes Abgearbeitet wird? Und bei true die rechte Seite nicht weiter verarbeitet wird?</p>
<p>Das es so ist, dass kann man ja ausprobieren. Aber ist das Standard-Konform?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2112023</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2112023</guid><dc:creator><![CDATA[Siassei - Auf der Arbeit]]></dc:creator><pubDate>Mon, 29 Aug 2011 12:09:41 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 12:10:32 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/23275">@314159265358979</a>:<br />
Ja, ist natürlich immer gut, Spezialfälle nicht zu berücksichtigen, von denen man meint sie wären sowieso verboten. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
</blockquote>
<p>Stell dir vor, meine Funktion funktioniert trotzdem. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2112025</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2112025</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 29 Aug 2011 12:10:32 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 12:16:30 GMT]]></title><description><![CDATA[<p>Siassei - Auf der Arbeit schrieb:</p>
<blockquote>
<p>Wo steht im C++ Standard, dass der linke Ausdruck &quot;anzahl == 0&quot; als erstes Abgearbeitet wird? Und bei true die rechte Seite nicht weiter verarbeitet wird?</p>
</blockquote>
<p>Da, wo der || Operator beschrieben wird (5.15§1).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2112030</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2112030</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Mon, 29 Aug 2011 12:16:30 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 12:34:02 GMT]]></title><description><![CDATA[<p>TylerDurden0903 schrieb:</p>
<blockquote>
<p>Hallo Leute!</p>
<p>Ich soll eine Funktion <strong>bool</strong> <em>alleven</em>(<strong>int</strong> <em>feld</em>[],<strong>int</strong> <em>anzahl</em>) schreiben die für ein übergebenes (<em>feld</em>) in dem <em>anzahl</em> Elemente gespeichert sind, bestimmt, ob alle Elemente gerade sind. Falls sich keine Elemente darin befinden so ist das Ergebnis true.</p>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    bool ergebnis
	if (anzahl==0)
	{
	return true;
	}
	else
	{
		ergebnis= alleven(feld, anzahl-1);
		if (ergebnis==true)
		{
			if (feld[anzahl-1]%2==0)
			{
				return true;
			}
		      else 
			{
				return false;
			}
		}
		return false;
	}
}
</code></pre>
<p>Ist das so korrekt?</p>
</blockquote>
<p>Ja, sieht guut aus.</p>
<p>Aber Mega-Umständlich.</p>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    bool ergebnis;//Nee, erst später definieren, erst, wenn man sie sinnvoll initialisieren kann
    if (anzahl==0)
    {
    return true;
    }
    else
    {
        ergebnis= alleven(feld, anzahl-1);
        if (ergebnis==true)
        {
            if (feld[anzahl-1]%2==0)
            {
                return true;
            }
              else
            {
                return false;
            }
        }
        return false;
    }
}
</code></pre>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    if (anzahl==0)
    {
    return true;
    }
    else
    {
        bool ergebnis= alleven(feld, anzahl-1);//ach, dann kann sie auch weg
        if (ergebnis==true)
        {
            if (feld[anzahl-1]%2==0)
            {
                return true;
            }
              else
            {
                return false;
            }
        }
        return false;
    }
}
</code></pre>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    if (anzahl==0)
    {
    return true;
    }
    else
    {
        if (alleven(feld, anzahl-1)==true)
        {
            if (feld[anzahl-1]%2==0)//Trick 17
            {
                return true;
            }
              else
            {
                return false;
            }
        }
        return false;
    }
}
</code></pre>
<p>Trick 17:<br />
<strong>if(ausdruck==true) return true; else return false;</strong> ist identisch zu und meistens besser zu lesen als <strong>return ausdruck;</strong></p>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    if (anzahl==0)
    {
    return true;
    }
    else
    {
        if (alleven(feld, anzahl-1)==true)
        {
            return feld[anzahl-1]%2==0;
        }
        return false;
    }
}
</code></pre>
<p>Oh, in eine Optimier-Sackgasse getappt. Zurück.</p>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    if (anzahl==0)
    {
    return true;
    }
    else
    {
        if (alleven(feld, anzahl-1)==true)
        {
            if (feld[anzahl-1]%2==0)//Nee, nicht Trick 17, sondern Trick 18 rückwärts
            {
                return true;
            }
              else
            {
                return false;
            }
        }
        return false;
    }
}
</code></pre>
<p>Trick 18: <strong>if(A &amp;&amp; <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--smiling_face_with_sunglasses"
      title="B)"
      alt="😎"
    /> cmd;</strong> ist identisch zu und oft besser zu lesen als <strong>if(A) if(B) cmd;</strong></p>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    if (anzahl==0)
    {
    return true;
    }
    else
    {
        if (alleven(feld, anzahl-1)==true &amp;&amp; feld[anzahl-1]%2==0)//17
        {
            return true;
        }
        else
        {
            return false;
        }
        return false;
    }
}
</code></pre>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    if (anzahl==0)
    {
    return true;
    }
    else
    {
        return alleven(feld, anzahl-1)==true &amp;&amp; feld[anzahl-1]%2==0);
        return false;//UUps, woher kommt die Leiche denn?
    }
}
</code></pre>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    if (anzahl==0)
    {
        return true;
    }
    else
    {
        return alleven(feld, anzahl-1)==true &amp;&amp; feld[anzahl-1]%2==0);
    }
}
</code></pre>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    if (anzahl==0)
        return true;
    return alleven(feld, anzahl-1)==true &amp;&amp; feld[anzahl-1]%2==0);
}
</code></pre>
<pre><code class="language-cpp">bool alleven(int feld[],int anzahl)
{
    if (anzahl==0)
        return true;
    return alleven(feld, anzahl-1) &amp;&amp; feld[anzahl-1]%2==0;
}
</code></pre>
<p>Und dann vielleicht noch</p>
<pre><code class="language-cpp">bool isEven(int n)
{
    return n%2==0;
}
bool areAllEven(int feld[],int anzahl)
{
    if (anzahl==0)
        return true;
    return areAllEven(feld, anzahl-1) &amp;&amp; isEven(feld[anzahl-1]);
}
</code></pre>
<p>Aber ich hätte keine Rekursion genommen, sondern eine Schleife. Die fällt dann auch zusammen, ungefähr zu sowas:</p>
<pre><code class="language-cpp">bool areAllEven(int feld[],int anzahl)
{
    for(int i=0;i&lt;anzahl;++i)
       if(!isEven(feld[i])
           return false;
    return true;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2112031</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2112031</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 29 Aug 2011 12:34:02 GMT</pubDate></item><item><title><![CDATA[Reply to Funktion die bestimmt ob Elemente Gerade sind on Mon, 29 Aug 2011 12:22:48 GMT]]></title><description><![CDATA[<p>Bashar schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Das sieht korrekt aus, aber extrem umständlich. Wie wäre es mit einer kleinen Schleife? Dann wird das zu einem Dreizeiler.</p>
</blockquote>
<p>Ist das ein Ein- oder ein Vierzeiler?</p>
<pre><code class="language-cpp">bool alleven(int feld[], int anzahl)
{
  return anzahl == 0 || feld[0] % 2 == 0 &amp;&amp; alleven(feld + 1, anzahl - 1);
}
</code></pre>
</blockquote>
<p>In meiner obigen Zählweise hätte ich das als Einzeiler bezeichnet, weil es ein einziger Ausdruck ist. Aber das ist ja auch wieder die rekursive Variante. Ich hätte (um etwas anfängerfreundlich zu sein als 314159265358979s Version) die letzte Version von volkard genommen:</p>
<pre><code class="language-cpp">bool areAllEven(int feld[],int anzahl)
{
    for(int i=0;i&lt;anzahl;++i)
       if(!isEven(feld[i])
           return false;
    return true;
}
</code></pre>
<p>Habe mich aber wohl verzählt als ich im Kopf programmiert habe und auf drei Zeilen gekommen bin. Aber man könnte bestimmt irgendwie rechtfertigen, dass man das return false ind die gleiche Zeile wie das if schreibt :p .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2112036</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2112036</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 29 Aug 2011 12:22:48 GMT</pubDate></item></channel></rss>