title: Finding average and standard deviation from numbers read from a file
body: <p>Evening all, I have a problem assignment that asks to write a program that reads in a bunch of numbers from a file and outputs the letter grades, average, and standard deviation. The letter grades and average I have taken care of, but I'm having trouble with the standard deviation. The issue is that my program reads in all the values and calculates the average after it has done so. Since the formula for standard deviation requires using the particular data point AND the average, well, I'm just not sure how to go about it. One idea I had was to try making the program open the file twice, so that on the second run (by which point it has the average calculated) it can calculate the standard deviation. However, the compiler does not like this at all. Any advice?</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  int main(){
  ifstream file("grades.txt");
  double number,sum=0,noNumbers=0,average=0;
  int noAs=0, noBs=0, noCs=0, noDs=0, noFs=0, noAms=0, noBms=0, noCms=0, noDms=0, noAps=0, noBps=0, noCps=0, noDps=0, quant=0, quanttot=0;
  
  while(file &gt;&gt; number){
      if(number &gt;= 97 &amp;&amp; number &lt;=100 ){
      noAps++;
      }
      if(number &gt;= 93 &amp;&amp; number &lt; 97){
      noAs++;
      }
      if(number &gt;= 90 &amp;&amp; number &lt; 93){
      noAms++;
      }
  if(number &gt;= 87 &amp;&amp; number &lt;90 ){
      noBps++;
      }
      if(number &gt;= 83 &amp;&amp; number &lt; 87){
      noBs++;
      }
      if(number &gt;= 80 &amp;&amp; number &lt; 83){
      noBms++;
      }
      if(number &gt;= 77 &amp;&amp; number &lt; 80 ){
      noCps++;
      }
      if(number &gt;= 73 &amp;&amp; number &lt; 77){
      noCs++;
      }
      if(number &gt;= 70 &amp;&amp; number &lt; 73){
      noCms++;
      }
      if(number &gt;= 67 &amp;&amp; number &lt; 70 ){
      noDps++;
      }
      if(number &gt;= 63 &amp;&amp; number &lt; 67){
      noDs++;
      }
      if(number &gt;= 60 &amp;&amp; number &lt; 63){
      noDms++;
      }
      if(number &lt; 60){
      noFs++;
      }
      noNumbers++;
      sum = sum += number;
  }
  average = sum / noNumbers;
  while(file &gt;&gt; number){
      quant = (number - average)*(number-average);
      quanttot = quanttot + quant;
  }
  double sigma;
  sigma = sqrt(quanttot/noNumbers);
  cout &lt;&lt; "Average is: " &lt;&lt; average &lt;&lt; endl;
  cout &lt;&lt; "Standard deviation is: " &lt;&lt; sigma &lt;&lt; endl;
  ofstream newfile("grades2.txt");
  newfile &lt;&lt;  "A+'s: " &lt;&lt; noAps &lt;&lt; endl 
      &lt;&lt; "A's: " &lt;&lt; noAs &lt;&lt; endl 
      &lt;&lt;"A-'s: " &lt;&lt; noAms &lt;&lt; endl 
      &lt;&lt;"B+'s: " &lt;&lt; noBps &lt;&lt; endl 
      &lt;&lt; "B's: " &lt;&lt; noBs &lt;&lt; endl 
      &lt;&lt; "B-'s: " &lt;&lt; noBms &lt;&lt; endl 
      &lt;&lt;"C+'s: " &lt;&lt; noCps &lt;&lt; endl 
      &lt;&lt;"C's: " &lt;&lt; noCs &lt;&lt; endl 
      &lt;&lt;"C-'s: " &lt;&lt; noCms &lt;&lt; endl 
      &lt;&lt;"D+'s: " &lt;&lt; noDps &lt;&lt; endl 
      &lt;&lt;"D's: " &lt;&lt; noDs &lt;&lt; endl 
      &lt;&lt; "D-'s: " &lt;&lt; noDms &lt;&lt; endl 
      &lt;&lt; "F's: " &lt;&lt; noFs &lt;&lt; endl;
  newfile.close();
  cout &lt;&lt; "Press any key to continue...THEN PRESS ENTER!\n:";
  int f;
  cin &gt;&gt; f;
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <hr>
  
  <p>Ah, saving the numbers into an array to manipulate later was a good idea indeed. So I changed my code to this:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  int main(){
  ifstream file("grades.txt");
  double sum=0,noNumbers=0,average=0;
  int noAs=0, noBs=0, noCs=0, noDs=0, noFs=0, noAms=0, noBms=0, noCms=0, noDms=0, noAps=0, noBps=0, noCps=0, noDps=0, quant=0, quanttot=0;
  int i = 1;
  double number[500];
  while(file &gt;&gt; number[i]){
  while(number[i]&lt;=100){
  
      if(number[i] &gt;= 97 &amp;&amp; number[i] &lt;=100){
      noAps++;
      }
      if(number[i] &gt;= 93 &amp;&amp; number[i] &lt; 97){
      noAs++;
      }
      if(number[i] &gt;= 90 &amp;&amp; number[i] &lt; 93){
      noAms++;
      }
      if(number[i] &gt;= 87 &amp;&amp; number[i] &lt; 90){
      noBps++;
      }
      if(number[i] &gt;= 83 &amp;&amp; number[i] &lt; 87){
      noBs++;
      }
      if(number[i] &gt;= 80 &amp;&amp; number[i] &lt; 83){
      noBms++;
      }
      if(number[i] &gt;= 77 &amp;&amp; number[i] &lt; 80){
      noCps++;
      }
      if(number[i] &gt;= 73 &amp;&amp; number[i] &lt; 77){
      noCs++;
      }
      if(number[i] &gt;= 70 &amp;&amp; number[i] &lt; 73){
      noCms++;
      }
      if(number[i] &gt;= 67 &amp;&amp; number[i] &lt; 70){
      noDps++;
      }
      if(number[i] &gt;= 63 &amp;&amp; number[i] &lt; 67){
      noDs++;
      }
      if(number[i] &gt;= 60 &amp;&amp; number[i] &lt; 63){
      noDms++;
      }
      if(number[i] &lt; 60){
      noFs++;
      }
      noNumbers++;
      i++;
  }
  }
  for(int i=0; i = noNumbers; i++){
      sum = sum + number[i];
  }
  average = sum/noNumbers;
  
  for(int i=0; i=noNumbers;i++){
      quant = (number[i] - average)*(number[i] - average);
      quanttot = quanttot + quant;
  }
  
  double sigma;
  sigma = sqrt(quanttot/noNumbers);
  cout &lt;&lt; "Average is: " &lt;&lt; average &lt;&lt; endl;
  cout &lt;&lt; "Standard deviation is: " &lt;&lt; sigma &lt;&lt; endl;
  ofstream newfile("grades2.txt");
  newfile &lt;&lt;  "A+'s: " &lt;&lt; noAps &lt;&lt; endl 
      &lt;&lt; "A's: " &lt;&lt; noAs &lt;&lt; endl 
      &lt;&lt;"A-'s: " &lt;&lt; noAms &lt;&lt; endl 
      &lt;&lt;"B+'s: " &lt;&lt; noBps &lt;&lt; endl 
      &lt;&lt; "B's: " &lt;&lt; noBs &lt;&lt; endl 
      &lt;&lt; "B-'s: " &lt;&lt; noBms &lt;&lt; endl 
      &lt;&lt;"C+'s: " &lt;&lt; noCps &lt;&lt; endl 
      &lt;&lt;"C's: " &lt;&lt; noCs &lt;&lt; endl 
      &lt;&lt;"C-'s: " &lt;&lt; noCms &lt;&lt; endl 
      &lt;&lt;"D+'s: " &lt;&lt; noDps &lt;&lt; endl 
      &lt;&lt;"D's: " &lt;&lt; noDs &lt;&lt; endl 
      &lt;&lt; "D-'s: " &lt;&lt; noDms &lt;&lt; endl
      &lt;&lt; "F's: " &lt;&lt; noFs &lt;&lt; endl
      &lt;&lt; "Average: " &lt;&lt; average &lt;&lt; endl
      &lt;&lt; "Standard Deviation: " &lt;&lt; sigma &lt;&lt; endl;
  newfile.close();
  cout &lt;&lt; "Press any key to continue...THEN PRESS ENTER!\n:";
  int f;
  cin &gt;&gt; f;
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>However, now I get a crazy error that forces a break and takes me to some mumbojumbo code within the compiler. Does anyone see any errors that could cause this? To be specific, the error it gives me is in a popup window and says: "Unhandled exception at 0x5A221D7F (msvcp110d.dll) in ConsoleApplication79.exe: 0xC0000005: Access violation reading location 0x4050C000." Also, there are somewhere between 300 and 450 numbers in the text file so the array shouldn't be running out of slots.</p>