There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Originally posted by fred rosenberger:
well, without looking deeply at your code, i suggest the following line of attack.
you are either not putting anything into the array, or you are not correctly printing out the array.
I'd suggest putting a few "System.out.println()" statements in your code. for example, every time you try inserting something into the array, print out the value.
put in a print statement showing the result of your (i+1)%z. and a few other places.
FWIW, 'z' is a horrible variable name. why not use something meaningful, like 'numOfInputs'?
also, why does your loop go from 0-30? where does the 30 and the 32 come from i see in the code? these are called 'magic numbers', because they appear like magic. Nobody knows what they really mean. if you only want to loop for the number of times the user input, then loop like this:
for (i=0; i<z; i++)
lastly, note that each time through your loop, you call the RedoMethod.arr() function, thus re-bulding your array MANY times...
Originally posted by Rodrigo Tomita:
Jack,
Think for a moment about what you are doing in the code below:
I think you need to think about whether your method arr() is going to do the calculations for all the 30 numbers at once (so you have a for inside it) or if it is supposed to give you an answer for just one item (the one you gave by passing i as an argument).
Also, think about the role of the i coming as argument of arr() the way you coded it - does it matter if you set i = 0 or i = 10?
Good luck.
Rodrigo
[ May 12, 2008: Message edited by: Rodrigo Tomita ]
Originally posted by Rodrigo Tomita:
Jack,
Yes, you are correct. What I'm trying to say is that the way your arr() method is designed, you do all the calculations at once and store it to your answer[] array. Then check what you return.
Also, notice that you call the arr() method 3 times in your main method. That's why I'm asking you if your point is to do all the calculations inside arr() and return all the results to main or just give the answer back for a given i.
Try to put some println(i) in your code and check it out. Sorry if it sounds vague, but I'm trying to do the "focus on helping them discover their own solutions, instead of simply providing answers."
Originally posted by Rodrigo Tomita:
ok, so let's cut to the chase
Suppose that you call you method: arr(10), so i == 10 at that point.
Then you have a for loop using the same i - so the i == 0 at the begin of the first loop and will be 30 at the end of it.
Then you have a second for loop using again i - so i == 0 at the begin of the loop and will be 29 at the end of it.
Then you do a return answer[i]. The i variable is certainly not equals to 10 at that point, right?
So, you are in the right path. Answering to your question, I believe it would make more sense if you return answer (the whole array) to your main(). That way you do the calculations only once. So your code would look like:
//Perform calculations
double[] answer = RedoMethod.arr(i);
//Print out the values of array3
for(i=0; i<30; i++){
if((i+1)%z==0)
System.out.println( answer[i] );
else
System.out.print( answer[i] );
}
}
//...
//Definition of method arr
public static double arr(){
//...
for (int i = 0; i < 30; i++) {
//...
}
Hope it helps.
[ May 12, 2008: Message edited by: Rodrigo Tomita ]
[ May 12, 2008: Message edited by: Rodrigo Tomita ]
but RedoMethod.arr(i) is just a number right?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Originally posted by fred rosenberger:
no. it's a method call that RETURNS a number.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Originally posted by fred rosenberger:
ughhh... ok. Remember how above I talked about using better variable names? look VERY CLOSELY at your arr() method.
you pass in a paramter, and call it i.
you then, in your first loop, use as an index a variable named... i. so, you've just blown away whatever data you passed in.
you use it AGAIN in the second loop, blowing it away again... so it would appear that by the time you exit, no matter what, i is going to be 31.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Originally posted by Garrett Rowe:
There are lots of things wrong there. First you've declared your method to return a double[], but in the calling method you're trying to assign the result to an element of a double array, which is a double. Also, as others have said, you pass an int "i" to your method, and the first thing you do with it is reset it to zero. (The i in your loop is the same i in your method parameter).