简体   繁体   中英

How can I make additions to this code to print a full triangle with my arduino?

It obviously only makes half a triangle but I don't know how to fix that. I'm new to this and honestly don't really know how to work it, so help would be greatly appreciated. I got this far but now I'm lost:

void setup() 
{
  Serial.begin(9600);
  Serial.setTimeout(100000); // timeout now is 100 seconds

  // read a number from serial port
  String s = Serial.readStringUntil(10); // read a line from serial port
  int n = s.toInt(); // convert the input string to integer value

for (int i=0; i<n; i++){
  for (int j=0; j<i+1; j++){
    Serial.print("*" );
   }

Serial.print("\n");
}

}

void loop(){

}

replace n with rows

for(int i = 1, k = 0; i <= rows; ++i, k = 0)
{
    for(space = 1; space <= rows-i; ++space)
    {
        cout <<"  ";
    }

    while(k != 2*i-1)
    {
        cout << "* ";
        ++k;
    }
    cout << endl;
}    

here the output

https://ideone.com/OPMeO1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM