Sri Sridhar

Greenhorn

Posts: 2

Barry Gaunt

Ranch Hand

Posts: 7729

posted 12 years ago

Yes 11 lines is correct. 11 = 4 + 2 + 5, does that give you a clue?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch

Getting someone to think and try something out is much more useful than just telling them the answer.

rengarajan vaikuntam

Ranch Hand

Posts: 37

posted 12 years ago

Sri

Its like this

Look at the condition of the first for loop

i<x.length

x.length is the length of the x array which is clearly declared in the statement

int [][][] x=new int[3][][];

so x.length is 3, ok, so the first for loop will run for 3 times for values of i=0, i=1, i=2 ok.

Now the next for loop

for(j=0;j<x[i].length;j++)

here the condition is j shoulbe lessthan x[i].length,so

when i is zero, the condition is j<x[0].length,ok

Look at the line 5 from ur code it says

x[0]=new int [4][];

so x[0].length is 4, so the condition is j<4;

so the inner for loop will run for 4 times for j values of j=0, j=1, j=2 & j=3.now the value of i is zero.Now the sout will print 4 times ok.

Now when i=1,

the condition become j<x[1].length ie j<2 now the inner loop will run for 2 times for j values of j=0 and j=1.Now the sout will print 2 times.

Now i=2,

condition j<x[2].length ie j<5 so now the inner j loop will run for 5 times.now the sout will print 5 times.

So totally

when i=0; it prints 4 times

when i=1; it prints 2 times

when i=2; it prints 5 times

So the answer is (5+2+4) 11 times.

Hope this helps you.

Its like this

Look at the condition of the first for loop

i<x.length

x.length is the length of the x array which is clearly declared in the statement

int [][][] x=new int[3][][];

so x.length is 3, ok, so the first for loop will run for 3 times for values of i=0, i=1, i=2 ok.

Now the next for loop

for(j=0;j<x[i].length;j++)

here the condition is j shoulbe lessthan x[i].length,so

when i is zero, the condition is j<x[0].length,ok

Look at the line 5 from ur code it says

x[0]=new int [4][];

so x[0].length is 4, so the condition is j<4;

so the inner for loop will run for 4 times for j values of j=0, j=1, j=2 & j=3.now the value of i is zero.Now the sout will print 4 times ok.

Now when i=1,

the condition become j<x[1].length ie j<2 now the inner loop will run for 2 times for j values of j=0 and j=1.Now the sout will print 2 times.

Now i=2,

condition j<x[2].length ie j<5 so now the inner j loop will run for 5 times.now the sout will print 5 times.

So totally

when i=0; it prints 4 times

when i=1; it prints 2 times

when i=2; it prints 5 times

So the answer is (5+2+4) 11 times.

Hope this helps you.