title: As a student, how can I improve poor code?
body: <p>How do I improve this spaghetti code? I am a student and this is me being slow in my first programming course.</p>
  
  <p><strong>What I think I want to achieve</strong></p>
  
  <p>Find an alternating sum up to a number.</p>
  
  <p><strong>Problem overview</strong></p>
  
  <p><a href="https://www.hackerrank.com/contests/cs102-s18-march31/challenges/treasure-road" rel="nofollow noreferrer">https://www.hackerrank.com/contests/cs102-s18-march31/challenges/treasure-road</a></p>
  
  <p>This is a problem from the archive at provided by the teacher. I have deduced that an alternating sum has to be calculated, where the sum of the first <em>odd integers</em> has to be subtracted from the sum of the first <em>even integers</em> (of course up to #ofsteps/2).</p>
  
  <p><strong>What I did</strong></p>
  
  <pre><code>#include &lt;cmath&gt;
  #include &lt;cstdio&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  
  int main() {
      int arrodds[100]={0},arrevens[100]={0},sumevens=0,sumodds=0;
      int steps;
      cin &gt;&gt; steps;
      int i=0;
      while(i&lt;steps/2)
      {
          if(i%2==0)
              arrevens[i]=i;
          else arrodds[i]=i;
          i++;
      }
      for(int d=0;d&lt;100;d++)
          sumevens+=arrevens[d];
      for(int h=0;h&lt;100;h++)
          sumodds+=arrodds[h];
  
      cout &lt;&lt; sumodds-sumevens;
      return 0;
  }
  </code></pre>
  
  <p><strong>My logic/reasoning</strong></p>
  
  <p>Zingo can only move Steps/2, if he moves an odd step he moves forward, and Ringo moves a step closer to to Zingo, else if Zingo moves an even step Ringo moves away from Zingo.</p>
  
  <p>Let's say there are 10 steps... 10/5=5... 5 steps= 1+2+3+4+5, odd steps=1,3,5; even steps=2,4; so displacement is 1+3-(2+4)=-2.</p>
  
  <p><strong>The code</strong></p>
  
  <p>I try to store the even and odd numbers in different arrays, then take the sum of each one and subtract the sums.</p>
  
  <p><strong>What's wrong?</strong> </p>
  
  <p>My code works for small numbers it seems, as some test cases go beyond what I think My code could handle.</p>
  
  <p><strong>What I tried</strong></p>
  
  <p>I tried playing around with the sizes of my arrays but didn't get what I want.</p>
  
  <p>Should I abandon this code and try something else? If not, how can I fix this abomination and guide me through it?</p>