title: Objective-C: Smooth NSArray of NSNumber objects
body: <p>I have an NSArray which contains a large number of NSNumber objects (thousands).  I use these numbers to build a nice line graph.  The problem is, the graph uses every value, which makes the graph very sharp. It gets even worse the more values I have:</p>
  
  <p><img src="https://i.stack.imgur.com/lnrXm.png" alt="enter image description here"></p>
  
  <p>I want to be able to make this into a smoother graph, possibly throwing out those numbers that make it jagged looking.  I want it to look like this:</p>
  
  <p><img src="https://i.stack.imgur.com/wBVoQ.png" alt="enter image description here"></p>
  
  <p>Maybe it is late, but I am not thinking of a way to do this.  Possibly looping through my values and throwing some of them out?  The graph doesn't need to be 100% accurate, it just needs to be a decent representation of the values.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is the method that I came up with using jrturton's answer:</p>
  
  <pre><code>- (NSArray *)smoothedArray:(NSArray *)items {
  
      int average = 0;
      int i = 0;
      int numberToAverageBy = 10;
  
      NSMutableArray *newItems = [[[NSMutableArray alloc] init] autorelease];
      // Loop
      for (NSNumber *elevation in items) {
  
          average += [elevation intValue];
          i++;
  
          if (i == numberToAverageBy) {
              [newItems addObject:[NSNumber numberWithInt:average/numberToAverageBy]];
              i = 0;
              average = 0;
          }//end
      }//end for
  
      return [NSArray arrayWithArray:newItems];
  
  }//end
  </code></pre>
  
  <p>It worked perfectly!</p>