Make this code better

Hello again,

I made an code for one task of the exam.
But it is not 100% correct.
Can sameone give me an hint to make this code better?

This is the task in the exam ( it is in german);

Schreiben	Sie	zwei	Funktionen:	produkt()	und	gameOver().
produkt()	bekommt	einen	Array	von	int-Zahlen	übergeben	und	gibt	das	Produkt der	Zahlen	
(Multiplikation)	zurück. Zum	Beispiel	resultiert	{1,	2,	3}		in	1	*	2	*	3,	also	6.
gameOver()	bekommt	einen	Namen	(String)	und	eine	Zahl	(int)	übergeben	und	gibt	einen	String	zurück.	
Wenn	die	Zahl	0	ist,	soll	sie	"You	lose,	NAME"	zurückgeben,	bei	1	"You	win,	NAME"	und	bei	2	"Take	a	
break,	NAME".
Testen	Sie	Ihre	Funktionen	mit	folgendem	Code	(verwenden	Sie	keine	globalen	Variablen!)
void setup() {
int[] a1 = {1, 2, 3};
int[] a2 = {5, -5, 4};
println(produkt(a1));
println(produkt (a2));
println(gameOver("Mike", 1));
println(gameOver("Doris", 0));
println(gameOver("Mike", 2));
}
Im	Test	sollte	ausgegeben	werden:
6-
100
You win, Mike
You lose, Doris
Take a break, Mike

Now this is my Code which I am not happy because if I run it it is not the correct answer what the professor expects from me:

void setup ()
{
  int []a1 = {1, 2, 3};
  int []a2 = {5, -5, 4};
  println(produkt(a1));
  println(produkt(a2));

  println(gameOver("Mike", 1));
  println(gameOver("Doris", 0));
  println(gameOver("Mike", 2));
}


int produkt (int[] n)
{ 
  int res = 20;

  for (int i =0; i < n.length; i++)
  {
    res = n[0] * n[1] * n[2]    ;
  }
  return res;
}

String gameOver (String p, int n)
{
  if (n == 0)
  {
    println("You lose, " + p);
  }
  if (n == 1)
  {
    println("You win, " + p);
  }

  if (n == 2)
  {
    println("Take a break, " + p);
  } 
  
   


  return p;
}

The problem in the produkt-Code ist the res = n[i] thing.
But I dont know how I must write it correct.
I know that the i needs to be multyplied but how?

The second problem ist the problem in the gameOver code.
If I run it the console writes the name twice.
How I can solve this problem?
Do I need an for loop and need to make an array?

the function produkt

instead of int res = 20; say int res = 1;

try res *= n[i]; // using for loop!!!!!!!!!!!!!!!!!!!!!!!!!

(*= is the same as res = res * ...)


the function gameOver

in game over: you use println() in setup, so you don’t want to use println() in the function gameOver.

Instead say String res=""; in the function gameOver and in the if-clauses res= "Take a break, " + p; etc. for all ifs; return res at the end of the function, not p.

Same principle for res like in your other functions: you give it a value in the function and then return it.
res stands for result.

Chrisir

Hey Chris,

thank you again.
Now I have found the solution.

It is perfect as you said.
This task in the exam was one of the hardest ones.
But I can more and more understand the codes.

Now I have another task:

Schreiben Sie zwei Funktionen betragSumme() und randomN().

betragSumme() bekommt einen Array von int-Werten und gibt die Summe aller Beträge zurück, d.h. negative Werte werden positiv gerechnet.

randomN() bekommt eine ganze Tahl N übergeben und einen Array von float-Zahlen der Länge N zurückgeben, der mit Zufallszahlen zwischen 0 und 10 gefüllt ist.
Zum Beispiel: bei randomN(3) bekomme ich einen float-Array mit drei Zufallszahlen zurück.

Testen Sie Ihre Funktion mit folgendem Code. Verwenden Sie keine globalen Variabeln!

void setup() {
int[] a = {1, -5, 10, -3};
int[] b = {-5, -3, -5};
println(betragSumme(a));
println(betragSumme(b));
println();
println(randomN(2));
println();
println(randomN(3));
}
Es!sollte!ausgegeben!werden:!
19
13
(die folgenden Zahlen sind natürlich nur Beispiele, da Zufallszahlen)
[0] 0.7408035
[1] 4.857767
[0] 4.02894
[1] 0.35072148
[2] 9.653801

This is my code:

void setup ()
{
  //int[] a = {1, -5, 10, -3};
  //int[]b ={-5, -3, -5};
  //println(betragSumme(a));
  //println(betragSumme(b));
  //println();

  println(randomN(2));
  println();
  println(randomN(3));
}


//int betragSumme(int[] n)
//{ 
//  int res = 0;
//  for (int i =0; i< n.length; i++)
// {
//  if(n[i] <0)
//{
//res += -n[i];
//else
//{
//  res += n[i]

//}
//  return res;
//}


float randomN(float[] n)
{ float[] res = new float [n.length];

for( float i = 0; i < n.length; i++)
{
 n[i] = random(0,10);
  
}

  
  
 return res; 
}

The betragSumme-code should be right.

My problem is the randomN function.
There I could not find the mistake.

Could you say me where I should fix the problem.
Then I will try it again.

Thank you :smiley:

Hier sind 2 Fehler.

Überlege nochmal: was bekommt die Funktion als Parameter, was soll sie zurückgeben?

Nicht schlecht, aber links sollte res[i]=

stehen

Das ist der Parameter

Ich habe es geschafft :smiley:

void setup ()
{
  int[] a = {1, -5, 10, -3};
  int[]b ={-5, -3, -5};
  println(betragSumme(a));
  println(betragSumme(b));
  println();

  println(randomN(2));
  println();
  println(randomN(3));
}


int betragSumme(int[] n)
{ 
  int res = 0;
  for (int i =0; i< n.length; i++)
  {
    if (n[i] <0)
    {
      res += -n[i];
    }
    else
{
  res += n[i];
}
}
  
return res;
  
}


float[] randomN(int n)
{ 
  float[] res = new float[n];

  for ( int i = 0; i < n; i++)
  {
    res[i] =  random(0, 10);
  }



  return res;
}

Kannst du es bitte kontrollieren.
Kann man das noch besser machen oder passt es so?

Danke dir für deine Hilfe Chris. Ich weiß es wirklich sehr zu schätzen.

Sieht gut aus.

Du kannst in processing ctrl-t für Auto-Formatierung drücken; bessere Einrückungen

Schau mal maximal 1 Leerzeile und nicht mehr.

Statt dem if kannst du dir auch abs() ansehen- gibt den Betrag

Super!