finding the sum of numbers in an array.

edited October 2015 in Library Questions

Hi everyone,

I was trying to find the sum of an array of numbers. but when i tried calculating it using the code below, I found the values being continuously increased since the forloop is running. Is there any way that I can get the sum of the numbers in the array other than adding a noLoop function or doing the addition inside void setup?

It would be a great help if someone can help me with this. Many thanks in advance

Cheers, Yousuf.

    float sum = 0;
    float [] f = {12, 2, 4, 6, 23};

    void setup() {

      size(600, 600);
      smooth();
    }

    void draw() {

      for (int i =0; i<f.length; i++) {

        sum +=f[i];
      }
      println(sum);//// I only wanted to print the first value of sum, which is the total of the numbers listed array of numbers.
    }

Answers

  • draw() is fired every frame - so your for loop is run multiple times a second and keeps adding to the value of sum constantly. For a one shot calculation you could add a condition to not repeat the for loop once it has run once; but if that's what you want it would seem reasonable to just move the loop to setup (the value assigned to sum will be available in draw).

    Any good reason you don't want to do this?

  • Thank you GoToLoop and blindfish for your replies. @Blindfish, I actually wanted to make the array dynamic where the data is updated from a csv file using a controlP5 slider. so, I wanted to get the changing sum of the numbers in the array while updating the array with new set of numbers.

  • Then your code shouldn't be in draw(). It should be called from draw() whenever the data changes

  • edited October 2015

    Your int sum is global, you can make a separate function to get sum of array items:

           float [] f = {12, 2, 4, 6, 23};
    
           void setup() { 
           size(400,400);  
           }
    
           void draw() { 
            println(arrSum(f));
           }
    
            float arrSum(int[] arr){
            float sum = 0;
            for (int i =0; i<arr.length; i++) {
                 sum +=arr[i];
              }
            return sum;
            }
    
  • edited October 2015

    @After Thank you so much. I just moved the variable int sum from global to local, and it serves my purpose now :). I tried running the code u posted, but somehow it is not working..

  • edited October 2015

    If the array is not empty (length != 0) then this works as well

     float arrSum(int[] arr){
       float sum = arr[0];
       for (int i = 1; i < arr.length; i++) {
          sum +=arr[i];
       }
       return sum;
     }
    
  • edited October 2015 Answer ✓

    Adapted from: http://forum.Processing.org/two/discussion/13000/how-to-check-for-nan

    // forum.processing.org/two/discussion/13021/finding-the-sum-of-numbers-in-an-array
    // forum.Processing.org/two/discussion/13000/how-to-check-for-nan
    
    // GoToLoop (2015-Oct-15)
    
    static final float[] FLOATS = {
      PI, TAU, sqrt(3.0), EPSILON, 1e3
    };
    
    void setup() {
      println(arrSum(FLOATS)); // 1011.1569
      exit();
    }
    
    static final float arrSum(float... arr) {
      float sum = 0.0;
      for (final float f : arr)  sum += f;
      return sum;
    }
    
Sign In or Register to comment.