# Make this code better

**URL:** <https://discourse.processing.org/t/make-this-code-better/19770>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [April 14, 2020, 1:12pm UTC](https://discourse.processing.org/t/make-this-code-better/19770 "2020-04-14T13:12:04Z")\
**Posts on this page:** 9\
**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 14, 2020, 1:12pm UTC](https://discourse.processing.org/t/make-this-code-better/19770/1 "2020-04-14T13:12:04Z")

</div>

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

```auto
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:

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

---

<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 14, 2020, 1:31pm UTC](https://discourse.processing.org/t/make-this-code-better/19770/2 "2020-04-14T13:31:27Z")

</div>

**the function produkt**

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

> [@gurki](#):
>
> res = n[0] \* n[1] \* n[2] ;

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

---

<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 14, 2020, 2:02pm UTC](https://discourse.processing.org/t/make-this-code-better/19770/3 "2020-04-14T14:02:25Z")

</div>

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.

---

<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 14, 2020, 2:34pm UTC](https://discourse.processing.org/t/make-this-code-better/19770/4 "2020-04-14T14:34:14Z")

</div>

Now I have another task:

```auto
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:

```auto
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 😃

---

<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 14, 2020, 2:45pm UTC](https://discourse.processing.org/t/make-this-code-better/19770/5 "2020-04-14T14:45:52Z")

</div>

> [@gurki](#):
>
> float randomN(float n)

Hier sind 2 Fehler.

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

---

<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 14, 2020, 2:46pm UTC](https://discourse.processing.org/t/make-this-code-better/19770/6 "2020-04-14T14:46:53Z")

</div>

> [@gurki](#):
>
> n[i] = random(0,10);

Nicht schlecht, aber links sollte res[i]=

stehen

---

<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 14, 2020, 2:47pm UTC](https://discourse.processing.org/t/make-this-code-better/19770/7 "2020-04-14T14:47:43Z")

</div>

> [@gurki](#):
>
> randomN() bekommt eine ganze Tahl N übergeben

Das ist der Parameter

---

<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 14, 2020, 6:11pm UTC](https://discourse.processing.org/t/make-this-code-better/19770/8 "2020-04-14T18:11:09Z")

</div>

Ich habe es geschafft 😃

```auto
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.

---

<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 14, 2020, 7:21pm UTC](https://discourse.processing.org/t/make-this-code-better/19770/9 "2020-04-14T19:21:28Z")

</div>

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!
