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:
Sheriffs:
Saloon Keepers:
Bartenders:

# Creating a table using 'for loop' for range of integers

Greenhorn
Posts: 1
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
You have everything in loop so it looks like code splits out table. So place

and trailer sysout

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: 59146
180
Welcome to the Ranch

You would probably do well to write
x * x * x
Math.pow(x, 3)

Greenhorn
Posts: 2
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: 12022
196

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: 59146
180
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.

Master Rancher
Posts: 2658
91

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

Can you elaborate?

Piet Souris
Master Rancher
Posts: 2658
91
@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: 59146
180

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
Master Rancher
Posts: 2658
91
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

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.

 We find this kind of rampant individuality very disturbing. But not this tiny ad: Rocket Oven Kickstarter - from the trailboss https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss