7 Replies - 3233 Views - Last Post: 05 October 2012 - 12:04 AM Rate Topic: -----

#1 tainted  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-October 12

Creating a Pyramid of numbers

Posted 04 October 2012 - 04:11 PM

Hey guys. I am in Intro to Computer Programming, and I am having trouble with one of my programming assignments due two Monday's out. (10/15) I need to create a pyramid of numbers

                                       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 what I have so far and it outputs the right half of the pyramid.
public class fourpointnineteen {
	public static void main(String[] args) {
		int c = 0;
		for(int r = 0
				; r <= 7; r++) {
			for(c = 0; c <= r ; c++) {
					if (c == 0) {
						if (Math.pow(2,r) < 10)
							System.out.print("    " + (int)Math.pow(2, r));
						else if (Math.pow(2, r)< 100)
							System.out.print("   " + (int)Math.pow(2, r));
						else
							System.out.print("  " + (int)Math.pow(2, r));
					}
					else {
						if ( Math.pow(2, (r-c)) < 10)
							System.out.print("    " + (int)Math.pow(2, (r-c)));
						else if ( Math.pow(2, (r-c)) < 100)
							System.out.print("   " + (int)Math.pow(2, (r-c)));
						else
							System.out.print("  " + (int)Math.pow(2, (r-c)));
					}
			}
			System.out.println(" ");
		}
	}
}


Outputs the following:
[code]
   1
   2    1
   4    2    1
   8    4    2    1
  16    8    4    2    1
  32   16    8    4    2    1
  64   32   16    8    4    2    1
 128   64   32   16    8    4    2    1



My problem is that I'm not sure how to replicate this on the other side mainly due to the spacing. Do I have to count up the amount of spaces that need to be in front of each row?

Thanks in advance guys!

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a Pyramid of numbers

#2 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Creating a Pyramid of numbers

Posted 04 October 2012 - 05:06 PM

Okay, I have a few questions about your assignment. Does it have to be applicable to any numbers, do you simply have to display specific numbers? I am thinking maybe you could do something much simpler than this mess of if statements for example perhaps an array table?

I do not want to do your assignment for you, but mess about with this


public class TableDisplay {

	public static void main(String[] args){
		System.out.println("\t\tPyramind of Numbers");
		int numbers[]={1, 2, 3, 4, 5};
		
		for(int counter=0;counter<numbers.length;counter++){
			System.out.println("\t\t\t" + numbers[counter]);
		}
	}
}



I think something like this may help a bit. Let me know what you think cause I am new to java as well, this is just how I would go about doing something like this. This code is also assuming there is an end to the numbers you are using, and that you know what numbers you are using.

This post has been edited by Ytry: 04 October 2012 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 tainted  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-October 12

Re: Creating a Pyramid of numbers

Posted 04 October 2012 - 06:45 PM

Hey man, thanks for the reply. The numbers have to look exactly like the output i showed. It says to use nested-for loops (Sorry for not mentioning that.) Also we haven't learned about arrays yet,so I am unsure if our professor would want us to do that. We may learn about them in the upcoming week because this assignment is due 2 weeks out still.
Was This Post Helpful? 0
  • +
  • -

#4 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Creating a Pyramid of numbers

Posted 04 October 2012 - 06:52 PM

No problem, well seeing as my way will not work due to the fact that you have to use nested for loops, I will play with it a bit, and see what I can come up with. by the way looking at your code I see you print out five spaces like this
System.out.println("     ");
//Another effective way of doing the exact same thing is by doing this
System.out.println("\t");
//Using a \t will tab over once which is exactly 5 spaces


This post has been edited by Ytry: 04 October 2012 - 07:02 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating a Pyramid of numbers

Posted 04 October 2012 - 07:58 PM

I have the feeling you make it over but really over but really over complicated
If you know what is the center bottom row element shouldn't be that complicated
Was This Post Helpful? 0
  • +
  • -

#6 tainted  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-October 12

Re: Creating a Pyramid of numbers

Posted 04 October 2012 - 11:08 PM

Hey guys! Thanks for your help! I think I almost have it, but I cannot figure out the errors
public class fourpointnineteen {
	public static void main(String[] args) {
		int c = 0;
		for(int r = 0; r <= 7; r++) {
			for(c = 0; c <= (r+7); c++) {
				if (c == 7) {
					if (Math.pow(2,r) < 10) 
						System.out.print("    " + (int)Math.pow(2, r));
					else if (Math.pow(2, r) < 100) 
						System.out.print("   " + (int)Math.pow(2, r));
					else 
						System.out.print("  " + (int)Math.pow(2, r));
				}
				else if (c > 7) {
					else if (Math.pow(2, (r - (c - 7))) < 10)
						System.out.print("    " + (int)Math.pow(2, (r - (c - 7))));
					else if (Math.pow(2, (r - (c - 7))) < 100)
						System.out.print("   " + (int)Math.pow(2, (r - (c - 7))));
					else
						System.out.print("  " + (int)Math.pow(2, (r - (c - 7))));
				}
				else { //	Syntax error, insert "}" to complete Statement
					   //	Syntax error on token "else", delete this token
					if (Math.pow(2, (r + (c - 7))) == 0)
						System.out.print("\t");
					else if (Math.pow(2, (r + (c - 7))) < 10)
						System.out.print("    " + (int)Math.pow(2, (r + (c - 7))));
					else if (Math.pow(2, (r + (c - 7))) < 100)
						System.out.print("   " + (int)Math.pow(2, (r + (c - 7))));
					else
						System.out.print("  " + (int)Math.pow(2, (r + (c - 7))));
				}
			}
			System.out.println(" ");
		} //	Syntax error on token "}", delete this token
	}
}



I have tried counting all the { and } and I get the same amount. Even Eclipse shows that all the pairs are matched up properly. I will edit the completed code out after I get this resolved so that it isn't on Google for people to search, just need help ATM thanks.
Was This Post Helpful? 0
  • +
  • -

#7 tainted  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-October 12

Re: Creating a Pyramid of numbers

Posted 04 October 2012 - 11:51 PM

Alright guys, I figured out the error =/ I had an extra else in front of if in line 15. Can anyone tell me how to edit a post?

Also for whatever reason I have 2 middle columns the exact same in the middle now.
Was This Post Helpful? 0
  • +
  • -

#8 tainted  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-October 12

Re: Creating a Pyramid of numbers

Posted 05 October 2012 - 12:04 AM

I'm sorry for triple post, but I do not know how to edit on these forums. I finally got the program to work 100% in perfect form although it is rather complex. If anyone wants to take a look at it and give me advice on how to simplify it while still using nested for loops, just message me, or post here, and I will send you the code so I do not post completed code here. Thanks guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1