This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Best way to figure it out is just to walk through it. You have a nested loop. In the outer loop you go through creating an index starting at 0 and looping until you have made 4 loops. Each time through you are creating an int array in triArray that holds as many as the number of times that you have been in the loop. (when i=0 the first time, you make an array with 0+1, next time you make an array of 1+1 ETC.) Inside each of the outer loops is another loop that only goes for as long as the length that you made that level of triArray. So the first time through, you made an array of length 1, so you fill it with the value of val which is 1. The second time through you have a length of 2 which you fill with 2 and then 3, the thrid time you have a length of 3 which you fill with 4 then 5 then 6 etc.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Yes there are lot of ways to solve these kind of problems.. you should first check whether any choices can be dropped.. you can apply it to any problem. 1.) figure out the problem ..ie. make an algorithm 2.) check out the choices.. (if this works you're lucky ) 3.) logical reasoning.. mathematics (find out the realtion existing) there is always some relations that exist in loops, but you cant waste all you time finding it.. find it quick, if you can't and if the given choices that looks right are atleast 2 then walk through as said by
Cindy Glass: Best way to figure it out is just to walk through it.
this problem.. algorithm: size=2; val=1; loop 11 -- 0 to 3 loop 21 -- 0 to 3 array --> val++; obviously its a 4 by 4 matrix .. checkout the choices.. there's only one choice.. so just pick it (choice d) )up val--> 1 till (4*4=)16 you should have 16 values, checkout whices have 16 values and drop the other choices. but it will never be this easy always, there will be a tougher conditional statement and values will change in some order.. so find the relation and you will get it.
**assignment** find the missing terms..
a.) 3, 5, 18, 95, 1728, ???
b.) 7, 13, 56, 182, 3192, 33306, ???
[This message has been edited by Subramaniam Venkatesan (edited March 01, 2001).]
Joined: Feb 24, 2001
a.) 3, 5, 18, 95, 1728,
3, 5, 18=(3*(5+1)), 95=(5*(18+1)),1728=(18+(95+1)) and hence you get 164225.
<b.) 7, 13, 56, 182, 3192, 33306, >
the alternate are in series.. 7, 56=(7*(7+1)), 3192=(56*(56+1)).. the other alternate terms have the same relation too.. 13, 182=(13*(13+1)), 33306=(182*(182+1)) hence from the first alt. series, missing term will be (3192*3193) good, I finished the assignment!
[This message has been edited by Subramaniam Venkatesan (edited March 02, 2001).]