Need Assistance with a program
I am doing can exercise where i have to find the largest integer n such that n^3 < 12,000. Below is the code i wrote, i believe i wrote an infinite loop.
Could you assist me please by showing me how i could end this loop as I have tried various ways and they dont seem to work and tell me if i have done things properly.
import java.lang.*;
import java.util.*;
public class Exercise04_13
{
public static void main(String[] args)
{
int n = (int) Math.random() * 100;
int nCubed = (int) Math.pow(n,3);
while( nCubed < 12000 )
{
if (nCubed == 10648)
{
System.out.print("The largest integer, given that n^3 < 12000 is: " + n);
}
else
{
n = (int) Math.random() * 100;
}
}
}
}
Re: Need Assistance with a program
Hi abi, welcome to the forums.
You are right in your assumption, your code does generate an infinate loop. The reason for this is that your while loop loops whilst nCubed is less than 12000. As the value of nCubed does not change within the loop, if nCubed is intially set to below 12000 then this will loop indefinately.
Regards.
Re: Need Assistance with a program
I should add that the value of n is always going to be 0.
Code:
int n = (int) Math.random() * 100;
The above code converts Math.random to an integer prior to multiplying this by 100. Instead place the whole equation in brackets.
Code:
int n = (int) (Math.random() * 100);
Please also use the [code] tags for any code, it makes it easier to read.
Regards.
Re: Need Assistance with a program
First, what are the requirements of the exercise? Are you expected to creep up on the value by successively by incrementing a value starting at 1? Or could you just take the cube root of 12000 and then truncate the fractional part? I am not certain why you are using random numbers at all. Since the answer is 22 and 22 cubed is 10648 it looks like you already have the answer encoded in your program.
Regards,
Jim
Re: Need Assistance with a program
Thanks. I hadn't realized that. The smallest things escape you don't they. Thanks for the welcome too.
I want the value of the nCubed to change within the loop as well, how can i get it to do that. Without that happening the code would be pointless, if you know what Iam trying to say.
Re: Need Assistance with a program
All the exercise says is find the largest integer n such that n^3 <12000 using a while loop.
Arent we supposed to use random no.s? I was using the answer to make the loop definite but it doesnt seem to be working as you may have noticed.
Re: Need Assistance with a program
Well, based on what you just posted, you don't need a random number. Just continue to increment an initial value say of zero by 1. Then cube it and compare to 12000. If it is greater than 12000, then the previous value is the largest integer which when cubed is less than 12000. Otherwise, try the next sequential number. You can use a break statement to exit the loop when you have found the number.
Regards,
Jim
Re: Need Assistance with a program
something like this
int n = 0;
int nCubed = (int) Math.pow(n,3);
while( nCubed < 12000)
{
n += 1;}
if (nCubed == 10648)
{
System.out.print("The largest integer, given that n^3 < 12000 is: " + n);
}
else
{
++n;
}
its faulty but I wanna know if I am getting what your trying to say. Also, am i on the right track? I tried this and iam not getting any output at all.
Re: Need Assistance with a program
Not quite. First, forget about including the if statement with 10648 in your code as it is not necessary. You are not updating nCubed at all so it will never change and the loop will go on forever. All you need in your while loop is
Code:
n += 1;
nCubed = n * n * n;
As soon as nCubed is greater than or equal to 12000, the loop will terminate. This means that the value n must be 1 greater than the value you really want so you need to subtract 1 from it.
Then print out that answer.
Regards,
Jim
Re: Need Assistance with a program
Thanks man, I got it now. Although, I am still confusing myself with the algorithm. Must be because I am tried that I am not thinking properly. I'll look at it again in the morning.
I did get it to work though.
int n = 0;
int nCubed = n * n * n;
while( nCubed <= 12000)
{
n += 1;
nCubed = n * n * n;
}
System.out.print("The largest integer, given that n^3 < 12000 is: " + ( n - 1 ));
Re: Need Assistance with a program
I was wondering if you know how to convert the deci values for ASCII characters to display as the characters themselves in the output?
e.g.
if i put System.out.print( 65 ) will that by default show an A??
Re: Need Assistance with a program
Nope! Check out the Character and String class methods. There should be some conversion methods there.
Jim
Re: Need Assistance with a program
FYI turns out you have to cast it s.o.p((char)65)
Re: Need Assistance with a program
Yep! Totally forgot about the cast.
Jim