# Println the smallest number in the arrays

**URL:** https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694
**Category:** Beginners
**Tags:** homework
**Created:** [April 12, 2020, 12:42pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694 "2020-04-12T12:42:22Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)
#### Post date: [April 12, 2020, 12:42pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/1 "2020-04-12T12:42:22Z")

</div>

Hello Everyone, I have a problem with this task.

## The Questin is in german:

Schreiben Sie eine FunkQon minAlle(), die zwei Arrays bekommt und die kleinste Zahl aus beiden Arrays  
zurückgibt. Testen Sie Ihre FunkQon mit  
void setup() {  
int[] a = { 10, -5, 8, 33 };  
int[] b = { -3, 1, 0, 55, -16 };  
println(minAlle(a, b));  
}  
Es sollte erscheinen:  
-16  
Ihr Programm muss auch funkAonieren, wenn die zwei Arrays andere Werte enthalten und/oder eine   
andere Anzahl von Werten enthalten. Sie dürfen nicht die FunkAon min() von Processing verwenden.  
Tipp: Wie findet man das Minimum von einem Array? Merken Sie sich das erste Minimum und setzen Sie  
eine zweite Schleife dazu…

* * *

I am not allowed to use min();

Here is my try but it is false:

```auto
void setup ()
{

  int[]a = {10, -5, 8, 33};
  int[] b = {-3, 1, 0, 55, -16};
  println(minAlle(a, b));
}

int[] minAlle(int[] a, int[] b)
{  
  int res[] = new int[a.length&b.length];

  for ( int i = 0; i < a.length; i++)
  {
    res[i] = a[i] ;
    if (res[i] < a[i] && res[i] < b[i])
    {
      res[i] = a[i] & b[i];
    }
    
  }

  return res;
}

```

Can someone tell me where I need to fix the false coding places?

Thank you for your help.

Gurki.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 12, 2020, 1:59pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/2 "2020-04-12T13:59:40Z")

</div>

Hello,

I would start over…

Keep it simple and do one step at a time:

- Find the smallest number _sna_ in a This is one loop.
- Find the smallest number _snb_ in b This is another loop; if it worked for a it will work for b.
- Compare _sna_ and _snb_ and return the smallest number
- Use println() statements throughout your code for troubleshooting and testing.

`:)`

> **Resources (Click to expand)**
>
> Some resources here to start you on your journey:
> 
> - Processing website has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - Those available with the Processing PDE (IDE).  
> An exploration of the tabs will reveal what is available; there are libraries and examples there.
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)

---

<div class="post-metadata">

### Author: ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)
#### Post date: [April 12, 2020, 2:13pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/3 "2020-04-12T14:13:08Z")

</div>

Ok now I unterstand where the problem is but I dont know how I can fix it.

The problem is in the for loop.

If I make it like this than the console juts make the array holders [0] = 0 and so on.

```auto
int[] minAlle(int[] a, int[] b)
{
  int res[] = new int[a.length&b.length];

  for ( int i = 0; i < a.length; i++)
  {

    if (res[i] > a[i] && res[i] < 0)
    {
      res[i] = a[i] ;
    }
  }

  return res;
}

```

Could you tell me then how I can make the parameters in the IF case to make this right?  
After that I can start then to make the b-array section and the code will be finished.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 12, 2020, 2:37pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/4 "2020-04-12T14:37:51Z")

</div>

Hello,

Read the question again…

This may not be doing what you are expecting:

```auto
int[] a = {10, -5, 8, 33, 10, 100, 1, 1};
int[] b = {-3, 1, 0, 55, -16, 1, 7};

println(a.length & b.length);

```

Reference:  
[https://processing.org/reference/bitwiseAND.html](https://processing.org/reference/bitwiseAND.html)

It is not necessary to return an array; the smallest number is just an integer.

This is an achievable exercise and I will leave the rest of this with you.

`:)`

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [April 12, 2020, 2:50pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/5 "2020-04-12T14:50:11Z")

</div>

You might find it easier if you wrote down the algorithm to solve it and then convert it to code.

```auto
Create a variable to store the smallest value found in the arrays e.g. `sna`
Initialise this variable to a very large number (e.g. 2000000000)
for each element in the first array
  if the array element value is less than 'sna' then
    store the value in 'sna '
end for loop
for each element in the second array
  if the array element value is less than 'sna' then
    store the value in 'sna '
end for loop
return 'sna' from the function

```

---

<div class="post-metadata">

### Author: ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)
#### Post date: [April 12, 2020, 6:52pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/6 "2020-04-12T18:52:48Z")

</div>

Hey guys thank you for your help but I dont unterstand anything from your answers.

I am a beginner and not so good in processing.

Could you tell me where the wrong blocks in this code are?  
Is the whole int[] function wrong or is just the for loop wrong?  
Where is the mistake?

---

<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: [April 12, 2020, 7:32pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/7 "2020-04-12T19:32:39Z")

</div>

> [@gurki](#):
>
> Where is the mistake?

first check out the difference between & and +

- 
  - means add like in 3+4 = 7

- & means logical AND (or &&) like in if the movie is good AND it rains we go to the cinema

you want + where you write &

see reference [Reference / Processing.org](https://www.processing.org/reference/)

- see [+ (addition) / Reference / Processing.org](https://www.processing.org/reference/addition.html)
- see [& (bitwise AND) / Reference / Processing.org](https://www.processing.org/reference/bitwiseAND.html)

make yourself familiar with the **entire reference**. And with the entire website.

* * *

> Schreiben Sie eine Funktion minAlle(), die zwei Arrays bekommt und die kleinste **Zahl** aus beiden Arrays zurückgibt.

You wrote `int[] minAlle(int[] a, int[] b)` which gives back an array (int) - you need ` **int** minAlle(int[] a, int[] b)` ohne

> [@gurki](#):
>
> int res = new int[a.length&b.length];

can be seen as a Folgefehler; again res shouldn’t be an array but an int: int res = 2000000000;

* * *

**Two for loops**

in the function: you need 2 for loops, independent checking both arrays a and b (not only one for-loop)

This

```auto
if (res[i] > a[i] && res[i] < 0)
{
  res[i] = a[i] ;
}

```

is wrong; better (since res is not an array but a number)

```auto
if (a[i] < res)
{
  res = a[i] ;
}

```

you must use a similar if-clause in the second for loop as well (for b)

Chrisir

---

<div class="post-metadata">

### Author: ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)
#### Post date: [April 12, 2020, 8:38pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/8 "2020-04-12T20:38:06Z")

</div>

Hello Chris,

now I made it like this:

```auto
void setup ()
{

int[]a = {10, -5, 8, 33};
int[] b = {-3, 1, 0, 55, -16};
println(minAlle(a, b));
}

int minAlle(int[] a, int[] b)
{
int res = 20;

for ( int i = 0; i < a.length; i++)
{

if (res > a[i] && res < 0)
{
  res = a[i] ;
}

for ( int j = 0; j < b.length; j++)
{

if (res > b[j] && res < 0)
{
  res = b[j] ;
}

}
}

return res;
}

```

I think that the parameters in the If clauses are just wrong.  
Which of the two if clauses are now wrong?

Thank you for your help

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 12, 2020, 9:12pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/9 "2020-04-12T21:12:53Z")

</div>

Hello,

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)  
It helps you and us.

I formatted code below so you could see opening bracket { and closing bracket } and nested loops and if statements.

```auto
void setup ()
  {
  int[]a = {10, -5, 8, 33};
  int[] b = {-3, 1, 0, 55, -16};
  println(minAlle(a, b));
  }

int minAlle(int[] a, int[] b)
  {
  int res = 20;

  for ( int i = 0; i < a.length; i++)
    {
    if (res > a[i] && res < 0)
      {
      res = a[i] ;
      }

     for ( int j = 0; j < b.length; j++)
       {  
       if (res > b[j] && res < 0)
         {
         res = b[j] ;
         }
      }
    }
  return res;
  }

```

Get one loop working then add another loop then think about refining it later.  
Use println() statements in code to help with understanding the code.

I encourage you to try this without us; you can do it!

`:)`

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 12, 2020, 9:25pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/10 "2020-04-12T21:25:10Z")

</div>

Hello,

There is an error in your code; obvious to me but it would be best if you can work through it.

I modified your code to one array to test code using println() statements:

```auto
void setup ()
  {
  int[] a = {10, -5, 8, 33};
  int[] b = {-3, 1, 0, 55, -16};
  println(minAlle(a));
  }

int minAlle(int[] a)
  {
  int res = 20;
  println(a); //ok
  for (int i = 0; i < a.length; i++)
    {
    println(i); //ok
    if (res > a[i] && res < 0)
      {
      res = a[i] ;
      println(res); // Not what I am expecting! Maybe the if statement conditions/tests?
      }

    //for ( int j = 0; j < b.length; j++)
    // {  
    // if (res > b[j] && res < 0)
    // {
    // res = b[j] ;
    // }
    // }
    }
  return res;
  }

```

`:)`

---

<div class="post-metadata">

### Author: ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)
#### Post date: [April 12, 2020, 9:36pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/11 "2020-04-12T21:36:22Z")

</div>

Hi glv,

Now I get more and more confused. It is just an trial and error thing for me

I need to know where the problems are. Where to start. Where it is needed to fix the f…ing code  
Does the parameters in the if cause are right?  
I am nearly to cry cause I dont unterstand this language.  
the more I write in the forum the more I hate it.

It makes me really upset in the middle of the night.

Nonetheless thank you so much for your help but it makes me really angry that noone is allowed to say me just the answer.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 12, 2020, 10:20pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/12 "2020-04-12T22:20:08Z")

</div>

Hello,

I suggest you get some sleep and come back to this when you are fresh.

> [@gurki](#):
>
> It is just an trial and error thing for me

You can learn from trial and error but must also work at understanding it.  
Keep working at it.

I updated my last example; there was a hint in there that states:  
_“Maybe the if statement conditions/tests?”_

---

<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: [April 12, 2020, 10:24pm UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/13 "2020-04-12T22:24:58Z")

</div>

The answer would not help you when you don’t understand the principles

Wenn du eine Klausur schreiben musst zB.

Gib nicht dem Forum die Schuld, wenn du ungenau liest:

Wieso hast du nicht die Zeilen übernommen, die ich für dich schrieb??

```auto
if (a[i] < res)
{
  res = a[i] ;
}

```

UND

` int res = 2000000000;`

---

<div class="post-metadata">

### Author: ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)
#### Post date: [April 13, 2020, 9:46am UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/14 "2020-04-13T09:46:58Z")

</div>

Sorry guys yesterday I was just stressed out. I try my best today. I will send you in the next hours my new try.

---

<div class="post-metadata">

### Author: ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)
#### Post date: [April 13, 2020, 9:56am UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/15 "2020-04-13T09:56:43Z")

</div>

```auto
void setup ()
{

  int[]a = {10, -5, 8, 33};
  int[] b = {-3, 1, 0, 55, -16};
  println(minAlle(a, b));
}

int minAlle(int[] a, int[] b)
{
  int res = 200;

  for ( int i = 0; i < a.length; i++)
  {

    if (a[i] < res)
    {
      res = a[i] ;
    }

    for ( int j = 0; j < b.length; j++)
    {

      if (b[j] < res)
      {
        res = b[j] ;
      }
    }
  }

  return res;
}

```

Hey Chris,

thank you again for your help.  
Now I have unterstand the problem and I made also the 2nd for loop.

This is the result but why I dont need to make the int array res?  
That is the only thing why I dont understand clearly.  
Can you explain it please?

Thank you all again.  
Sorry for my bad mood yesterday. It is really frustating for someone who is not studying IT but needs to finish an exam in IT.

---

<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: [April 13, 2020, 10:32am UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/16 "2020-04-13T10:32:29Z")

</div>

Hello,

as you can see, your code can be improved since the second for loop is INSIDE the first for loop

(when you place the cursor behind a bracket, the corresponding bracket is highlighted. Thus you can see where the first for-loop ends: BEHIND the second for loop, not before it. This doesn’t change the result but takes time because the 2nd for loop is executed `i < a.length; ` times ! Not only once.)

* * *

**Your question**

> [@gurki](#):
>
> This is the result but why I dont need to make the int array res?  
> That is the only thing why I dont understand clearly.

You have to read the assignment more carefully. It says, we search a **number** (res) not an array.

The number should be the smallest value of both arrays. Therefore we need a number res and not an array (list). We copy the number out of the array, but this number is one number and not an array.

Warm regards,

Chrisir

```auto

void setup ()
{
  int[]a = {10, -5, 8, 33, -118, 119};
  int[] b = {-3, 1, 0, -55, -16};
  println(minAlle(a, b));
}//function 

int minAlle(int[] a, int[] b)
{
  // result 
  int res = 2000000;

  for (int i = 0; i < a.length; i++)
  {
    if (a[i] < res)
    {
      res = a[i];
    }//if
  }//for I 

  // ------------------------------------------

  for (int j = 0; j < b.length; j++)
  {
    if (b[j] < res)
    {
      res = b[j];
    }//if
  }// for II

  return res;
}//func
//

```

---

<div class="post-metadata">

### Author: ![gurki](https://avatars.discourse-cdn.com/v4/letter/g/47e85d/32.png) [@gurki](https://discourse.processing.org/u/gurki)
#### Post date: [April 13, 2020, 10:39am UTC](https://discourse.processing.org/t/println-the-smallest-number-in-the-arrays/19694/17 "2020-04-13T10:39:22Z")

</div>

Perfect. Now I understand it clearly. I hope that I wont fail this upcoming exam.

This is far one of the most complex tasks.

Thank you for your support everyone
