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?
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.
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
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?
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!
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;
}
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.
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.
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
//