• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to handle rows/columns in arrays  RSS feed

 
Ashley Kin
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to print a 2-d array of powers, but I'm having trouble getting my head around the idea of columns and rows.
So if I'm correct, is my inner loop assigning values in the array for columns, and my outer for loop is for the rows?
Or should I have a separate array for each row of the table?
Thanks for any help!



is printing:


It should be printing:

[1.0 1.0 1.0 1.0 ]
[2.0 4.0 8.0 16.0 ]
[3.0 9.0 27.0 81.0 ]
[4.0 16.0 64.0 256.0 ]
[5.0 25.0 125.0 625.0 ]
[6.0 36.0 216.0 1296.0 ]
[7.0 49.0 343.0 2401.0 ]
[8.0 64.0 512.0 4096.0 ]
[9.0 81.0 729.0 6561.0 ]
[10.0 100.0 1000.0 10000.0 ]
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the inner loop the j begins at 0.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While Akhilesh has pointed you where the problem lies, you can just add one when you find the pow and keeping the loop and the value of j as is.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to look at the conventions about the Math#pow() method. Particularly what x to the power of 0 is.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!