<?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[findet jemand den Fehler?]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;algorithm&gt;
#include &lt;iomanip&gt;
#include &lt;iostream&gt;
#include &lt;istream&gt;
#include &lt;iterator&gt;
#include &lt;numeric&gt;
#include &lt;ostream&gt;
#include &lt;vector&gt;

int main()
{
std::vector&lt;int&gt; v;
std::copy(std::istream_iterator&lt;int&gt;(std::cin), std::istream_iterator&lt;int&gt;(), std::back_inserter(v));
int avg = std::accumulate(v.begin(), v.end(), 0) / v.size();
std::cout &lt;&lt; avg &lt;&lt; std::endl;
}
</code></pre>
<p>Das Programm soll den Mittelwert der eingegebenen Werte liefern. In den meisten Faellen funktioniert es, aber manchmal wird ein falsches Ergebnis geliefert. Weis jemand wieso?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/209473/findet-jemand-den-fehler</link><generator>RSS for Node</generator><lastBuildDate>Mon, 12 Oct 2026 20:17:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/209473.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 31 Mar 2008 09:54:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to findet jemand den Fehler? on Mon, 31 Mar 2008 09:54:14 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;algorithm&gt;
#include &lt;iomanip&gt;
#include &lt;iostream&gt;
#include &lt;istream&gt;
#include &lt;iterator&gt;
#include &lt;numeric&gt;
#include &lt;ostream&gt;
#include &lt;vector&gt;

int main()
{
std::vector&lt;int&gt; v;
std::copy(std::istream_iterator&lt;int&gt;(std::cin), std::istream_iterator&lt;int&gt;(), std::back_inserter(v));
int avg = std::accumulate(v.begin(), v.end(), 0) / v.size();
std::cout &lt;&lt; avg &lt;&lt; std::endl;
}
</code></pre>
<p>Das Programm soll den Mittelwert der eingegebenen Werte liefern. In den meisten Faellen funktioniert es, aber manchmal wird ein falsches Ergebnis geliefert. Weis jemand wieso?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1483626</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1483626</guid><dc:creator><![CDATA[DEvent]]></dc:creator><pubDate>Mon, 31 Mar 2008 09:54:14 GMT</pubDate></item><item><title><![CDATA[Reply to findet jemand den Fehler? on Mon, 31 Mar 2008 10:04:02 GMT]]></title><description><![CDATA[<p>Vermutlich ein Rundungsfehler durch &quot;int avg&quot; ?</p>
<p>Bei Division würde ich jedenfalls float oder double nehmen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1483631</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1483631</guid><dc:creator><![CDATA[modestia]]></dc:creator><pubDate>Mon, 31 Mar 2008 10:04:02 GMT</pubDate></item><item><title><![CDATA[Reply to findet jemand den Fehler? on Mon, 31 Mar 2008 10:09:39 GMT]]></title><description><![CDATA[<p>Du dividierst nur integrale Zahlenwerte, d.h. der Nachkommaanteil wird ignoriert. Caste mal v.size() in einen double und mach aus avg auch einen double.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1483636</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1483636</guid><dc:creator><![CDATA[Tyrdal]]></dc:creator><pubDate>Mon, 31 Mar 2008 10:09:39 GMT</pubDate></item><item><title><![CDATA[Reply to findet jemand den Fehler? on Wed, 02 Apr 2008 04:58:28 GMT]]></title><description><![CDATA[<p>Tyrdal schrieb:</p>
<blockquote>
<p>Du dividierst nur integrale Zahlenwerte, d.h. der Nachkommaanteil wird ignoriert. Caste mal v.size() in einen double und mach aus avg auch einen double.</p>
</blockquote>
<p>Vielleicht haette ich sagen sollen &quot;Mittelwert ohne Nachkomma&quot;.</p>
<p>Nein, das ist nicht der Fehler. Tipp: Der Fehler tritt auf, wenn std::accumulate() eine negative Summe zurueckliefert. Dann koennte es passieren, dass der Mittelwert ganz und gar falsch ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1484921</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1484921</guid><dc:creator><![CDATA[DEvent]]></dc:creator><pubDate>Wed, 02 Apr 2008 04:58:28 GMT</pubDate></item><item><title><![CDATA[Reply to findet jemand den Fehler? on Wed, 02 Apr 2008 07:04:42 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int avg = std::accumulate(v.begin(), v.end(), 0) / static_cast&lt;int&gt;(v.size());
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1484961</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1484961</guid><dc:creator><![CDATA[Airdamn]]></dc:creator><pubDate>Wed, 02 Apr 2008 07:04:42 GMT</pubDate></item><item><title><![CDATA[Reply to findet jemand den Fehler? on Wed, 02 Apr 2008 08:17:10 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">if( !v.empty())
{
  int avg = std::accumulate(v.begin(), v.end(), 0) / static_cast&lt;int&gt;(v.size());
// weiter
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1484998</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1484998</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Wed, 02 Apr 2008 08:17:10 GMT</pubDate></item><item><title><![CDATA[Reply to findet jemand den Fehler? on Wed, 02 Apr 2008 09:15:49 GMT]]></title><description><![CDATA[<p>bei den hier genannten Lösungen musst du aber dann noch aufpassen, dass v.size() einen bestimmten Wert nicht überschreitet(2^31? nicht ganz sicher), denn int nutzt das höchstwertigste bit als vorzeichenbit.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1485039</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1485039</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Wed, 02 Apr 2008 09:15:49 GMT</pubDate></item><item><title><![CDATA[Reply to findet jemand den Fehler? on Wed, 02 Apr 2008 17:02:21 GMT]]></title><description><![CDATA[<p>So schnell wird er den std::vector wohl nicht mit 2 Milliarden Werten füllen können, denn das wären dann ja schon 8 GB Hauptspeicher...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1485382</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1485382</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Wed, 02 Apr 2008 17:02:21 GMT</pubDate></item></channel></rss>