Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

finding the sum of numbers in an array.

$
0
0

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.
    }

Viewing all articles
Browse latest Browse all 2896

Trending Articles