Println the smallest number in the arrays

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:

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.

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:

1 Like

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.

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.

Hello,

Read the question again…

This may not be doing what you are expecting:

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

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.

:)

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

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
3 Likes

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?

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

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 []

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

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

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

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

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

Chrisir

1 Like

Hello Chris,

now I made it like this:

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

Hello,

Please 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.

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!

:)

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:

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;
  }

:)

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.

Hello,

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

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?”

1 Like

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??

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

UND

int res = 2000000000;

1 Like

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

2 Likes
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.

2 Likes

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

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


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
//
1 Like

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

2 Likes