• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pyramid of numbers

 
Ranch Hand
Posts: 959
2
  • 3
  • Number of slices to send:
    Optional 'thank-you' note:
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
 
Marshal
Posts: 79422
377
  • 2
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Ranch Hand
Posts: 64
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
You could try something like this
 
Campbell Ritchie
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Saloon Keeper
Posts: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:

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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:

 
sai rama krishna
Ranch Hand
Posts: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:
when i post formatting is being realigned somehow. But i would like to get my output as attached.

formatting.png
format
 
Sheriff
Posts: 17652
300
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:

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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:
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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:
Can you please delete comments that are not relevant.
 
Junilu Lacar
Sheriff
Posts: 17652
300
  • Number of slices to send:
    Optional 'thank-you' note:

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: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:
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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:

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: 17652
300
  • Number of slices to send:
    Optional 'thank-you' note:

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: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:


Now, starting at line 6, how many leading spaces do you need? Line 5?, Line 4? See a pattern forming?
 
Junilu Lacar
Sheriff
Posts: 17652
300
  • Number of slices to send:
    Optional 'thank-you' note:

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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:


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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:

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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:

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: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:

sai rama krishna wrote:

Line 37 needs to be print() not println().
 
Carey Brown
Saloon Keeper
Posts: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:

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
  • Number of slices to send:
    Optional 'thank-you' note:

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
 
Marshal
Posts: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:
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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:

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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:

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: 17652
300
  • Number of slices to send:
    Optional 'thank-you' note:

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: 17652
300
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:


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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:

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: 17652
300
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
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: 17652
300
  • Number of slices to send:
    Optional 'thank-you' note:
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: 17652
300
  • Number of slices to send:
    Optional 'thank-you' note:
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: 17652
300
  • Number of slices to send:
    Optional 'thank-you' note:
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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:
i am wondering why we need separate integer array and one other separate string array?
 
sai rama krishna
Ranch Hand
Posts: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:


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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:


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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:


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: 959
2
  • Number of slices to send:
    Optional 'thank-you' note:


pyramid star worked as above
 
Liutauras Vilda
Marshal
Posts: 8880
638
  • 3
  • Number of slices to send:
    Optional 'thank-you' note:
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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |