Help "Function calculation sum"

I am trying to make a program that sums the individual elements of an array of 10 numbers. In order to calculate the sum I must create a function "Sum function" that returns the value corresponding to the sum of the elements of an array.Then print the entire array on the screen, and the sum. But I don't understand how to use the return function in this specific exercise. Can someone help me to understand and create the function sum? Thank you a lot.

void setup()
{
  size(400, 400);
  background(#0047b3);
  noLoop();
}

void draw()
{
  int[] numbers={14, 7, 90, 5, 8, 42, 128, 64, 12, 3};
  for (int i=0; i<numebers.length; i++)
  {
    print(numeri[i]+", ");
  }
  println();
  println("Sum = "+sum(numbers));
}

Answers

  • int sum( int... arr1) {

    for.....

    iirc

  • edited March 2017 Answer ✓

    I must create a function

    then create a function. currently everything is within draw() which isn't what you want.

    move the calculation to another method. see reference for return for an example.

    https://processing.org/reference/return.html

    (in fact, look at the second example there, it's passing in an array)

  • Hi Chris, thank you for your help but I am sorry I don't understand. Do you mean something like

    int sum(int...arr1) { for (int i =0; i<f.length; i++ ) { ? }

    and then?

    ps. Thank you Koogs, I read the reference but it didn't help me. Maybe I am confused.

  • Answer ✓

    Do you know what a function is?

    setup and draw are functions.

    Like a function in math.

    The receive some value and return a value.

    In your case array goes in and int comes out.

    Anyway.

    int sum above is the name of the function sum and what it returns

    int sum (int[] array1) {

    int returnValue;

    for loop like yours above but with array1.length ) {

    returnValue=......

    }

    } // function

  • Thank you a lot. I finished my exercise:

    void setup()
    {
      size(400, 400);
      background(#0047b3);
      noLoop();
    }
    
    void draw()
    {
      int[] numbers={14, 7, 90, 5, 8, 42, 128, 64, 12, 3};
      for (int i=0; i<numbers.length; i++)
      {
        print(numbers[i]+", ");
      }
      println();
      println("Sum = "+sum(numbers));
      println();
      int[] variable={2,4,6,8};
      for (int i=0; i<variable.length; i++)
      {
        print(variable[i]+", ");
      }
      println();
      println("Sum = "+sum(variable)); 
    }
    
    int sum(int[] array)                                          
    {                                                               
    
      int sum=0;                                                    
      for (int i=0; i<array.length; i++)                           
      {
        sum += array[i];
      }
      return sum;                                                   
    }
    
  • edited March 2017 Answer ✓
    void setup() {
      println(sum(14, 7, 90, 5, 8, 42, 128, 64, 12, 3)); // 373
      exit();
    }
    
    @SafeVarargs static final int sum(final int... vals) {
      if (vals == null)  return 0;
      int sum = 0;
      for (final int val : vals)  sum += val;
      return sum;
    }
    
  • Answer ✓

    well done, MirielLind

    ;-)

  • Thank you a lot Chris. Thanks a lot for all the help from the forum. ;)

Sign In or Register to comment.