Text box of points / Textkasten, bestehend aus Punkten

The next task is this one (also in german):

Schreiben Sie eine Funktion kreuz() mit vier den Parametern: hoehe, breite, zeile, spalte.
Ihre Funktion sollte einen (Text-)Kasten der entsprechenden Höhe und Breite zeichnen, bestehend aus
Punkten. Die angegebene Zeile sollte mit #-Zeichen ausgefüllt sein, die angegebene Spalte ebenfalls.
Testen Sie Ihre Funktion mit folgendem Code:
(verwenden Sie keine globalen Variablen!)
void setup() {
kreuz(5, 5, 2, 2);
println();
kreuz(6, 6, 0, 0);
println();
kreuz(4, 4, 3, 1);
}
Sie sollten folgenden Output sehen:


If I print the code it should come this:

…#…
…#…

…#…
…#…

#…
#…
#…
#…
#…

.#…
.#…
.#…


Now I took the code from the other task and tried it a little bit but I dont know if I need one more loop or more If clauses.
Could you say me what I should try then I will write my next code.

I tried it with this code but it is wrong

void kreuz(int h, int b, int z, int s)
{
  for (int zeile = 0; zeile < b; zeile++)
  {
    for (int spalte = 0; spalte < h; spalte++)
    {
      if (spalte < z)

      {
        print("#");
      } else
      {
        print(".");
      }
    }

    println();
  }
}

What were you expecting to happen, and what specifically happens instead?

Hey Jeremy,

unfortunately it show me the wrong pattern of code in the console.

normally it should come this as an result:

…#…
…#…
#####
…#…
…#…

#…
#…
#…
#…
#…

.#…
.#…
.#…

I dont know how exactly i must switch the if causes. Do I need more else if variatens?

Are you saying that this is what you should get, or what you do get?

If we look at the specific difference between should and do, there will be a clue there for what to change.

1 Like

Please do this first. How is the code for this?

No

It is not

Chrisir

Hey Guys this is the output what my professor wants.

Read my advice above and show your attempt

1 Like
void setup() {
  kreuz(5, 5, 2, 2);
  println();
  kreuz(6, 6, 0, 0);
  println();
  kreuz(4, 4, 3, 1);
}


void kreuz(int h, int b, int z, int s)
{
  for (int zeile = 0; zeile < b; zeile++)
  {
    for (int spalte = 0; spalte < h; spalte++)
    {
      {

        if (spalte < z +1 && spalte > z - 1 )

        {
          print("#");
        } else if (zeile < s +1 && spalte > z-1)
        {
          print("#");
        }
        else
        {
          print(".");
        }
      }
    }
  
    println();
  }
}

Now this is the code which i tried.
I expanded it with an else if cause. Unfortunately I couldn solve this problem.

Do I need more else if causes or are these 2 enough?

This seems to be more guess work here

Why not do only . for one?

When you filled the rectangle, continue with this.

This is an if clause with 2 conditions connected with ||

see reference

But your approach is good

Just use ==

void setup() {
  kreuz(5, 5, 2, 2);
  println();
  kreuz(6, 6, 0, 0);
  println();
  kreuz(4, 4, 3, 1);
}


void kreuz(int h, int b, int z, int s)
{
  for (int zeile = 0; zeile < b; zeile++)
  {
    for (int spalte = 0; spalte < h; spalte++)
    {
      {

        if (spalte < z +1 && spalte > z - 1 || zeile == s +1 && spalte == z || zeile  == z)

        {
          print("#");
        } 
        else
        {
          print(".");
        }
      }
    }
  
    println();
  }
}

Now I understand mostly everything.
The last pattern in the code is wrong.
Could you give me a hint.
I think I will finish it then.

Just follow this sentence.

Your code is good except for the if clause

if ( zeile == z || spalte == s)

Weißt du warum?

Chrisir

zeile  == z || spalte == z - 3 

Ja die letzte bedingung ist falsch aber ich komme nicht darauf.

#…#
#…#
#…#

Das kommt raus bei der Konsole

??

Das ist doch die Lösung!!!

1 Like

Vielleicht hast du b und h verwechselt in der for Schleife?

void setup() {
  kreuz(5, 5, 2, 2);
  println();
  kreuz(6, 6, 0, 0);
  println();
  kreuz(4, 4, 3, 1);
}


void kreuz(int h, int b, int z, int s)
{
  for (int zeile = 0; zeile < b; zeile++)
  {
    for (int spalte = 0; spalte < h; spalte++)
    {
      {

        if (spalte < z +1 && spalte > z  || zeile == s +2 && spalte == z || zeile  == z || spalte == s )

        {
          print("#");
        } 
        else
        {
          print(".");
        }
      }
    }
  
    println();
  }
}

Now I have the solution.
This task was one of the hardest tasks in the exams.

Thank you all.
I think I will start with the next task in the next hour.

Bitte teste es mal eben mit einer größeren Breite beim Aufruf in setup.

Hey Chris,

nee das passt so.
Der Code passt.