# Multplying each element in an array

**URL:** <https://discourse.processing.org/t/multplying-each-element-in-an-array/24827>\
**Category:** Coding Questions\
**Created:** [October 22, 2020, 6:22pm UTC](https://discourse.processing.org/t/multplying-each-element-in-an-array/24827 "2020-10-22T18:22:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mischief-Managed](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mischief-Managed](https://discourse.processing.org/u/Mischief-Managed)\
**Post date:** [October 22, 2020, 6:22pm UTC](https://discourse.processing.org/t/multplying-each-element-in-an-array/24827/1 "2020-10-22T18:22:14Z")

</div>

Hi, I wanted to make an array where each element is simply being multiplied by 2. Here is what I have so far. Help would be greatly appreciated

```auto
int expo(int[] array[]){
  int mult = 2;
  for (int i=0;i<array.length;i++){ //goes through each element of the array and multiplies by 2 
     int [] a1 = mult *array[i];
    
  }
  return mult;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2020, 7:39pm UTC](https://discourse.processing.org/t/multplying-each-element-in-an-array/24827/2 "2020-10-22T19:39:38Z")

</div>

Hello and welcome to the processing forum!

Great to have here with us!

can you show us a code that printlns an array?

That would be a good starting point

---

<div class="post-metadata">

**Author:** ![Mischief-Managed](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mischief-Managed](https://discourse.processing.org/u/Mischief-Managed)\
**Post date:** [October 22, 2020, 7:49pm UTC](https://discourse.processing.org/t/multplying-each-element-in-an-array/24827/3 "2020-10-22T19:49:31Z")

</div>

do you want one from this example? I have more code that is on a separate tab.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2020, 8:06pm UTC](https://discourse.processing.org/t/multplying-each-element-in-an-array/24827/4 "2020-10-22T20:06:34Z")

</div>

Yeah, that would be awesome

---

<div class="post-metadata">

**Author:** ![Mischief-Managed](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mischief-Managed](https://discourse.processing.org/u/Mischief-Managed)\
**Post date:** [October 22, 2020, 8:16pm UTC](https://discourse.processing.org/t/multplying-each-element-in-an-array/24827/5 "2020-10-22T20:16:53Z")

</div>

```auto
void test_expo(int[] a, int[] expected){
  int[] test = expo(a);
  if(compare_Arrays(test,expected)){
     println("Exp given (" + Arrays.toString(a) + ") outputs " + Arrays.toString(test) + "when expecting" + Arrays.toString(expected) + "PASS");
  } else{
      println("Exp given (" + Arrays.toString(a) + ") outputs " + Arrays.toString(test) + "when expecting" + Arrays.toString(expected) + "FAIL");
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2020, 8:29pm UTC](https://discourse.processing.org/t/multplying-each-element-in-an-array/24827/6 "2020-10-22T20:29:31Z")

</div>

I meant this:

println an array using a for-loop and println :

```auto
int[] array = { 
  7, 12, 14, 18, 22
};

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

```

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2020, 8:30pm UTC](https://discourse.processing.org/t/multplying-each-element-in-an-array/24827/7 "2020-10-22T20:30:13Z")

</div>

for more information read [https://www.processing.org/tutorials/arrays/](https://www.processing.org/tutorials/arrays/)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 23, 2020, 1:13am UTC](https://discourse.processing.org/t/multplying-each-element-in-an-array/24827/8 "2020-10-23T01:13:44Z")

</div>

> [@Mischief-Managed](#):
>
> Hi, I wanted to make an array where each element is simply being multiplied by 2.

> [@Mischief-Managed](#):
>
> `int mult = 2;`

> [@Mischief-Managed](#):
>
> `return mult;`

Local variable _mult_ isn’t declared as an array but as a single `int`.

In order to multiply each element in parameter _array[]_ by 2, _mults[]_ needs to be as big as _array[]_.

We have to read _array[]_'s _length_ field in order to know its size:  
`final int len = array.length;`

And use it to declare _mults[]_:  
`final int[] mults = new int[len];`

Alternatively we can invoke method **clone()** in order to get a shallow copy out of _array[]_:  
`final int[] mults = array.clone();`

> [@Mischief-Managed](#):
>
> `int expo(int[] array[]) {`

Last detail, you’re gonna need to `return` an `int[]` instead of just `int`:  
`int[] expo(int[] array[]) {`

The example sketch below demos both approaches:

```auto
// https://Discourse.Processing.org/t/multplying-each-element-in-an-array/24827/8
// GoToLoop (2020/Oct/22)

static final int[] VALS = { 15, -3, 100, 0, };

void setup() {
  println(str(VALS)); // 15 -3 100 0
  println(str(expoNewArr(VALS))); // 30 -6 200 0
  println(str(expoClonedArr(VALS))); // 30 -6 200 0
  exit();
}

static final int[] expoNewArr(final int... vals) {
  if (vals == null) return new int[0];

  final int len = vals.length;
  final int[] mults = new int[len];

  for (int i = 0; i < len; mults[i] = vals[i++] << 1);

  return mults;
}

static final int[] expoClonedArr(final int... vals) {
  if (vals == null) return new int[0];

  final int[] mults = vals.clone();

  for (int len = mults.length, i = 0; i < len; mults[i++] <<= 1);

  return mults;
}

```
