<?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[Dauerschleife? Warum?]]></title><description><![CDATA[<p>Also ich hätte da noch eien Frage:<br />
wie kann ich erreichen, dass ein Array unbekannter Länge eine zufällige Anordnunge bekommt.</p>
<p>Mein Quelltext dazu:</p>
<pre><code class="language-cpp">int anzahl;
int k;
int start;
cin &gt;&gt; anzahl;

int zahlen[anzahl];
int zahlen2[anzahl];

	for(k=0;k&lt;anzahl;k++)
	zahlen[k]=(k+1);

    srand(time(NULL));
    start = rand() % anzahl + 1;

	zahlen2[0]=start;
	zahlen[(start-1)]=0;

for(k=1;k&lt;anzahl;k++)

	{
	srand(start);
	start = rand() % anzahl + 1;

	if(zahlen[(start-1)]==0)
		{
		start=start+1;
		k--;
		}

	else
		{
		zahlen2[k]=start;
		zahlen[(start-1)]=0;
		}

	}
</code></pre>
<p>Allerdings geht das Programm für anzahls &gt; 2 nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Wieso?</p>
<p>Danke für jegliche Hilfe.</p>
<p>Gruß.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/173284/dauerschleife-warum</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 06:39:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/173284.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 13 Feb 2007 17:52:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 17:52:02 GMT]]></title><description><![CDATA[<p>Also ich hätte da noch eien Frage:<br />
wie kann ich erreichen, dass ein Array unbekannter Länge eine zufällige Anordnunge bekommt.</p>
<p>Mein Quelltext dazu:</p>
<pre><code class="language-cpp">int anzahl;
int k;
int start;
cin &gt;&gt; anzahl;

int zahlen[anzahl];
int zahlen2[anzahl];

	for(k=0;k&lt;anzahl;k++)
	zahlen[k]=(k+1);

    srand(time(NULL));
    start = rand() % anzahl + 1;

	zahlen2[0]=start;
	zahlen[(start-1)]=0;

for(k=1;k&lt;anzahl;k++)

	{
	srand(start);
	start = rand() % anzahl + 1;

	if(zahlen[(start-1)]==0)
		{
		start=start+1;
		k--;
		}

	else
		{
		zahlen2[k]=start;
		zahlen[(start-1)]=0;
		}

	}
</code></pre>
<p>Allerdings geht das Programm für anzahls &gt; 2 nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Wieso?</p>
<p>Danke für jegliche Hilfe.</p>
<p>Gruß.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228294</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228294</guid><dc:creator><![CDATA[CaribicSurfer]]></dc:creator><pubDate>Tue, 13 Feb 2007 17:52:02 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 17:59:02 GMT]]></title><description><![CDATA[<p>Ok, die Formulierung war ein bisschen ungünstig.</p>
<p>Also am Ende soll eine ungeordnete Zahlenfolge erscheinen der Länge N, in der jedes Element von 0 bis N genau einmal enthalten ist...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228296</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228296</guid><dc:creator><![CDATA[CaribicSurfer]]></dc:creator><pubDate>Tue, 13 Feb 2007 17:59:02 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 18:01:39 GMT]]></title><description><![CDATA[<p>CaribicSurfer schrieb:</p>
<blockquote>
<p>Allerdings geht das Programm für anzahls &gt; 2 nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
</blockquote>
<p>Boah, was fuer 'ne aussagekraeftige Fehlerbeschreibung...</p>
<p><a href="http://homepage1.nifty.com/algafield/sscce.html" rel="nofollow">http://homepage1.nifty.com/algafield/sscce.html</a><br />
<a href="http://www.catb.org/~esr/faqs/smart-questions.html" rel="nofollow">http://www.catb.org/~esr/faqs/smart-questions.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228297</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228297</guid><dc:creator><![CDATA[SG1]]></dc:creator><pubDate>Tue, 13 Feb 2007 18:01:39 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 18:08:59 GMT]]></title><description><![CDATA[<p>hmm.. also um ehrlich zu sein kann ich das Problem nicht weiter einschränken, denn mein Compiler gibt a.) keine Fehlermeldung aus und das Programm läuft b.) nur bis Anzahl == 2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228299</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228299</guid><dc:creator><![CDATA[CaribicSurfer]]></dc:creator><pubDate>Tue, 13 Feb 2007 18:08:59 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 18:41:30 GMT]]></title><description><![CDATA[<p>Ich hab dein Beispiel mal kompiliert, und weis immer noch nicht was du genau möchtest. Fragen wir mal so: WELCHE Schleife läuft nur bis zwei ?!</p>
<p>Ein kleiner Tipp, vielleicht bringt es dann die gewünschte Lösung: Setze mal für die erste for Schleife geschweifte Klammern, und du wirst merken, dass sie aufeinmal zählt ;). Die ist doch völlig offen, im Gegensatz zur zweiten, da hast du es richtig gemacht.</p>
<p>MfG mikey.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228307</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228307</guid><dc:creator><![CDATA[mikey]]></dc:creator><pubDate>Tue, 13 Feb 2007 18:41:30 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 19:12:28 GMT]]></title><description><![CDATA[<p>Hmm, nein, das ist es lieder nicht (ich habe es sogar mal so gelernt, dass man, wenn man innerhalb einer For-Schleife nur einen Befehl ausführt, man keine Klammern benötigt). Die erste For-Schleife dient also dazu, dass in dem Array Zahlen1 1,2,3,4,5 usw steht.<br />
also -&gt;</p>
<pre><code class="language-cpp">for(k=0;k&lt;anzahl;k++)
	{zahlen[k]=(k+1);}
</code></pre>
<p>Wie auch immer. Mein Anliegen ist folgendes:</p>
<p>Ich will, dass das Array, welches VOR der zweiten Schleife streng geordnet ist<br />
Zahlen[0] = 1<br />
Zahlen[1] = 2<br />
Zahlen[2] = 3<br />
Zahlen[3] = 4<br />
...<br />
nach der zweiten Schleife ungeordnet ist,<br />
z.B.<br />
Zahlen2[0] = 2<br />
Zahlen2[1] = 4<br />
Zahlen2[2] = 3<br />
Zahlen2[3] = 1</p>
<p>allerdings soll keine Zahl doppelt vorhanden sein.</p>
<p>Und das geht nur, solange Anzahl == 2 ist, für Anzahl == 3 hängt es sich auf (Dauerschleife)</p>
<p>Danke schonmal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228334</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228334</guid><dc:creator><![CDATA[CaribicSurfer]]></dc:creator><pubDate>Tue, 13 Feb 2007 19:12:28 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 19:43:02 GMT]]></title><description><![CDATA[<p>std::random_shuffle() wäre wohl nicht ganz angebracht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228344</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228344</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Tue, 13 Feb 2007 19:43:02 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 20:20:53 GMT]]></title><description><![CDATA[<p>der Verweiss auf die STD ist zwar manchmal sehr gut. Aber viele Dinge programmiert man ja um ein grundverständnis für eben solche Containerklassen zu bekommen. Ich errinnere mich noch an Algorithmen und Datenstrukturen wo wir eigene Hashmaps sowie Suchbäume gebaut haben oder auch Stacks etc. Obwohl es dafür schon genügend dinge gibt.</p>
<p>Aber zur eigentlichen fragen.<br />
Was genau soll deine zweite schleife erzielen? Ein zufälliges mischen würde ich damit erzielen das ich von 0 - Anzahl laufe.... eine zufallszahl &lt;= anzahl ziehe und das element mit dem an der zufälligen position tausche. Ist zwar nicht das non plus ultra an zufälligkeit. Aber es wäre ein ansatz.</p>
<p>schnelles pseudo C++ beispiel</p>
<pre><code class="language-cpp">for ( int i = 0; i &lt; anzahl; ++i)
{
    swapField = rand() % anzahl;
    int cur = feld[i];
    feld[i] = feld[swapField];
    delf[swapField] = cur;
}
</code></pre>
<p>da du beim erzeugen der zahlen schon gesichert hast das keine doppelt vorhanden ist passiert das hier ja auch nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228356</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228356</guid><dc:creator><![CDATA[Fedaykin]]></dc:creator><pubDate>Tue, 13 Feb 2007 20:20:53 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 21:02:18 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>erstmal Danke für die Antworten.</p>
<p>Also erstmal.. ich bin C++ Anfänger (relativ). Habs nur dieses Sem. gehabt und auch nur zwei Semesterwochenstunden (also praktisch garnicht).</p>
<p>Meine Idee war folgende:</p>
<p>Ich habe das geordnete Array. Nun Schreibe ich das in ein neues Array eine beliebige Zahl an die nullte Position, welche ein Element aus dem ersten Array ist. Die entsprechende Zahl wird aus der alten Liste gelöscht.</p>
<p>Nun suche ich mir erneut eine zufällige Zahl.</p>
<p>Nun überprüfe ich, ob in dem alten Array an entsprechender Position (zahl-1) noch eine Zahl steht (dieses Element also noch nicht in der NEUEN Liste vorhanden ist), oder ob eine Null drinnen steht (das Element also bereits in der neuen Liste vorhanden ist).</p>
<p>Den letzten Schritt führt man solange durch, bis nur noch Nullen in dem ersten Array stehen.</p>
<p>Es gibt sicherlich elegantere Möglichkeiten.</p>
<p>Meine Möglichkeit ging nicht, weil der Zufallsgenerator garnicht so zufüllige Zahlen mir würfelt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":-("
      alt="😞"
    /></p>
<p>Habe nun des Quelltext etwas abgeändert, und nun funktioniert es.</p>
<p>(Habe die Funktion bereits ins Hauptprogramm integriert, der Funktion wird das geordnete Array bereits übergeben)</p>
<p>[cpp]void zufall(int zahlen[],int anzahl)<br />
{</p>
<p>int k;<br />
int start;<br />
int zahlen2[anzahl];</p>
<p>srand(time(NULL));<br />
start = rand() % anzahl;</p>
<p>zahlen2[0]=start;<br />
zahlen[(start-1)]=0;</p>
<p><strong>int test=0;</strong><br />
for(k=1;k&lt;anzahl;k++)</p>
<p>{<br />
srand(start);<br />
start = rand() % anzahl;</p>
<p>if(zahlen[(start-1)]==0)<br />
{<br />
k=k-1;<br />
<strong>test=test+1233;</strong> }</p>
<p>else<br />
{<br />
test=0;<br />
zahlen2[k]=start;<br />
zahlen[(start-1)]=0;<br />
}</p>
<p><strong>start=start+test;</strong></p>
<p>}</p>
<p>for(k=0;k&lt;anzahl;k++)<br />
zahlen[k]=zahlen2[k];</p>
<p>}</p>
<p>[/cpp]</p>
<p>So wirklich zufällig sind die Zahlen zwar immer noch nicht, aber es langt für meine Bedürfnisse.</p>
<p>Gruß</p>
<p>p.s.: Was ist die Random(shuffle) funktion??</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228382</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228382</guid><dc:creator><![CDATA[CaribicSurfer]]></dc:creator><pubDate>Tue, 13 Feb 2007 21:02:18 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 21:11:31 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">void zufall(int zahlen[],int anzahl) 
{ 

int k;
int start;
int zahlen2[anzahl];

    srand(time(NULL));
    start = rand() % anzahl;

	zahlen2[0]=start;
	zahlen[(start-1)]=0;

int test=0;
for(k=1;k&lt;anzahl;k++)

	{
	srand(start);
	start = rand() % anzahl;

	if(zahlen[(start-1)]==0)
		{
		k=k-1;
		test=test+1233;
		}

	else
		{
		test=0;
		zahlen2[k]=start;
		zahlen[(start-1)]=0;
		}

	start=start+test;

	}

for(k=0;k&lt;anzahl;k++)
	zahlen[k]=zahlen2[k];

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1228385</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228385</guid><dc:creator><![CDATA[CaribicSurfer]]></dc:creator><pubDate>Tue, 13 Feb 2007 21:11:31 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 21:40:12 GMT]]></title><description><![CDATA[<p>Die Dauerschleife entsteht dadurch, dass Du 'k' immer dekrementierst, sobald keine Zahl !=0 gefunden wird. Und dass ist spätestens dann der Fall, wenn zahlen2 gefüllt und zahlen[] mit 0'en überschrieben wird.<br />
.. und es sucht ewig nach einer Zahl!=0</p>
<p>BTW. das srand in der for-SChleife ist überflüssig, bzw. sogar falsch und es muss wohl zahlen[start] statt start-1 heißen.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228409</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228409</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Tue, 13 Feb 2007 21:40:12 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Tue, 13 Feb 2007 22:11:10 GMT]]></title><description><![CDATA[<p>Werner Salomon schrieb:</p>
<blockquote>
<p>Die Dauerschleife entsteht dadurch, dass Du 'k' immer dekrementierst, sobald keine Zahl !=0 gefunden wird. Und dass ist spätestens dann der Fall, wenn zahlen2 gefüllt und zahlen[] mit 0'en überschrieben wird.<br />
.. und es sucht ewig nach einer Zahl!=0</p>
<p>BTW. das srand in der for-SChleife ist überflüssig, bzw. sogar falsch und es muss wohl zahlen[start] statt start-1 heißen.</p>
<p>Gruß<br />
Werner</p>
</blockquote>
<p>Und warum geht dann das Programm, nachdem ich die Variable TEST eingebaut habe??</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1228426</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1228426</guid><dc:creator><![CDATA[CaribicSurfer]]></dc:creator><pubDate>Tue, 13 Feb 2007 22:11:10 GMT</pubDate></item><item><title><![CDATA[Reply to Dauerschleife? Warum? on Wed, 14 Feb 2007 23:09:52 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">void zufall(int zahlen[],int anzahl) 
{ 

int k; 
int start; 
int zahlen2[anzahl];
</code></pre>
<p>Äh, sorry, aber das geht in C++ garnicht. In C ja, in C++ nein.<br />
Würde mich interessieren welcher Compiler das ist dass der das frisst...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1229146</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1229146</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 14 Feb 2007 23:09:52 GMT</pubDate></item></channel></rss>