<?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[GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich lerne munter weiter C++ ^^ und bin nun bei Rekursion und Iterativer Lösung angelangt. Hier nun mein Lösungsansatz für den Größten Gemeinsamen Teiler:</p>
<pre><code class="language-cpp">//GGT
#include &lt;iostream&gt;
using namespace std;

// Rekursiv
int ggtrekursiv(int a, int b)
{
	if (a%b==0)
	{
		return b;
	}
	else
		ggtrekursiv(a, a%b);
}

int ggt( int a, int b)
{
	if (a&gt;b)
	{
		for (int n=1; n&lt;=b; n++)
		{
			if (a%n == 0 &amp;&amp; b%n ==0)
			{
				int ggf= n;
				return ggf;
			}
			else if (b&gt;a)
			{
				for (int n=1; n&lt;=a; n++)
				{
					if (a%n == 0 &amp;&amp; b%n ==0)
					{
					int ggf= n;
					return ggf;
					}
				}
			}
		}
	}
}

int main()
{
	int a;
	int b;

	cout &lt;&lt; &quot;Bitte geben Sie zwei Integer ein: &quot; &lt;&lt; endl;
	cin &gt;&gt; a; cin &gt;&gt; b;

	cout &lt;&lt; &quot;Rekursiv: &quot; &lt;&lt; ggtrekursiv(a, b) &lt;&lt; endl;
	cout &lt;&lt; &quot;Iterative Loesung: &quot; &lt;&lt; ggt(a, b) &lt;&lt; endl;

	system (&quot;PAUSE&quot;);

	return 0;
}
</code></pre>
<p>Das die Rekursive Funktion nur Funktioniert wenn a die größere Integer ist verstehe ich ja noch und glaube, das man das rekursiv nicht abfangen kann, richtig?</p>
<p>Bei der Iterativen Form scheint jedoch ein (Denk-)Fehler drin zu stecken, auf den ich einfach nicht selber komme.</p>
<p>Irgendjemand eine Idee? Danke im voraus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/204317/ggt-verstädnis-problem-bzw-fehlersuche-in-meinem-code</link><generator>RSS for Node</generator><lastBuildDate>Thu, 08 Oct 2026 13:47:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/204317.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 01 Feb 2008 15:17:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 15:17:44 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich lerne munter weiter C++ ^^ und bin nun bei Rekursion und Iterativer Lösung angelangt. Hier nun mein Lösungsansatz für den Größten Gemeinsamen Teiler:</p>
<pre><code class="language-cpp">//GGT
#include &lt;iostream&gt;
using namespace std;

// Rekursiv
int ggtrekursiv(int a, int b)
{
	if (a%b==0)
	{
		return b;
	}
	else
		ggtrekursiv(a, a%b);
}

int ggt( int a, int b)
{
	if (a&gt;b)
	{
		for (int n=1; n&lt;=b; n++)
		{
			if (a%n == 0 &amp;&amp; b%n ==0)
			{
				int ggf= n;
				return ggf;
			}
			else if (b&gt;a)
			{
				for (int n=1; n&lt;=a; n++)
				{
					if (a%n == 0 &amp;&amp; b%n ==0)
					{
					int ggf= n;
					return ggf;
					}
				}
			}
		}
	}
}

int main()
{
	int a;
	int b;

	cout &lt;&lt; &quot;Bitte geben Sie zwei Integer ein: &quot; &lt;&lt; endl;
	cin &gt;&gt; a; cin &gt;&gt; b;

	cout &lt;&lt; &quot;Rekursiv: &quot; &lt;&lt; ggtrekursiv(a, b) &lt;&lt; endl;
	cout &lt;&lt; &quot;Iterative Loesung: &quot; &lt;&lt; ggt(a, b) &lt;&lt; endl;

	system (&quot;PAUSE&quot;);

	return 0;
}
</code></pre>
<p>Das die Rekursive Funktion nur Funktioniert wenn a die größere Integer ist verstehe ich ja noch und glaube, das man das rekursiv nicht abfangen kann, richtig?</p>
<p>Bei der Iterativen Form scheint jedoch ein (Denk-)Fehler drin zu stecken, auf den ich einfach nicht selber komme.</p>
<p>Irgendjemand eine Idee? Danke im voraus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1447656</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447656</guid><dc:creator><![CDATA[ThaRealMatix]]></dc:creator><pubDate>Fri, 01 Feb 2008 15:17:44 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 15:26:24 GMT]]></title><description><![CDATA[<p>ThaRealMatix schrieb:</p>
<blockquote>
<p>Das die Rekursive Funktion nur Funktioniert wenn a die größere Integer ist verstehe ich ja noch und glaube, das man das rekursiv nicht abfangen kann, richtig?</p>
</blockquote>
<p>Nein. Ruf statt ggtrekursiv(a,a%b) einfach ggtrekursiv(b,a%b) auf.</p>
<p>Deine iterative Version ist irgendwie etwas merkwürdig. Spiel doch mal die rekursive Variante auf dem Papier durch und schau, ob Du das hinkriegst.</p>
<p>Mein Anfang würde so aussehen:</p>
<pre><code class="language-cpp">while(b!=0)
{
  // b!=0, also dürfen wir modulo b rechnen!
  // ersetze also a durch b und b durch a%b.
}

return a;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1447660</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447660</guid><dc:creator><![CDATA[Jester]]></dc:creator><pubDate>Fri, 01 Feb 2008 15:26:24 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 15:30:35 GMT]]></title><description><![CDATA[<p>Laut Wikipedia sollten die Algorithmen ungefähr so aussehen:</p>
<pre><code class="language-cpp">int gcd_iterative(int a, int b)
{
	while (b != 0)
	{
		// siehe Jester
}

int gcd_recursive(int a, int b)
{
	if (b == 0)
		return a;
	else
		return gcd_recursive(b, a % b);
}
</code></pre>
<p>Die funktionieren auch für alle Zahlen. Nur wenn einer der beiden 0 ist wird der andere zurückgegeben.</p>
<p>Gruß<br />
Don06</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1447662</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447662</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Fri, 01 Feb 2008 15:30:35 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 15:54:28 GMT]]></title><description><![CDATA[<p>Hm... tut mir leid hab mich wohl falsch ausgedrückt</p>
<p>ich meine ich kann keine rekursive Funktion schreiben die unterscheidet ob a oder b größer ist und entsprechend arbeitet oder?</p>
<p>und die iterative Funktion... tut mir leid ich blicks absolut nicht, egal wie ich rumspiele <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f622.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--crying_face"
      title=";("
      alt="😢"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1447673</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447673</guid><dc:creator><![CDATA[ThaRealMatix]]></dc:creator><pubDate>Fri, 01 Feb 2008 15:54:28 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 16:23:45 GMT]]></title><description><![CDATA[<p>Die rekursive Funktion funktioniert mit _allen_ Werten. Die Werte werden automatisch vertauscht. Geh das ganze doch mal theoretisch auf einem Blatt Papier durch.<br />
Die iterative hat Jester bereits genau beschrieben. Du musst es nur noch in C++ &quot;übersetzen&quot;.</p>
<p>Gruß<br />
Don06</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1447692</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447692</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Fri, 01 Feb 2008 16:23:45 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 16:37:08 GMT]]></title><description><![CDATA[<p>Hm...</p>
<pre><code class="language-cpp">int ggt( int a, int b)
{

	if (a&gt;b)
	{
		while (b!=0)
		{
			b=a%a%b;
		}
		return b;
	}

	else if (a&lt;b)
	{
		while (b!=0)
		{
			b=a%b%a;
		}
		return b;
	}
}
</code></pre>
<p>Funktioniert nicht und egal wie ich rumspiele es will nicht.... was denk ich denn nur falsch -.-</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1447698</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447698</guid><dc:creator><![CDATA[ThaRealMatix]]></dc:creator><pubDate>Fri, 01 Feb 2008 16:37:08 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 16:41:34 GMT]]></title><description><![CDATA[<p>wie kommst du auf a%a%b bzw. a%b%a ?<br />
a%a ist 0 für alle a!=0<br />
a%b ist a, wenn b &gt; a und folglich a%b%a ebenfalls gleich 0</p>
<p>Nicht wild rumspielen, sondern nachdenken und bewusst schreiben.<br />
Nicht das dieser Code irgendetwas mit dem Ursprünglichen zu tun hätte...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1447700</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447700</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 01 Feb 2008 16:41:34 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 16:56:19 GMT]]></title><description><![CDATA[<p>Hm...</p>
<p>ich glaube, ich hab es?</p>
<pre><code class="language-cpp">int ggt( int a, int b)
{

	if (a&gt;b)
	{
		while (b!=0)
		{
			 b=a%b;
			 return b;
		}		
	}

	else if (a&lt;b)
	{
		while (a!=0)
		{
			a=b%a;
			return a;
		}

	}
}
</code></pre>
<p>Edit: zu früh gefreut... die Iterative Lösung gibt mir 21 als bei 70 und 49 Lösung an -.-</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1447705</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447705</guid><dc:creator><![CDATA[ThaRealMatix]]></dc:creator><pubDate>Fri, 01 Feb 2008 16:56:19 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 17:07:47 GMT]]></title><description><![CDATA[<p>Ich weise nochmal auf das hin, was Jester geschrieben hat:</p>
<p>Jester schrieb:</p>
<blockquote>
<pre><code class="language-cpp">while(b!=0) 
{ 
  // b!=0, also dürfen wir modulo b rechnen! 
  // ersetze also a durch b und b durch a%b. 
} 

return a;
</code></pre>
</blockquote>
<p>Es fehlen drei Anweisungen in der Schleife, Jester hat sie (fast) alle beschrieben, Du solltest dir eine Hilfsvariable anlegen, um das Zwischen ergebnis zu speichern.</p>
<p>Gruß<br />
Don06</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1447709</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447709</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Fri, 01 Feb 2008 17:07:47 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 17:11:26 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">//GGT
#include &lt;iostream&gt;
using namespace std;

// Rekursiv
int ggtrekursiv(int a, int b)
{
	if (a&gt;b)
	{
		if (a%b==0)
		{
			return b;
		}
		else
			ggtrekursiv(a, a%b);
	}

	else if (a&lt;b)
	{
		if (a%b==0)
		{
			return a;
		}
		else
			ggtrekursiv(b,a%b);
	}

}

int ggt( int a, int b)
{

	if (a&gt;b)
	{
		while (b!=0)
		{
			int c = a%b;
			a = b;
			b = c;
		}

		return a;
	}

	else if (a&lt;b)
	{
		while (a!=0)
		{
			int c = b%a;
			b = a;
			a = c;
		}

		return b;		
	}
}

int main()
{
	int a;
	int b;

	cout &lt;&lt; &quot;Bitte geben Sie zwei Integer ein: &quot; &lt;&lt; endl;
	cin &gt;&gt; a; cin &gt;&gt; b;

	cout &lt;&lt; &quot;Rekursiv: &quot; &lt;&lt; ggtrekursiv(a, b) &lt;&lt; endl;
	cout &lt;&lt; &quot;Iterative Loesung: &quot; &lt;&lt; ggt(a, b) &lt;&lt; endl;

	system (&quot;PAUSE&quot;);

	return 0;
}
</code></pre>
<p>ich habs.... ich dachte die ganze zeit ihr meint ich soll nur mti ab und b -.-</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1447710</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447710</guid><dc:creator><![CDATA[ThaRealMatix]]></dc:creator><pubDate>Fri, 01 Feb 2008 17:11:26 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 17:34:07 GMT]]></title><description><![CDATA[<p>Wie gesagt, die if-Abfragen sind unnötig. Die Algorithmen vertauschen die Werte automatisch. Also:</p>
<pre><code class="language-cpp">int gcd_iterative(int a, int b)
{
	while (b != 0)
	{
		int modulo = a % b;
		a = b;
		b = modulo;
	}
	return a;
}

int gcd_recursive(int a, int b)
{
	if (b == 0)
		return a;
	else
		return gcd_recursive(b, a % b);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1447726</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447726</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Fri, 01 Feb 2008 17:34:07 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 17:42:55 GMT]]></title><description><![CDATA[<p>hm sorry aber bei der rekursion ist das definitiv nicht so. ich bekomme da ein falsches ergebnis ohne if</p>
<pre><code class="language-cpp">if (a%b==0)
		{
			return b;
		}
		else
			return ggtrekursiv(a, a%b);
</code></pre>
<p>ist da a kleiner als b kommt der startwert von a raus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1447728</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447728</guid><dc:creator><![CDATA[ThaRealMatix]]></dc:creator><pubDate>Fri, 01 Feb 2008 17:42:55 GMT</pubDate></item><item><title><![CDATA[Reply to GGT Verstädnis Problem bzw Fehlersuche in meinem(!) Code on Fri, 01 Feb 2008 18:19:35 GMT]]></title><description><![CDATA[<p>ThaRealMatix schrieb:</p>
<blockquote>
<p>hm sorry aber bei der rekursion ist das definitiv nicht so. ich bekomme da ein falsches ergebnis ohne if</p>
<pre><code class="language-cpp">if (a%b==0)
		{
			return b;
		}
		else
			return ggtrekursiv(a, a%b);
</code></pre>
<p>ist da a kleiner als b kommt der startwert von a raus</p>
</blockquote>
<p>stimmt. Ist ja auch nicht das, was vorher gepostet wurde.</p>
<pre><code>return ggtrekursiv([b]b[/b], a%b);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1447739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1447739</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 01 Feb 2008 18:19:35 GMT</pubDate></item></channel></rss>