How to print this pyramid pattern?

  • Thread starter shivajikobardan
  • Start date
  • Tags
    Pyramid
In summary: Not schoolwork. I don't understand Perl. I'd love hints than code on how to convert that one logic to programming logic.But it is still a schoolwork type of problem, so I think that they will want you to follow the schoolwork process.
  • #1
shivajikobardan
674
54
How to print this pyramid pattern?
sHIT0kVeuAAFA1qJHuYl-30JoU_ByI0fU5IACwVp0dzG0X_C0Q.png

mo221wCC6JQWl46wgW65fhTHM5qvjBrIv-bHsp6NuhPpueN2IQ.png

I've built a matrix like this in order to generate logic. The "0" means spaces.

At i=1, j=5 should be printed as star, rest as white spaces
At i=2, j=4,6…
At i=3, j=3,5,7….
At i=4,j=2,4,6,8…
At i=5, j=1,3,5,7,9…

How do I convert this into logic?
 

Attachments

  • sHIT0kVeuAAFA1qJHuYl-30JoU_ByI0fU5IACwVp0dzG0X_C0Q.png
    sHIT0kVeuAAFA1qJHuYl-30JoU_ByI0fU5IACwVp0dzG0X_C0Q.png
    357 bytes · Views: 90
Last edited:
Technology news on Phys.org
  • #2
Suppose you are going to print N lines in all. For line n, you can start the line with N-n spaces and then append the two-character string "* " n times.
This Perl code:

This Perl code:
$N=10;
foreach $n (1..$N){
    $line = ' 'x($N-$n);
    $line = $line . "* "x$n;
    print "$line\n";
}
Produces this result:
         *
        * *
       * * *
      * * * *
     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
* * * * * * * * * *
PS. I forgot to add any trailing space. If that was desired it's easy to do.
 
  • #3
FactChecker said:
Suppose you are going to print N lines in all. For line n, you can start the line with N-n spaces and then append the two-character string "* " n times.
This Perl code:

This Perl code:
$N=10;
foreach $n (1..$N){
    $line = ' 'x($N-$n);
    $line = $line . "* "x$n;
    print "$line\n";
}
Produces this result:
         *
        * *
       * * *
      * * * *
     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
* * * * * * * * * *
PS. I forgot to add any trailing space. If that was desired it's easy to do.
I only understand javascript,c,c++,java, python . I'm using nested for loops as well.
 
  • #4
can anyone help to do with the logic I laid out here? It'd be great.
 
  • #5
Sorry. If this is a schoolwork-type of problem, then we are only allowed to give hints and guidance about your work. (I really shouldn't have given a code solution. It has a slight error anyway.) Can you show how you would start your code and nested loops? You can pick a language or use pseudocode.
 
  • #6
FactChecker said:
Sorry. If this is a schoolwork-type of problem, then we are only allowed to give hints and guidance about your work. (I really shouldn't have given a code solution. It has a slight error anyway.) Can you show how you would start your code and nested loops? You can pick a language or use pseudocode.
Not schoolwork. I don't understand Perl. I'd love hints than code on how to convert that one logic to programming logic.
 
  • #7
But it is still a schoolwork type of problem, so I think that they will want you to follow the schoolwork process.
Hint #1: Your logic clearly is to go through it one line at a time, so the main loop should be the line number.
Hint #2: Even for line 1, you need to know how many total lines you are going to make so you know what value j takes.
 
  • #8
shivajikobardan said:
I don't understand Perl.
The Perl that @FactChecker posted is not that different from what the equivalent Python would be.
 
  • #9
JavaScript:
n = 5;

for (i = 1; i <= n; i++) {
  for (j = 1; j <= 2 * n - 1; j++) {
    
    document.write("*");
  }
  document.write("<br>");
}
 
  • #10
shivajikobardan said:
JavaScript:
n = 5;

for (i = 1; i <= n; i++) {
  for (j = 1; j <= 2 * n - 1; j++) {
  
    document.write("*");
  }
  document.write("<br>");
}
You have to actually print the empty spaces. Here are a couple of choices:
1) Start by filling the entire thing with blanks and then use the loops to only place '*'s where they are needed.
2) Within the main loop, place a full set of one line of spaces and then use an inner loop to place the '*'s where they are needed.
3) For each line in the main loop, proceed from left to right and place a space ' ' or a '*' as appropriate at that position.
4) Try to mimic the logic of my Perl program in Javascript.
 
  • #11
i know asking code would make this homework problem, but if you could share pseudocode, flowchart or whatever it doesn't make it look like solving other's homework, it'd be helpful.I printed this by watching some tutorial. not exactly this but similar.

JavaScript:
n = 5;
for (i = 1; i <= 5; i++) {
  for (space = 1; space <= 5 - i; space++) {
    document.write("y");
  }
  for (j = 1; j <= 2 * i - 1; j++) {
    document.write("x");
  }
  document.write("<br>");
}
 
  • #12
I think that it would help you in general to put comments in your code indicating what the code should do.
JavaScript:
// Set number of lines of the artwork
n=5;
// Loop through the lines from i=1 to i=n
for (i=1;i <=n; i++) {
    // start the line with n-i spaces
    for( j=1; j <=n-i; j++) {
        document.write(" ");
    }
    // print "* " i times
    // etc.
}
 
Last edited:
  • #13
shivajikobardan said:
i know asking code would make this homework problem, but if you could share pseudocode, flowchart or whatever it doesn't make it look like solving other's homework, it'd be helpful.I printed this by watching some tutorial. not exactly this but similar.

JavaScript:
n = 5;
for (i = 1; i <= 5; i++) {
  for (space = 1; space <= 5 - i; space++) {
    document.write("y");
  }
  for (j = 1; j <= 2 * i - 1; j++) {
    document.write("x");
  }
  document.write("<br>");
}
y is space and x is *
 
  • #14
shivajikobardan said:
y is space and x is *
Ok. Why not use the correct symbols? If you run it does it do what you wanted it to do? If not, you need to make some changes to your logic. (I think you have forgotten some things.)
 
  • #15
Code:
for (i = 1; i <= 5; i++) {
  for (space = 1; space <= 5 - i; space++) {
    document.write("0");
  }
  for (j = 1; j <= i; j++) {
    document.write("X0");
  }
  document.write("<br>");
}

This has been solved now.
 
  • #16
I assume that you know that you can use the space bar like any other character. So you could print " " and "* " instead of "0" and "X0", respectively.
 
  • #17
It's also worth googling for things like "how do I repeatedly print the same string in javascript". That would lead you to the String.repeat function, the use of which would shorten and (I suspect) speed up your code.
 
  • Like
Likes FactChecker
  • #18
I've not learnt about strings yet. Honestly, I'm finding programming very undoable and hard. :( Any tips in physicsforums for it?
 
  • #19
shivajikobardan said:
can anyone help to do with the logic I laid out here? It'd be great.
FactChecker said:
Perl:
$N=10;
foreach $n (1..$N){
    $line = ' 'x($N-$n);
    $line = $line . "* "x$n;
    print "$line\n";
}
I don't speak Perl, but if I had to guess, here's what I think the code above does.
The variables here are $N, $n, and $line. The foreach loop here will execute 10 times, once for each value of $n, which is the loop control variable. $line is a string variable
The first statement in the loop inserts 10 - 1 spaces in $line. The second statement in the loop appends $n asterisks onto the string in $line (1 asterisk the first iteration, 2 asterisks the second time, and so on).
The third statement in the loop prints the string in $line, followed by a newline character.
 
  • Like
Likes FactChecker
  • #20
Mark44 said:
I don't speak Perl, but if I had to guess, here's what I think the code above does.
The variables here are $N, $n, and $line. The foreach loop here will execute 10 times, once for each value of $n, which is the loop control variable. $line is a string variable
The first statement in the loop inserts 10 - 1 spaces in $line. The second statement in the loop appends $n asterisks onto the string in $line (1 asterisk the first iteration, 2 asterisks the second time, and so on).
The third statement in the loop prints the string in $line, followed by a newline character.
Yes. And I should be ashamed for:
1) not commenting my code after I have criticized others so often for no comments.
2) using unclear, generic, variable names like N
It is an example of bad coding practices.
 

FAQ: How to print this pyramid pattern?

How do I print a pyramid pattern using loops?

To print a pyramid pattern using loops, you can use nested for loops. The outer loop will control the number of rows in the pyramid, while the inner loop will control the number of stars to print in each row.

Can I print a pyramid pattern with user-defined height?

Yes, you can print a pyramid pattern with any height specified by the user. You will need to use a loop to iterate through the number of rows and adjust the number of stars printed in each row accordingly.

How can I print a mirrored pyramid pattern?

To print a mirrored pyramid pattern, you will need to use two nested loops. The outer loop will control the number of rows, while the inner loop will print the spaces and stars in a mirrored fashion.

Is it possible to print a pyramid pattern in different shapes?

Yes, it is possible to print a pyramid pattern in different shapes such as diamond, inverted pyramid, and hollow pyramid. You will need to adjust the logic in your loop to achieve the desired shape.

Can I print a pyramid pattern with a different symbol instead of stars?

Yes, you can print a pyramid pattern using a different symbol instead of stars. You will need to change the symbol used in the inner loop to print the desired pattern.

Similar threads

Replies
22
Views
4K
Replies
9
Views
1K
Replies
97
Views
8K
Replies
28
Views
29K
Replies
3
Views
4K
Replies
6
Views
1K
Back
Top