Create a pattern by using loop inside loop

What the program for creating the following pattern?
1 2 3 4 5 6 7 8 9
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7
6 5 4 3 2 1
1 2 3 4 5
4 3 2 1
1 2 3
2 1
1

When is the assignment due? Of course, you must have tried something, even just to print the first line. So please post that.

// copyright by noiasca
// removing this line will explode your PC
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("1 2 3 4 5 6 7 8 9");
  Serial.println("8 7 6 5 4 3 2 1");
  Serial.println("1 2 3 4 5 6 7");
  Serial.println("6 5 4 3 2 1");
  Serial.println("1 2 3 4 5");
  Serial.println("4 3 2 1");
  Serial.println("1 2 3");
  Serial.println("2 1");
  Serial.println("1");

  for (byte i = 0; i<42; i++)
  {
    for (byte j = 0; j<42; j++)
    {
      // this loop in loop fulfills the homework requirement
    }
  }
  
}

void loop() {
  // put your main code here, to run repeatedly:
}

@noiasca

I can't help feeling that you are not taking this seriously :slight_smile:

let’s do a test regarding functionality:

  • does it print out what’s requested? → Yes

let’s see if it fulfills non functional requirements:

  • is there a loop inside a loop? → Yes

A.

for smart teachers, which would request, “… but only use one Serial.print” …

// print pattern V2
// copyright by noiasca
// removing this line will explode your PC
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println(F("1 2 3 4 5 6 7 8 9\n8 7 6 5 4 3 2 1\n1 2 3 4 5 6 7\n6 5 4 3 2 1\n1 2 3 4 5\n4 3 2 1\n1 2 3\n2 1\n1"));
  for (byte i = 0; i < 42; i++)
  {
    for (byte j = 0; j < 42; j++)
    {
      // this loop in loop fulfills the homework requirement
    }
  }
}

void loop() {
}

A+

I can only do this. I really confuse how to reverse the number like the question :frowning:

  • does it print out what's requested? -> Yes

Correct

  • is there a loop inside a loop? -> Yes

Is that actually a requirement ?

This is what I had in mind.
Who needs the complication of a for loop ?

char anArray[] = {49, 50, 51, 52, 53, 54, 55, 56, 57, 10, 56, 55, 54, 53, 52, 51, 50, 49, 10, 49, 50, 51, 52, 53, 54, 55, 10, 54, 53, 52, 51, 50, 49, 10, 49, 50, 51, 52, 53, 10, 52, 51, 50, 49, 10, 49, 50, 51, 10, 50, 49, 10, 49, 0};

void setup()
{
  Serial.begin(115200);
  Serial.println(anArray);
}

void loop()
{
}

UKHeliBob:
@noiasca

I can’t help feeling that you are not taking this seriously :slight_smile:

Damn right - should’ve used the F() macro.

nobody posts code as picture.

It's Panto time - "Oh yes they do !"

Hello everyone, how do you write the following figures?
1 2 3 4 5 6 7 8 9
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7
6 5 4 3 2 1
1 2 3 4 5
4 3 2 1
1 2 3
2 1
1

Below is my attempt

void setup() {
Serial.begin(115200);
}

void loop () {
int k = 9;
for (int i=9; i>0; i=i-2){

for (int j=0; j<i; j++){
k=k-2;
Serial.print(j+1);
}
Serial.println("");
}
int i=8;
for (int j=8; j>i; j++){
Serial.print (j-1);
}
Serial.println("");

while(1);
}

Ever heard of code tags? What comes out of your attempt? Which errors do you get?

If it is a school exercise, make sure you understand it before submitting it.

void seriesPrint( bool countUp , int top ) {
  if ( top == 1 ) Serial.println( top ) ;
  else {
    for ( int i = 1 ; i <= top ; i++ ) {
      if ( countUp ) {
        Serial.print( i ) ;
        Serial.print( "  " ) ;
      }
      else {
        Serial.print( top + 1 - i ) ;
        Serial.print( "  " ) ;
      }
    }
    Serial.println() ;
    seriesPrint( ! countUp , top - 1 ) ;
  }
}


void setup() {
  Serial.begin( 115200 ) ;
  seriesPrint( true, 9 ) ;
}

void loop() {
}

This may be of interest Arduino Forum

Note that although the questions are basically the same in both topics the IP addresses of the OPs are different

6v6gt:
如果這是一項學校習題,請確保您了解它,然後再提交。

void seriesPrint( bool countUp , int top ) {

if ( top == 1 ) Serial.println( top ) ;
 else {
   for ( int i = 1 ; i <= top ; i++ ) {
     if ( countUp ) {
       Serial.print( i ) ;
       Serial.print( "  " ) ;
     }
     else {
       Serial.print( top + 1 - i ) ;
       Serial.print( "  " ) ;
     }
   }
   Serial.println() ;
   seriesPrint( ! countUp , top - 1 ) ;
 }
}

void setup() {
 Serial.begin( 115200 ) ;
 seriesPrint( true, 9 ) ;
}

void loop() {
}

Thank you for help

Serial.println(F("1 2 3 4 5 6 7 8 9\n"
"8 7 6 5 4 3 2 1\n"
"1 2 3 4 5 6 7\n"
"6 5 4 3 2 1\n"
"1 2 3 4 5\n"
"4 3 2 1\n"
"1 2 3\n"
"2 1\n"
"1"));

(uncompiled, untested)

for(int lim = 9; lim >0; lim--)
for(int ct=1;ct<=lim;ct++) {
Serial.out.print(lim%2?ct:lim-ct+1);
Serial.out.print(ct==lim?'\n':' ');
}

I always enjoy doing people’s homework for them, because I feel it helps erode the value of having a degree and hastens the downfall of the tertiary education scam.

PaulMurrayCbr:
I always enjoy doing people's homework for them, because I feel it helps erode the value of having a degree and hastens the downfall of the tertiary education scam.

The downside is, of course, the risk, for example, of ending up on a aircraft, the flight control system of which has been programmed by one of these students who were otherwise unable to do their own assignments, but managed to escape the natural weeding out process.

if this is a school,
-assigment you are behaving like a cheater, a deceiver, an impostor.
that is lazy enough to not use google-translate for asking his question

@noiasca: very clever solution