Number of instructions not comming correctly and moving out of int range
<p>I am a novice to programming and computing.</p>

<p>I am running a C++ based program that is taking approx 6 hours on this machine. I use the timing utility of the <a href="http://root.cern.ch" rel="nofollow">framework</a> I work in. </p>

<p>I tried calculating the total number of iterations of my nested loop via a simple program:</p>

<pre><code>{
int k=0;
for (int i = 0; i &lt; 196779; i++)
for (int j= i+1; j&lt; 196779; j++)
{
k++;
if((k+1)%10000 == 0)
cout&lt;&lt; "\n Number of Instructions: " &lt;&lt; k;
}
cout&lt;&lt; "\n Total Number of iterations = " &lt;&lt; k &lt;&lt; endl;
}
</code></pre>

<p>Mathematically I would expect it to <a href="http://www.wolframalpha.com/input/?i=196779%20choose%202" rel="nofollow">agree with</a> the value 1.9360889031 × 10^10 , which is the total number of 2-element subsets. I inserted that cout statement to see if something funny was happening, and indeed it does.</p>

<ol>
<li>The outpute exceeds the mathematically expected value. Is my calculation wrong?</li>
<li>The output goes into negative values after a while as it exceeds the int range, but it shouldn't.</li>
</ol>

<p>Sample output at the end where I manually break</p>

<pre><code>Number of Instructions: -2078590001
 Number of Instructions: -2078580001
 Number of Instructions: -2078570001
 Number of Instructions: -2078560001
</code></pre>

<p>I found out the range of Int to be 2147483647, but I had made the calculations and concluded that my k should never exceed the limit. So where is the problem?</p>
