Getting the sums of an array

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 :smiley:

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);
    
}
1 Like

When I see things like this:

int sum1;
int sum2;
int sum3;
int sum4;

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

int[] sum = new int[4];

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

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

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

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 is a tutorial on using arrays in Processing.

The best advice I can give you is to break your problem down into smaller steps 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 along with a more specific technical question. Good luck!

1 Like

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