Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

pyramid of numbers  RSS feed

 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying below code to print pyramid of numbers


i got output as below


Please enter number:
123456
  1
  22
  333
  4444
  55555
  666666



i want instead output something like pyramid as below with proper spaces at the beginning and at the end when i enter any given number like 123456 or 123 or 786 and also i need those numbers to be sorted alos



Please enter number:
123456
       1
      22
     333
    4444
  55555
666666

how to tweak my code to get pyramid of sorted numbers as above?
please advise
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot tweak that code. You don't even try. You get all that code out of the main method, you get rid of the comments which don't tell us anything we don't know already, and you create a NumberPyramid class. Sorting, creating arrays, and displaying are all separate actions which shou‍ld be done in different methods.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try something like this
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please have a look at what it says at the top of this forum.
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
It does no good to supply a ready‑made answer. Don't be annoyed, but I have pulled rank and removed the solution.

You had len in the solution. Not only is that a poor name for a variable, but it is usually a bad idea to create a variable somewhere else and use it as part of the continuation condition for a for statement. Use myArray.length.
 
Carey Brown
Saloon Keeper
Posts: 2840
43
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:i want instead output something like pyramid as below with proper spaces at the beginning and at the end when i enter any given number like 123456 or 123 or 786 and also i need those numbers to be sorted alos

Please enter number:
123456
       1
      22
     333
    4444
  55555
666666
Could you please repost this and enclose it in "Code" tags so that we can see the exact format that you are looking for. And one for "768" also.

Edit: How are you supposed to handle the fact that some lines will have an even number of characters, and some odd?
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i post formatting is being realigned somehow. But i would like to get my output as attached.

formatting.png
[Thumbnail for formatting.png]
format
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A problem clearly stated is a problem half solved —Dorothea Brande

If you can't state the problem any clearer than "I want ... proper spaces at the beginning and at the end ..." then how do you expect anyone to help you?

What is your definition of "proper spaces" -- give a few different examples if you can't come up with a generally applicable description.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try something like this
?
1
throw new IfCampbellSeeThisHellDeleteItException();


Hi Sankalp I have not got what you are trying to say. can you please elaborate?
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i see at the top like

A friendly place for programming greenhorns!

but some of the comments are not at all friendly esp. above comment by Sankalp.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please delete comments that are not relevant.
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:but some of the comments are not at all friendly esp. above comment by Sankalp.

His original comment provided solution code, something that we do not allow in these forums because that would encourage plagiarism and dishonesty. The solution code that Sankalp provided was edited by a moderator to what you see now.
 
Carey Brown
Saloon Keeper
Posts: 2840
43
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with the pyramid as shown in your screen shot is that the position of digits in alternate rows are shifted by half a character width, which is impossible. You could do this if there was a space between each digit, otherwise we'd need a screen shot of what would be expected for this odd/even row problem without moving a line by half a character width.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with the pyramid as shown in your screen shot is that the position of digits in alternate rows are shifted by have a character width, which is impossible.
You could do this if there was a space between each digit, otherwise we'd need a screen shot of what would be expected for this odd/even row problem without moving a line by half a character width. 


i am fine having a space between each digit. How to achieve that? please advise
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:i am fine having a space between each digit. How to achieve that? please advise

Anyone who answers that directly will be violating our forum etiquette/rule of not providing direct solutions. Try to work it out for yourself first and show us what you come up with. It's not hard to figure out how to add a space between the numbers in your output.
 
Carey Brown
Saloon Keeper
Posts: 2840
43
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now, starting at line 6, how many leading spaces do you need? Line 5?, Line 4? See a pattern forming?
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:The problem with the pyramid as shown in your screen shot is that the position of digits in alternate rows are shifted by half a character width, which is impossible.

More precisely, it is the effect of using a variable-width font and kerning, which isn't necessarily always half a character width. To get the desired result, he needs to use a fixed-width font like what he'd get by using code tags, and one space between digits, assuming he's going to use single digits only to build his pyramid.  If OP is using Eclipse or similar IDE, then he needs to ensure that the console is using a fixed-width font or he'll be adjusting his program until the cows come in.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


you mean like above add space beore printing the number in the for loop?

i got below wrong output


Please enter number:
126
  
1
  
2
2
  
6
6
6
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, starting at line 6, how many leading spaces do you need? Line 5?, Line 4? See a pattern forming?


i see pattern where empty spaces keep on reduceing from line 1 to 2 to 3 to 4 to 5 to 6 etc
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i see you are trying to use two additional methods one to print digits and other to print spaces.

here spaces seems tricky where at line one it has number_of_digits/2 and then keep on reducing as we go down i think?
 
Carey Brown
Saloon Keeper
Posts: 2840
43
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:
Line 37 needs to be print() not println().
 
Carey Brown
Saloon Keeper
Posts: 2840
43
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:here spaces seems tricky where at line one it has number_of_digits/2 and then keep on reducing as we go down i think?

Try number_of_digits - row_number.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:

you mean like above add space beore printing the number in the for loop?

i got below wrong output


Please enter number:
126
  
1
  
2
2
  
6
6
6


You got confused with my comment because that was modified by one of the moderators. My original comment was deleted.
Anyways, see the difference between print() and println() methods. You are printing both spaces and digits so you have to adjust your code according to your need with perticular spaces in between
 
Liutauras Vilda
Marshal
Posts: 4483
304
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Watch how that mess you have posted above should be looking like (at least).

Spacing between the lines can be arguable.

Can you try to wrap those code lines to a methods, where each of them (along with their names) would tell exactly what they are meant to do and would do only that one thing?

i.e.: lines 7,8, 9, 10 would be replaced with:
And somewhere down the code you'd have:

Can you try to look for more cases to make methods out of them? That way possibly would be a bit easier to cope with an exercise.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spacing between the lines can be arguable.


how to align the spaces. I do control A to select all the code in the file then control+shift+f to format which is formatting other stuff other than extra lines of space between lines. What is optimal way of formatting the code?
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyways, see the difference between print() and println() methods. You are printing both spaces and digits so you have to adjust your code according to your need with perticular spaces in between



above gave below output but trailing spaces are not coming correct


Please enter number:
124
   1
   2   2
   4   4   4


please advise
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:
above gave below output but trailing spaces are not coming correct

Please enter number:
124
   1
   2   2
   4   4   4

please advise

How do trailing spaces even make any difference in how the output looks?

You could have 500 spaces after the 1 there and then a linefeed and it wouldn't affect how your next line would look, unless your output window was set up to wrap on long lines. Forget about trailing spaces. Concentrate on printing out the correct number of leading spaces.
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:please advise

After looking at your code, I see that you must have just got mixed up with "trailing" and "leading" -- there is a difference you know. The word "trailing" means after while "leading" means before. Be careful with the terms you use so you don't cause misunderstandings.

I think your mind is trapped and entangled by all the details you're trying to work out.  Ignore the details first.  Describe at a high level what you're trying to do.  If you can't even do that much, to get a big picture idea of what EXACTLY you want to do, then how do you expect to put all the minute detailed parts together to make it actually work?

Break down the large problem into smaller chunks. Then break each small chunk into even smaller chunks. Keep doing that until you get to a point where you have to write detailed code. At that point, the details will be so small and focused that it will be trivial to write.

I'll start you off:

HIGH LEVEL:  Print a triangle from numbers entered by the user

NEXT LEVEL:
Given a String entered by the user,
Split the String up into an array that has 1 element per character in the String
Print each character as a line in the pyramid.

NEXT LEVEL: (print each character as a line in a the pyramid)
Print leading spaces
Print the character
Print a linefeed

NEXT LEVEL: (continue this breakdown)

You need to learn how to organize your thoughts, break down tasks, and attack the problem systematically and logically.

What you have been doing so far seems to little more than just pure guessing. You can't write correct programs by guessing. Think, plan, organize. That's how you can get it done.  If you learn how to use the above approach, you can solve this problem in less than 15 minutes.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

HIGH LEVEL:  Print a triangle from numbers entered by the user

NEXT LEVEL:
Given a String entered by the user,
Split the String up into an array that has 1 element per character in the String
Print each character as a line in the pyramid.

NEXT LEVEL: (print each character as a line in a the pyramid)
Print leading spaces
Print the character
Print a linefeed

NEXT LEVEL:
print leading space before a character
print the characters in the incremental order each line like 1 time in first line and 2 times in 2nd line and 3 times in 3rd line etc


I updated approach further as above and wrote code as below



Please enter number:
21
integerArray is [0, 0]
Splitted the String up into an array that has 1 element per character in the String [2, 1]
integerArray before sorting[2, 1]
integerArray after sorting[1, 2]
1
2 2


still leading space issues are there which i guess i am not approaching properly
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i tried as above and got below output

Please enter number:
4397
integerArray is [0, 0, 0, 0]
Splitted the String up into an array that has 1 element per character in the String [4, 3, 9, 7]
integerArray before sorting[4, 3, 9, 7]
integerArray after sorting[3, 4, 7, 9]
3
44
  777
   9999


I got spaces in reverse order though.

i followed below link
http://javaconceptoftheday.com/how-to-create-pyramid-of-numbers-in-java/
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're still having major disconnects between the concept of the  solution and the actual coding of the solution.

I think a lot of it has to do with the names you have chosen for your methods. Take for example the printDigit() method. Is that really doing what the name says it's doing?  Do you really need that many lines of code to print a single digit?

I can hear you say, "The method doesn't actually print just one digit. It does more than that." 

Well, then why make your code say one thing and then turn around and do a totally different thing? Do you not think that's misleading your mind somehow and confusing you?  Have you ever tried the Stroop Effect Test?  The meaning of words in your program need to match their intent.  Otherwise, you're writing a bunch of lies.  I hate code that lies to me.  That's the kind of code that usually has problems and lots of bugs.

Make your code DO what it SAYS IT DOES.
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I mean by "... disconnect between the concept of the solution and the actual coding of the solution."

I gave you this top high-level description of the problem/solution:  Print a triangle from numbers entered by the user.

This English sentence should translate to the code that's in your main() method.  Look at the code in your main() method:

Can you honestly tell me that this is what "Print a triangle from numbers entered by the user" translates to in Java? It doesn't, right? Not even close.

How about this version of main():

Doesn't that more closely match the sentence, "Print a triangle from numbers entered by the user"?

Now, drilling down to the next conceptual level that I gave you:

NEXT LEVEL:
Given a String entered by the user,
Split the String up into an array that has 1 element per character in the String
Print each character as a line in the pyramid.

How would that translate into Java code?
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Ridley Stroop used the term "incongruent", what I called a "disconnect" -- In other words, your solution code is incongruent with the solution intent.  That is, your intent (the plain English high-level conceptual description) does not match what the code says it does.  This causes confusion and it leads your mind astray.

Like I said before, make your code actually DO what it SAYS IT DOES. Also, make your code match your plain English high-level conceptual description of the solution.  The more you can make all these congruent with each other, the more certain you can be that your code is doing the right thing.  Conversely, the less congruent these are, the more likely it is that your program is wrong.
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, if you can't even describe your solution clearly in plain English, then you're back to square one.

"A problem clearly stated is a problem half solved" —Dorothea Brande
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am wondering why we need separate integer array and one other separate string array?
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


based on the star pyramid program i got below output

8219
integerArray is [0, 0, 0, 0]
Splitted the String up into an array that has 1 element per character in the String [8, 2, 1, 9]
integerArray before sorting[8, 2, 1, 9]
integerArray after sorting[1, 2, 8, 9]
    *
   * *
  * * *
* * * *


i need to replace star with actual numbers
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i cleaned up as above.

i am getting below exception


461
integerArray is [0, 0, 0]
Splitted the String up into an array that has 1 element per character in the String [4, 6, 1]
integerArray before sorting[4, 6, 1]
integerArray after sorting[1, 4, 6]
   Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at PyramidSortedNumber.main(PyramidSortedNumber.java:30)



I would like to separate to separate methods as well once above works fine
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


above gave below descending sorting output

329
integerArray is [0, 0, 0]
Splitted the String up into an array that has 1 element per character in the String [3, 2, 9]
integerArray before sorting[3, 2, 9]
integerArray after sorting[2, 3, 9]
   9
  33
222
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


pyramid star worked as above
 
Liutauras Vilda
Marshal
Posts: 4483
304
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sai rama krishna,

Have you read what Junilu wrote 2 posts ago? I really believed you'll absorb that information. Basically how I understood what he said, that you should take very small steps at a time and make some methods, so each of them would do very concrete singular task.

And what has happened now? You just completely ignored a good advice and trashed all two days of his invested time in giving advices and trying to teach you how this exercise actually supposed to be tackled.

Don't you want to open a new project in your programming environment and using currently writtent cram of code use as a reference to build what he said? Seriuosly, some guys here are long enough, so they like to see some improvements from students rather than same standard routine.

Not much point in helping you if you don't want to accept that help.

Now about your exercise. So you got those stars right according to you and need to replace with numbers. Interesting enough, but if you were to create those function he suggested earlier (didn't mention explicitly all those), all would be obvious how to convert them to numbers, moverover, you wouldn't have come up with starts, but rather with complete solution already.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!