• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Chris Wallington
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...

 
somashaker goud
Ranch Hand
Posts: 63
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • 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)
 
Nate Nio
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!
 
Junilu Lacar
Sheriff
Posts: 11164
160
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: 55761
163
  • 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.
 
Piet Souris
Rancher
Posts: 1983
67
  • 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: 1983
67
  • 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: 55761
163
  • 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: 1983
67
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!