Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Creating a pyramid

 
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, can anyone help me with this? I need to make a pyramid that displays these numbers but I have no idea how to do this. I end up getting different numbers besides the ones I need.
                         1                            
                                             1   2   1                        
                                        1   2   4   2   1                    
                                    1   2   4   8   4   2   1              
                               1   2   4   8  16   8   4   2   1        
                           1   2   4   8  16  32  16   8   4   2   1    
                      1   2   4   8  16  32  64  32  16   8   4   2   1  
                1   2   4   8  16  32  64 128  64  32  16   8   4   2   1

This is the code I'm working with:

 
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 I end up getting different numbers besides the ones I need.


Please copy the program's output and paste it here to show what you are talking about.
 
Andrea Menjivar
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what it currently looks like
Screenshot-(93).png
[Thumbnail for Screenshot-(93).png]
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, that looks like a good shape.  Now you need to change what numbers are printed in the shape.  
What is the relationship between the current numbers that are printed and the desired numbers?
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your second loop, you should be printing the  (j-1)th power of 2, not j. Same with third loop.
But you should change the number of spaces printed in the first loop depending on the number of row and therefore the number of digits in the largest value, so that the pyramid will look symmetrical.
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic