• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Language Fundamentals

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how many lines of output will be produced?
Answer is 11

Can anybody explain the flow in detail...

Thanks in advance
Sri

( tags added and formatted)
[ October 26, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes 11 lines is correct. 11 = 4 + 2 + 5, does that give you a clue?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sri Sridhar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rangarajan that was a good explanation i got cleared now. Ofcourse i could find the clue earlier but this detailed flow cleared my doubts..thanks barry.
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic