Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Creating a table using 'for loop' for range of integers  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Need some help with my code (working on an assignment). I thought I was done but, the program I have written (create a program that calculates cube values between a range of two (user entered ) positive numbers) requires a table, but my code spits out a table for each value in the range...

Any suggestions to fix this would be greatly appreciated...

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have everything in loop so it looks like code splits out table. So place

header sysout



and trailer sysout



out of your for loop.

One more question ? why are you calculating cubes of 3 variable inside loop. Since you are iterating loop for all the numbers in your range find cube of each number and print.
 
Marshal
Posts: 60101
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You would probably do well to write
x * x * x
instead of
Math.pow(x, 3)
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you might be over-complicating the code. If you must use a for loop, you can structure your code as follows:



This saves you from creating extra variables and uses fewer lines of code. The reason for declaring 'z' is to preserve the user-entered number for 'x'. Hope this helps!
 
Sheriff
Posts: 12331
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chris Wallington wrote:


All these comments are pointless: They don't add anything to what the code already clearly says.  You might as well be walking around and announcing, "I'm putting my left foot forward to walk one step. I'm putting my right foot forward to walk one step. I'm putting my left foot forward to..."  You would have been better served by putting the effort into aligning and indenting your code properly.

One thing that might help you see your mistake more clearly is choosing better names for your variables.  What does 'x' represent? What does 'y' represent? And what does 'z' represent? The names x, y, and z have no relevance to what they stand for so they hide the intent of your program.  Good variable names help clarify what your program is doing and explain the purpose/intent of the values they represent.  Take 'x', for example. Isn't that the first number? The lower limit of the range? Then why not call it lowerLimit instead? Now, if you used that name, would it make sense to print the cube of the lower limit every time through the loop?  What about the variable 'y', what does it represent?  The variable 'z'?  Answer these questions and you might realize which variable makes sense to actually calculate the cube for and print out the result.
 
Campbell Ritchie
Marshal
Posts: 60101
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nate Nio, welcome to the Ranch

Please don't be annoyed with me, but I am afraid I have pulled rank and deleted part of your post. We have this

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.

...written on the home page for this forum. We believe it doesn't help to supply too complete an answer too quickly. I have removed three lines.
Please work out why the advice in line l12‑13 is potentially dangerous.
 
Rancher
Posts: 2826
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You would probably do well to write
x * x * x
instead of
Math.pow(x, 3)


Can you elaborate?
 
Piet Souris
Rancher
Posts: 2826
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Chris
my first thought was to write down all the cubes themselves between two limits, So, if the two limits are 25 and 100, should you output 25^3, 26^3, et cetera, or just the numbers 27 and 64?
 
Campbell Ritchie
Marshal
Posts: 60101
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . Can you elaborate?

No need for logarithms which would underlie the calculations in Math#pow. Slightly faster execution. Probably less of an advantage than for squares.
 
Piet Souris
Rancher
Posts: 2826
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. I did some speed tests, measured hardly any speed difference. But I think I got fouled by some compiler optimization. Eliminating that, there is indeed a significant speed difference, in favor of x * x * x.
 
Nate Nio
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Nate Nio, welcome to the Ranch

Please don't be annoyed with me, but I am afraid I have pulled rank and deleted part of your post. We have this

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.

...written on the home page for this forum. We believe it doesn't help to supply too complete an answer too quickly. I have removed three lines.
Please work out why the advice in line l12‑13 is potentially dangerous.



Whoops. Was a bit overzealous. Thank you for reminding me. I don't remember the code for those lines but I was trying to use what OP wrote.

Since OP didn't provide a range for x and y, user input could output especially large numbers, throwing the format of the table off. The list could also be exceptionally long.

I feel I might be overlooking something.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!