Using an integer to manipulate a string/text variable

Write a function that consumes a non-negative integer k and prints a k-size diamond. A k-size diamond consists of a top, a middle and a bottom. The middle is a single row of 2k+1 d’s. The top consists of k rows. The i throw in the top (starting from 0) contains 2i+1 d’s. The d’s on each top row are centred over the d’s on the middle row. A bottom is like a top but upside down.

Ex. k = 2;

    d
   ddd
  ddddd
   ddd
    d

please format code with </> button * homework policy * asking questions

int k;
int l;

void setup() {
  size(400, 400);
  k = 2;
  
  textAlign(CENTER, CENTER);
 
String s = "d";
fill(255,0,0);
text(s + s + s + s +s, CENTER + 200, CENTER + 200);  
}
1 Like

Hello and welcome to the forum!

Great to have you here!

Please note that we are not allowed to give full code solutions for homework.

  • Please look in the reference at the keyword “for” and use a for-loop with i as a variable (which you mentioned). Then inside the for-loop use the formula you mentioned: contains 2i+1 diamonds.

You can use text(...., width/2, i*33); for text output; before this say textAlign(CENTER);

https://www.processing.org/reference/

Warm regards,

Chrisir

:wink: