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");

//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");
     sum1 += nums[i];
     len1 = nums.length;

   }else if(m.equals("02")) {
     sum2 += nums[i];
     len2 = nums.length;
    }else if(m.equals("03")) {
      sum3 += nums[i];
      len3 = nums.length;
    }else if(m.equals("04")) {
      sum4 += nums[i];
      len4 = nums.length;

    }//for loop ends here
    int average1 =sum1/len1;
    int average2 =sum2/len2;
    int average3 =sum3/len3;
    int average4 =sum4/len4;
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;

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++){

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: