<?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[Einbauen einer Schleife]]></title><description><![CDATA[<p>Hallo, ich möchte gern ein kleines Programm schreiben mit dem man Nullstellen einer Funktion berechenen kann.<br />
Wenn es eine Nullstelle gibt funktioniert das Programm auch nur wenn es keine<br />
gibt wird die if Bedingung endlos wiederholt.<br />
Wie kann ich jetzt eine Schleife einbauen, dass die if Bedingung z.B. 100mal<br />
wiederholt und wenn dann immernoch xn!=x ist irgendeine Meldung ausgegeben wird?</p>
<p>Hab das hier alles ein bischen gekürzt, aber von Prinzip ist es das gleiche:</p>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;
#include &quot;iostream.h&quot;

int main(int argc, char* argv[])
{

	float a,b ;    //Zahlen bzw. Multiplikatorn der Gleichung
    float x,xn;     //x und y Wert 

 	cout &lt;&lt;&quot;\n Bitte Wert fuer a eingeben:&quot;;   //Einlesen Wert von a
	cin &gt;&gt; (a);
	cout &lt;&lt;&quot;\n Bitte Wert fuer b eingeben:&quot;;   //Einlesen Wert von b
	cin &gt;&gt; (b);

	cout &lt;&lt;&quot;\n\n&quot;;

	cout &lt;&lt;&quot;\n Gewuenschten Wert fuer x eingeben (Startwert):&quot;; //Einlesen des Gewünschten x Wertes
	cin &gt;&gt; (x);
	cout &lt;&lt;&quot;\n\n&quot;;

Gleichung: //An diese Stelle wird gesprungen, wenn xn ungleich x ist

	xn = x-((a * x*x)+b)/(2*a * x); //Berechnet die Funktion
	x = xn-((a * xn*xn)+b)/(2*a * xn);

	if(xn!=x)
		{  
			goto Gleichung;	
	}

	else if(xn==x)
		{
			cout&lt;&lt;&quot; Nullstelle ist:&quot;&lt;&lt;xn;
	}

return 0;
}
</code></pre>
<p>Vielen Dank schon mal für eure Hilfe.</p>
<p>Gruß marwin</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/138053/einbauen-einer-schleife</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 00:44:50 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/138053.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 22 Feb 2006 23:37:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Einbauen einer Schleife on Wed, 22 Feb 2006 23:37:45 GMT]]></title><description><![CDATA[<p>Hallo, ich möchte gern ein kleines Programm schreiben mit dem man Nullstellen einer Funktion berechenen kann.<br />
Wenn es eine Nullstelle gibt funktioniert das Programm auch nur wenn es keine<br />
gibt wird die if Bedingung endlos wiederholt.<br />
Wie kann ich jetzt eine Schleife einbauen, dass die if Bedingung z.B. 100mal<br />
wiederholt und wenn dann immernoch xn!=x ist irgendeine Meldung ausgegeben wird?</p>
<p>Hab das hier alles ein bischen gekürzt, aber von Prinzip ist es das gleiche:</p>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;
#include &quot;iostream.h&quot;

int main(int argc, char* argv[])
{

	float a,b ;    //Zahlen bzw. Multiplikatorn der Gleichung
    float x,xn;     //x und y Wert 

 	cout &lt;&lt;&quot;\n Bitte Wert fuer a eingeben:&quot;;   //Einlesen Wert von a
	cin &gt;&gt; (a);
	cout &lt;&lt;&quot;\n Bitte Wert fuer b eingeben:&quot;;   //Einlesen Wert von b
	cin &gt;&gt; (b);

	cout &lt;&lt;&quot;\n\n&quot;;

	cout &lt;&lt;&quot;\n Gewuenschten Wert fuer x eingeben (Startwert):&quot;; //Einlesen des Gewünschten x Wertes
	cin &gt;&gt; (x);
	cout &lt;&lt;&quot;\n\n&quot;;

Gleichung: //An diese Stelle wird gesprungen, wenn xn ungleich x ist

	xn = x-((a * x*x)+b)/(2*a * x); //Berechnet die Funktion
	x = xn-((a * xn*xn)+b)/(2*a * xn);

	if(xn!=x)
		{  
			goto Gleichung;	
	}

	else if(xn==x)
		{
			cout&lt;&lt;&quot; Nullstelle ist:&quot;&lt;&lt;xn;
	}

return 0;
}
</code></pre>
<p>Vielen Dank schon mal für eure Hilfe.</p>
<p>Gruß marwin</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1000731</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1000731</guid><dc:creator><![CDATA[marwin]]></dc:creator><pubDate>Wed, 22 Feb 2006 23:37:45 GMT</pubDate></item><item><title><![CDATA[Reply to Einbauen einer Schleife on Thu, 23 Feb 2006 06:56:58 GMT]]></title><description><![CDATA[<p>wie wäre es mit einer for schleife</p>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;//die kenn ich nicht 
#include &quot;iostream.h//das sollte hier #include &lt;iostream&gt; heisen 
// dann brauchst du noch 
using namespace std;
//weil die nicht nach dem iso standart ist
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1000777</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1000777</guid><dc:creator><![CDATA[g]]></dc:creator><pubDate>Thu, 23 Feb 2006 06:56:58 GMT</pubDate></item><item><title><![CDATA[Reply to Einbauen einer Schleife on Thu, 23 Feb 2006 07:06:14 GMT]]></title><description><![CDATA[<p>und/oder einer while schleife</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1000784</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1000784</guid><dc:creator><![CDATA[ser1al]]></dc:creator><pubDate>Thu, 23 Feb 2006 07:06:14 GMT</pubDate></item><item><title><![CDATA[Reply to Einbauen einer Schleife on Thu, 23 Feb 2006 07:16:39 GMT]]></title><description><![CDATA[<p>btw solltest du diese goto-Gehüpfe durch eine echte Schleife ersetzen:</p>
<pre><code class="language-cpp">int counter;
while(xn!=x&amp;&amp;counter&lt;max_counter)//der counter bricht die Rechnung ab, wenn es zu lange dauert
{
  xn=...;
  x=...;
  ++counter;
}

if(xn==x)
  cout&lt;&lt;&quot;Nullstelle gefunden:&quot;&lt;&lt;xn&lt;&lt;endl;
else
  cout&lt;&lt;&quot;keine Nullstelle.&quot;&lt;&lt;endl;
</code></pre>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/8873">@g</a>: &quot;stdafx.h&quot; sind die vorkompilierten Header von Visual Studio <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/1000787</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1000787</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 23 Feb 2006 07:16:39 GMT</pubDate></item><item><title><![CDATA[Reply to Einbauen einer Schleife on Thu, 23 Feb 2006 21:22:02 GMT]]></title><description><![CDATA[<p>Anbei die Lösung für die Fortgeschrittenen.<br />
Es wird streng getrennt zwischen der Funktion, dem Lösungsalgorithmus und der Eingabe der Parameter und Ergebnisdarstellung.<br />
Es ist zu empfehlen Fließkommavariablen nicht auf Gleichheit sondern auf eine zulässige Abweichung (EPS) hin abzufragen.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;utility&gt;      // std::make_pair
#include &lt;cmath&gt;        // std::abs

struct Funktion
{
    Funktion( double a, double b )
        : m_a( a )
        , m_b( b )
    {}
    double operator()( double x ) const
    {
        return ( m_a * x * x + m_b ) / ( 2 * m_a * x );
    }
private:
    double m_a;
    double m_b;
};

// --   solve: findet eine Nullstelle von 'f'; beginned bei 'x'
//      EPS gibt die zulässige Abweichung des Funktionswertes von 0 an
//      N ist die maximale Anzahl der Iterations-Schritte
//      Return-Wert.second ist true, falls eine Nullstelle gefunden wurde
template&lt; typename F &gt;
std::pair&lt; double, bool &gt; solve( F f, double x, const double EPS, const int N )
{
    int n = 0;
    for( double y; x -= (y = f( x )), std::abs( y ) &gt; EPS &amp;&amp; n &lt; N; ++n )
        ;
    return std::make_pair( x, n &lt; N );
}

int main()
{
    using namespace std;
    cout &lt;&lt; &quot;Bitte zwei Zahlen eingeben: &quot;;
    double a, b;
    cin &gt;&gt; a &gt;&gt; b;
    cout &lt;&lt; &quot;Bitte Startwert eingeben: &quot;;
    double x;
    cin &gt;&gt; x;
    const Funktion f( a, b );
    const std::pair&lt; double, bool &gt; ret = solve( f, x, 1.E-8, 20 );
    if( ret.second )
    {
        cout &lt;&lt; &quot;Die Nullstelle liegt bei: &quot; &lt;&lt; ret.first &lt;&lt; endl;
        cout &lt;&lt; &quot;F( &quot; &lt;&lt; ret.first &lt;&lt; &quot; ) = &quot; &lt;&lt; f( ret.first ) &lt;&lt; endl;
    }
    else
    {
        cout &lt;&lt; &quot;keine Nullstelle gefunden&quot; &lt;&lt; endl;
    }
}
</code></pre>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1001538</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1001538</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Thu, 23 Feb 2006 21:22:02 GMT</pubDate></item><item><title><![CDATA[Reply to Einbauen einer Schleife on Thu, 23 Feb 2006 22:00:54 GMT]]></title><description><![CDATA[<p>Danke erstmal an alle für die Mühe. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
<p>@ CStoll, wenn ich den Code rein kopiere, dann sagt er mir:</p>
<pre><code class="language-cpp">: error C2065: 'max_counter' : nichtdeklarierter Bezeichner
 : error C2059: Syntaxfehler : '...'
 : error C2059: Syntaxfehler : '...'
</code></pre>
<p>Muss ich noch sonst irgendetwas ändern???</p>
<p>Gruß marwin</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1001551</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1001551</guid><dc:creator><![CDATA[marwin]]></dc:creator><pubDate>Thu, 23 Feb 2006 22:00:54 GMT</pubDate></item><item><title><![CDATA[Reply to Einbauen einer Schleife on Fri, 24 Feb 2006 00:13:36 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>btw solltest du diese goto-Gehüpfe durch eine echte Schleife ersetzen:</p>
</blockquote>
<p>Ach Quatsch. Das ist schon in Ordnung so. <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/1001593</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1001593</guid><dc:creator><![CDATA[Apollon]]></dc:creator><pubDate>Fri, 24 Feb 2006 00:13:36 GMT</pubDate></item><item><title><![CDATA[Reply to Einbauen einer Schleife on Fri, 24 Feb 2006 07:24:42 GMT]]></title><description><![CDATA[<p>marwin schrieb:</p>
<blockquote>
<p>Danke erstmal an alle für die Mühe. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
<p>@ CStoll, wenn ich den Code rein kopiere, dann sagt er mir:</p>
<pre><code class="language-cpp">: error C2065: 'max_counter' : nichtdeklarierter Bezeichner
 : error C2059: Syntaxfehler : '...'
 : error C2059: Syntaxfehler : '...'
</code></pre>
<p>Muss ich noch sonst irgendetwas ändern???</p>
<p>Gruß marwin</p>
</blockquote>
<p>Du solltst den Code nicht 1 zu 1 übernehmen, sondern an deine Bedürfnisse anpassen - und die &quot;...&quot;-Sequenzen ersetzt du natürlich durch sinnvolle Code-Elemente aus deinem Programm.</p>
<p>(so viel Verständnis von C++ hätte ich dir eigentlich schon zugetraut)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1001646</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1001646</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Fri, 24 Feb 2006 07:24:42 GMT</pubDate></item></channel></rss>