title: Why a portion of my Array output a value different from what i extracted(c++)?
body: <p>data11.txt:</p>
  <pre><code>Length(l) Time(t)   Period(T)
  200   10.55     0.527
  300   22.72     1.136
  400   26.16     1.308
  500   28.59     1.429
  600   31.16     1.558
  </code></pre>
  <p>ill be taking data from this file above in my code</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  
  /*Using data11.txt as reference, write code to read from the file. In a pendulum experiment certain data was recorded in order to calculate the acceleration due to gravity. All calculations should be in their SI units
  
  Write a function to calculate the acceleration due to gravity as the slope of the graph to this equation.T=2πlg−−√.
  What is the percentage error in your calculated value? Take g=9.8ms-2.
  If the pendulum weighs 50g and is displaced at an angle of 30, what will it maximum kinetic energy value for each length of the string? Display your answer in table form. */
  
  using namespace std;
  double slope_gravity(double T1, double T2, double L1, double L2){//slope function
      double T1_inverse = 1/pow(T1,2);
      double T2_inverse = 1/pow(T2,2);
      double difference_T = T2_inverse - T1_inverse;
      double difference_L = (L2 - L1)/100;
      double slope = (difference_T/difference_L);
      return slope;
  }
  
  const int n = 4;
  
  int main (){
      ifstream mydata;
      string capture;
      float Length[n], Time[n], Period[n], acc_gravity;//declaring variable
      
      mydata.open(&quot;Data11.txt&quot;);
      if (mydata.is_open()){
          for (int i=0; i&lt;n+1; i++){
              getline(mydata, capture);
              mydata &gt;&gt; Length[i] &gt;&gt; Time[i] &gt;&gt; Period[i];
          }
          acc_gravity = slope_gravity(Period[1], Period[0], Length[1], Length[0]);//acceleration due to gravity calc
          cout &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setprecision(1);
          cout &lt;&lt; &quot;The Acceleration Due To Gravity Obtained Experimentally &quot;
               &lt;&lt; &quot;from the data11 file is &quot;&lt;&lt;abs(acc_gravity)&lt;&lt;&quot;ms^-2&quot;&lt;&lt;endl;
          cout &lt;&lt; &quot;Calculating for Percentage error.............&quot;;
          cout &lt;&lt;endl&lt;&lt;endl;
          float percent_error = (abs(acc_gravity)- 9.8) * 100/9.8;//error analysis
          cout &lt;&lt; &quot;Percent Error is &quot;&lt;&lt; setprecision(2) &lt;&lt; abs(percent_error)&lt;&lt;&quot;%&quot;&lt;&lt;endl;
          cout &lt;&lt; endl;
          cout &lt;&lt; &quot;Calculating Max Kinetic Energy of pendulum weighing 50g being &quot;
               &lt;&lt; &quot;displaced at an angle of 30\n&quot;;
          
          cout &lt;&lt; setprecision(4);
          float velocity[n], x[n], y;//kinetic energy calc
          double max_KE[n];
          
          for (int j=0; j&lt;n+1; j++){
          x[j] = 2 * acc_gravity * Length[j]/100;
          y = 1 - cos(30);
          velocity[j] = sqrt(x[j] * y);
          
          max_KE[j] = 0.5 * 50/1000 * pow(velocity[j],2);
          }
          
          cout&lt;&lt;endl&lt;&lt;endl;
          
          cout &lt;&lt; setprecision(1);
          cout &lt;&lt; &quot;Length(m)\tMaximum Kinetic Energy(J)\n&quot;;//tabular form display
          for (int k=0; k&lt;n+1; k++){
              cout &lt;&lt; Length[k] &lt;&lt;&quot;\t&quot;&lt;&lt; max_KE[k] &lt;&lt;endl;    
              }
              
          }
          mydata.close();
      
      return 0;
  }
  </code></pre>
  <p>The objective of the program is in the first comment above.</p>
  <p>At the end of the program, i'm supposed to print the values of length and Maximum Kinetic energy to the console in a tabular form.
  However, in the array Length[0], it prints 31.16, but i extracted data from data11.txt, so there is supposed to be the value 200 instead.</p>
  <p>The origin of the problem start with this for loop. The for loop works well by extracting values from data11.txt. so the value of Length[0] is 200 here.</p>
  <p>i've tried to check why it is behaving this way. The problem starts here;</p>
  <pre><code>    mydata.open(&quot;Data11.txt&quot;);
      if (mydata.is_open()){
          for (int i=0; i&lt;n+1; i++){
              getline(mydata, capture);
              mydata &gt;&gt; Length[i] &gt;&gt; Time[i] &gt;&gt; Period[i];
          }
  </code></pre>
  <p>But outside the forloop, Length[0] is 31.16. I also noticed that Time[4] has different value from input value which is supposed to be 31.16.</p>
  <p>Please any help with this?</p>