• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Expalin the answer -- Arrays

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
19. If size = 4, triArray looks like:

int[][] makeArray( int size)
{ int[][] triArray = new int[size] [];
int val=1;
for( int i = 0; i < triArray.length; i++ )
{ triArray[i] = new int[i+1];
for( int j=0; j < triArray[i].length; j++ )
{ triArray[i][j] = val++;
}
}
return triArray;
}

a)
1 2 3 4
5 6 7
8 9
10
b)
1 4 9 16
c)
1 2 3 4
d)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
e)
1
2 3
4 5 6
7 8 9 10
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the right answer is e...
Try and run it but at a first sight e seems to be the right answer...
Val
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you run it
it prints
1
2
3
4
5
6
7
8
9
10
So is both a and e correct?
I am confused
Can someone explain how the code is working?
Thankx
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's not a matter of printing it is a matter of HOW the arrays look like...
the first array has one element: 1
the second array has two elements: 2 3
the third array has three elements: 4 5 6
the fourth array has four elements: 7 8 9 10
so answer e is correct !!
Val
 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Roopa Bagur:
19. If size = 4, triArray looks like:

int[][] makeArray( int size)
{ int[][] triArray = new int[size] [];
int val=1;
for( int i = 0; i < triArray.length; i++ )
{ triArray[i] = new int[i+1];
for( int j=0; j < triArray[i].length; j++ )
{ triArray[i][j] = val++;
}
}
return triArray;
}

a)
1 2 3 4
5 6 7
8 9
10
b)
1 4 9 16
c)
1 2 3 4
d)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
e)
1
2 3
4 5 6
7 8 9 10


 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good job Amit !!
Val
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent everybody thankyou

Originally posted by Roopa Bagur:
19. If size = 4, triArray looks like:

int[][] makeArray( int size)
{ int[][] triArray = new int[size] [];
int val=1;
for( int i = 0; i < triArray.length; i++ )
{ triArray[i] = new int[i+1];
for( int j=0; j < triArray[i].length; j++ )
{ triArray[i][j] = val++;
}
}
return triArray;
}

a)
1 2 3 4
5 6 7
8 9
10
b)
1 4 9 16
c)
1 2 3 4
d)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
e)
1
2 3
4 5 6
7 8 9 10


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic