#Pyramid

28 messages · Page 1 of 1 (latest)

carmine zealot
#

1
21
321
4321
54321....
I can make pyramid but couldn't Reverse print it... I only managed to do this...
1
12
123
1234
12345
P.S(I am a newbie)#1217658493977493525

idle crownBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

eternal flint
#

Not sure I understand. Did you intend to print:

5 4 3 2 1
  5 4 3 2
    5 4 3
      5 4
        5

or perhaps

1 2 3 4 5
  1 2 3 4
    1 2 3
      1 2
        1

Where are you stuck?

carmine zealot
#

I intend to print
1
21
321
4321
54321....

#

But I am stuck on this.

       1 
     12
    123 
  1234 

12345

eternal flint
#

Is there code involved that can be looked at?

vale hornet
#

it' likely you have a loop from i=1 to i=5, at some point

#

just reverse it to go from i=5 to i=1 by steps of -1

carmine zealot
eternal flint
#

Maybe think about how you might iterate over the 5 rows as an outer loop and then inside that loop figure out how many spaces to print based on which row you are on and then the decending numbers starting with the row you are on. For example the first row consists of 4 spaces and then the number 1. The second row is 3 spaces and starting with the number 2 down to 1 and so forth for all rows.

vale hornet
#

you haven't even tried the suggestion I gave.... very sad

idle crownBOT
#

#include<stdio.h>

int main() {
  for (int i = 1; i <= 5; i++) {
    for (int j = 5 - ij >= 1; j--) {
    }

    printf("  ");

    for (int k = 1; k <= i; k++) {
      printf("%d", k);
    }

    printf("\n");
  }

  return 0;
}
Kakashibrat
tacit surge
#

!code

idle crownBOT
#
How to Format Code on Discord
Markup

```c
int main() {}
```

Result
int main() {}
unique thorn
#

The solution using just one loop.

int main()
{
    const char* str = "54321";
    int len = strlen(str);

    for (int i = len -1 ; i >=0; --i)
    {
        printf("%*s\n", len, str + i);
    }
}

With a string length = 5

printf("%*s\n", len , str + i) always prints 5 characters exclusive of the newline.
If the string to be printed is less than 5 characters, then it will pad out the string at the left using spaces.

For the first loop, i will be 4.
Then str + 4 will point at "1\0". That easily fits 5 chars, so this prints "....1\n" (spaces shown as dots for clarity)

For the second loop, i will be 3.
Then str + 3 will point at "21\0". That easily fits 5 chars, so this prints "...21\n".

etc.

For the last loop, i will be 0.
Then str + 0 will point at "54321\0". That just fits 5 chars, so this prints "54321\n".

I know you cannot / should not use this, because no teacher will believe that you came up with this yourself.
Besides, we're using a string literal, and that may be considered cheating too.

candid basin
final carbonBOT
#
Program Output
1
   21
  321
 4321
54321
Compiler Output
<source>: In function 'main':
<source>:4:15: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
    4 |     int len = strlen(str);
      |               ^~~~~~
<source>:1:1: note: include '<string.h>' or provide a declaration of 'strlen'
  +++ |+#include <string.h>
    1 | int main()
<source>:4:15: warning: incompatible implicit declaration of built-in function 'strlen' [-Wbuiltin-declaration-mismatch]
    4 |     int len = strlen(str);
      |               ^~~~~~
<source>:4:15: note: include '<string.h>' or provide a declaration of 'strlen'
<source>:8:9: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
    8 |         printf("%*s\n", len, str + i);
      |         ^~~~~~
<source>:1:1: note: include '<stdio.h>' or provide a declaration of 'printf'
  +++ |+#include <stdio.h>
    1 | int main()
<source>:8:9: warning: incompatible implicit declaration of built-in function 'printf' [-Wbuiltin-declaration-mismatch]
  
candid basin
#

well the compiler tried to also compile the code from the comment i replied to

#

;compile ```c
#include <stdio.h>
int main(void) {
const char str[] = "54321";
for (int i = sizeof str - 1; --i >= 0;) {
// the lines start with "|" because
// the compiler bot removes leading
// whitespace from the first line
printf("|%*s\n", (int)sizeof str - 1, &str[i]);
}
}

final carbonBOT
#
Program Output
|    1
|   21
|  321
| 4321
|54321
candid basin
#

;asm -O3 ```c
#include <stdio.h>
int main(void) {
const char str[] = "54321";
for (int i = sizeof str - 1; --i >= 0;) {
printf("%*s\n", (int)sizeof str - 1, &str[i]);
}
}

final carbonBOT
#
Assembly Output
.LC0:
  .string "%*s\n"
main:
  push rbp
  mov eax, 49
  push rbx
  sub rsp, 24
  mov WORD PTR [rsp+14], ax
  lea rbp, [rsp+10]
  lea rbx, [rsp+14]
  mov DWORD PTR [rsp+10], 842216501
.L2:
  mov rdx, rbx
  mov esi, 5
  mov edi, OFFSET FLAT:.LC0
  xor eax, eax
  call printf
  mov rax, rbx
  sub rbx, 1
  cmp rax, rbp
  jne .L2
  add rsp, 24
  xor eax, eax
  pop rbx
  pop rbp
  ret

candid basin
#

;asm -O3 ```c
#include <stdio.h>
#include <string.h>
int main()
{
const char* str = "54321";
int len = strlen(str);

for (int i = len -1 ; i >=0; --i)
{
    printf("%*s\n", len, str + i);
}

}

final carbonBOT
#
Assembly Output
.LC0:
  .string "54321"
.LC1:
  .string "%*s\n"
main:
  push rbx
  mov ebx, OFFSET FLAT:.LC0+4
.L2:
  mov rdx, rbx
  mov esi, 5
  mov edi, OFFSET FLAT:.LC1
  xor eax, eax
  call printf
  mov rax, rbx
  sub rbx, 1
  cmp rax, OFFSET FLAT:.LC0
  jne .L2
  xor eax, eax
  pop rbx
  ret

final carbonBOT
#
Assembly Output
.LC0:
  .string "%*s\n"
main:
  push rbp
  push rbx
  sub rsp, 24
  mov DWORD PTR [rsp+10], 842216501
  lea rbp, [rsp+10]
  lea rbx, [rsp+14]
  mov WORD PTR [rsp+14], 49
.L2:
  mov rdx, rbx
  mov esi, 5
  mov edi, OFFSET FLAT:.LC0
  xor eax, eax
  call printf
  mov rax, rbx
  dec rbx
  cmp rax, rbp
  jne .L2
  add rsp, 24
  xor eax, eax
  pop rbx
  pop rbp
  ret