# Getting the sums of an array

**URL:** <https://discourse.processing.org/t/getting-the-sums-of-an-array/402>\
**Category:** Coding Questions\
**Created:** [May 26, 2018, 1:39pm UTC](https://discourse.processing.org/t/getting-the-sums-of-an-array/402 "2018-05-26T13:39:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dereklop](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@dereklop](https://discourse.processing.org/u/dereklop)\
**Post date:** [May 26, 2018, 1:39pm UTC](https://discourse.processing.org/t/getting-the-sums-of-an-array/402/1 "2018-05-26T13:39:03Z")

</div>

This is my code i have cvs file with values and dates in it. I want to be able to get the average of each day and month. So far i am able to get the value for each month but it would require a lot of input to get all the value for each day of the month. Is there a better way to do what i am doing??

i dont want all this global variables also im not sure that with the if statments im achiving my goal properly of getting the month my value is.

Help will be apriciated 😃

```
float w=0;
float h=0;
int len = 2880;
int[] nums = new int[len];
int sum;
int sum1;
int sum2;
int sum3;
int sum4;
int len1;
int len2;
int len3;
int len4;

void setup(){
Table table = loadTable("indice_2018.CSV","header");
size(8880,400);
background(0);
noLoop();

//Table row = new Table();

for(int i = 0; i<len; i++){
    nums[i] = table.getInt(i,"Noroeste PM10");
    
    sum += nums[i];
  
    w = w + .5;
    h= h+ 14.4;
    String date = table.getString(i,"Fecha");
    String d = date.substring(0,2);
    String m = date.substring(3,5);
    String a = date.substring(6,10);
     
     int y = table.getInt(i,"Noroeste PM10");
     int x = table.getInt(i,"Hora");
   
    noStroke();
    ellipse(w,y+200,3,3);
   
   if(m.equals("01")){
     sum1 += nums[i];
     len1 = nums.length;

    fill(#FAFF00);
   }else if(m.equals("02")) {
     sum2 += nums[i];
     len2 = nums.length;
     
    fill(#00FF63);
    }else if(m.equals("03")) {
      sum3 += nums[i];
      len3 = nums.length;
      
      fill(#008AFF);
    }else if(m.equals("04")) {
      sum4 += nums[i];
      len4 = nums.length;
       fill(#FF002F);
    }else{
  }

    }//for loop ends here
    
    
    int average1 =sum1/len1;
    int average2 =sum2/len2;
    int average3 =sum3/len3;
    int average4 =sum4/len4;
    fill(255);
    ellipse(100,average1,10,10);
    ellipse(200,average2,10,10);
    ellipse(300,average3,10,10);
    ellipse(400,average4,10,10);
    
    
    println(average1);
    println(average2);
    println(average3);
    println(average4);
    
}
```

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [May 26, 2018, 5:39pm UTC](https://discourse.processing.org/t/getting-the-sums-of-an-array/402/2 "2018-05-26T17:39:54Z")

</div>

When I see things like this:

```auto
int sum1;
int sum2;
int sum3;
int sum4;

```

This is usually a sign that you should be using an array instead:

```auto
int[] sum = new int[4];

```

Then you can access the indexes of the array like this:

```auto
sum[0] = 42;
println(sum[1]);

```

Or better yet, you can use a `for` loop to iterate over the indexes in the array:

```auto
for(int i = 0; i < sum.length; i++){
  println(sum[i]);
}

```

Note that you’re already doing this with your `nums` array. You just need to do the same thing with your other values.

Shameless self-promotion: [here](http://happycoding.io/tutorials/processing/arrays) is a tutorial on using arrays in Processing.

The best advice I can give you is to [break your problem down into smaller steps](http://happycoding.io/tutorials/how-to/program) and take those problems on one at a time. Don’t try to write your whole program at one time. Get something simpler working first. Can you create a simple sketch that uses a hard-coded array just to test things out? Then read from a simple CSV file that only contains one column of numbers. Get that working before moving on to the next step. Then if you get stuck, you can post a [MCVE](https://stackoverflow.com/help/mcve) along with a more specific technical question. Good luck!

---

<div class="post-metadata">

**Author:** ![dereklop](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@dereklop](https://discourse.processing.org/u/dereklop)\
**Post date:** [May 26, 2018, 5:47pm UTC](https://discourse.processing.org/t/getting-the-sums-of-an-array/402/3 "2018-05-26T17:47:53Z")

</div>

Thank you very much Kevin! Thats very cool ill use the array in the ints and test it out 😃
