Java Forum / General / October 2006
initializing array with even numbers
Sirius - 15 Oct 2006 23:09 GMT i am trying to do this for last 2 hrs... i need to use nestd for loops to populate a 5x3 array with only even numbers... like 2,4,6,8,....30.
what i got so far is for (int i=0; i<Arr2d.length; i++) { for (int j=0; j<Arr2d[i].length; j++) { Arr2d[i][j] = j+2; } }
but its not quite working out for me....any help is appreciated ty
Lew - 16 Oct 2006 00:39 GMT > i am trying to do this for last 2 hrs... i need to use nestd for loops > to populate a 5x3 array with only even numbers... like 2,4,6,8,....30. [quoted text clipped - 9 lines] > > but its not quite working out for me....any help is appreciated ty The loop has j attain odd values. Odd number + 2 will always be odd.
Is j in the semantic space of a value generator or the semantic space of a loop index? Don't mix semantic spaces.
Must you use consecutive even numbers, random even numbers, only one even number, every third even number, ...?
- Lew
Sirius - 16 Oct 2006 00:52 GMT > > i am trying to do this for last 2 hrs... i need to use nestd for loops > > to populate a 5x3 array with only even numbers... like 2,4,6,8,....30. [quoted text clipped - 19 lines] > > - Lew yeah only consecutive even numbers from 2 to 30. I did some changes to my for loop but that's still not it. this is thte new one: for (int j=0; j<Arr2d[i].length; j++) { if(j%2 == 0) { Arr2d[i][j] = j+2; } else { Arr2d[i][j] = j+3; } }
i tried to make it so if j is even it will add 2 to make it even or if j is odd it will add 3 to make it even again since i only need even numbers...
Daniel Dyer - 16 Oct 2006 01:06 GMT > i tried to make it so if j is even it will add 2 to make it even or if > j is odd it will add 3 to make it even again since i only need even > numbers... This isn't so much a programming problem as an arithmetic problem. You can't make your sequence of even numbers increase twice as fast as the loop counter (j) using addition. Your actual formula for mapping the values of i and j to even numbers needs something other than a '+'.
Dan.
 Signature Daniel Dyer http://www.uncommons.org
Richard F.L.R.Snashall - 16 Oct 2006 01:26 GMT > yeah only consecutive even numbers from 2 to 30. I did some changes to > my for loop but that's still not it. this is thte new one: [quoted text clipped - 13 lines] > j is odd it will add 3 to make it even again since i only need even > numbers... Instead of the inner 8 lines, maybe:
Arr2d[i][j] = (i+j)^(i-j);-)
Mark Jeffcoat - 16 Oct 2006 01:08 GMT > i am trying to do this for last 2 hrs... i need to use nestd for loops > to populate a 5x3 array with only even numbers... like 2,4,6,8,....30. [quoted text clipped - 9 lines] > > but its not quite working out for me....any help is appreciated ty You've trimmed off the part of the program where Arr2d (not a idiomatic Java name, by the way; variables begin with lowercase letters) is defined and initialized -- for best results, post a complete but minimal snippet of code.
There is an obvious logical problem with what you have posted, though:look at the assignment 'Arr2d[i][j] = j+2'. On the first pass, j is 0, so j+2 = 2. That's fine. On the second trip through the loop, j=1. Is j+2 even?
Also, think about what that value should be when i > 0.
 Signature Mark Jeffcoat Austin, TX
Sirius - 16 Oct 2006 01:15 GMT > > i am trying to do this for last 2 hrs... i need to use nestd for loops > > to populate a 5x3 array with only even numbers... like 2,4,6,8,....30. [quoted text clipped - 25 lines] > Mark Jeffcoat > Austin, TX yeah i did notice that Mark, and that's why i added this litttle block of code but that still didnt overcome the problem.... if(j%2 == 0) { Arr2d[i][j] = j+2; } else { Arr2d[i][j] = j+3; }
i get the first two numbers of the sequence (2 and 4.....since when j=0, 0+2=2 and when j=1, 1+3=4) but then it just goes any repeats it.
Lew - 16 Oct 2006 02:11 GMT >>> i am trying to do this for last 2 hrs... i need to use nestd for loops >>> to populate a 5x3 array with only even numbers... like 2,4,6,8,....30. [quoted text clipped - 7 lines] >>> } >>> }
> There is an obvious logical problem with what you have posted, >> though:look at the assignment 'Arr2d[i][j] = j+2'. On the first >> pass, j is 0, so j+2 = 2. That's fine. On the second trip through >> the loop, j=1. Is j+2 even? >> >> Also, think about what that value should be when i > 0.
> yeah i did notice that Mark, and that's why i added this litttle block > of code but that still didnt overcome the problem.... [quoted text clipped - 9 lines] > i get the first two numbers of the sequence (2 and 4.....since when > j=0, 0+2=2 and when j=1, 1+3=4) but then it just goes any repeats it.
>> Also, think about what that value should be when i > 0. What is the maximum value j can achieve?
- Lew
Patricia Shanahan - 16 Oct 2006 01:53 GMT > i am trying to do this for last 2 hrs... i need to use nestd for loops > to populate a 5x3 array with only even numbers... like 2,4,6,8,....30. You are thinking about the wrong question when you talk about "only even numbers" and arbitrary functions with even values.
Instead, you need to decide what value you want at Arr2d[i][j], as a function of i and j. Try doing it by hand - draw the array you want, with the values you want, and look for the relationships.
Patricia
Jussi Piitulainen - 16 Oct 2006 08:26 GMT >> i am trying to do this for last 2 hrs... i need to use nestd for loops >> to populate a 5x3 array with only even numbers... like 2,4,6,8,....30. [quoted text clipped - 5 lines] > function of i and j. Try doing it by hand - draw the array you want, > with the values you want, and look for the relationships. We could fill an array, even a ragged one, with an arbitrary sequence quite simply, by generating the sequence separately from the indices:
int t = first value; for (int j = 0 ; j < a.length ; ++ j) { for (int k = 0 ; k < a[j].length ; ++ k) { a[j][k] = t; t = next value; } }
Tom Forsmo - 17 Oct 2006 10:21 GMT >>> i am trying to do this for last 2 hrs... i need to use nestd for loops >>> to populate a 5x3 array with only even numbers... like 2,4,6,8,....30. [quoted text clipped - 8 lines] > } > } Sirius, what Jussi describes here is the simplest solution, not the most fancy, comprehensive or solution. I think there is a lesson here for you, which is one of the most important lessons you will ever learn when programming. Keeping it as simple as possible will save you a lot of headache with unmanageable code or a lot of time chasing ugly bugs.
The major point to take home with you here is that even though a solution might seem simple enough, you have to assess the cost of using a slightly, or much, more complicated solution, throughout the application. Your original problem is just a simple problem, but you ran in to a problem which was hard to solve. And if you have enough of them in a complete system it all adds up in maintenance/complexity cost for the project, meaning that you might end up spending f.ex 30% of your coding/maintenance time chasing bugs or maintaining code that you could have spent on developing new features or programs.
Programming is 50% about creating maintainable code, and this you do by selecting the simplest solutions for the problem (which you can later extend), choosing the correct/simplest design and documenting your code well enough (I.e. document code by writing simple code with short and concise method descriptions, and by adding code comments only for those statements where the functionality is not obvious.)
I hope this advice helps you.
tom
kelly - 16 Oct 2006 11:39 GMT > i am trying to do this for last 2 hrs... i need to use nestd for loops > to populate a 5x3 array with only even numbers... like 2,4,6,8,....30. [quoted text clipped - 9 lines] > > but its not quite working out for me....any help is appreciated ty Hi Sirius,
public class ArrayWithEvenNo {
/** * @param args */ public static void main(String[] args) { int[][] array = new int[5][3]; int evenNo = 0;
for(int rowIndex=0; rowIndex<5; rowIndex++){ for(int columnIndex=0; columnIndex<3; columnIndex++){ array[rowIndex][columnIndex] = (evenNo += 2); System.out.println(array[rowIndex][columnIndex]); } } } }
cheers,..
Kalyani Alshi
PofN - 16 Oct 2006 12:32 GMT > public class ArrayWithEvenNo { Would you fuckwit please stop doing other fuckwit's homework? Do you want to work with programmers who know nothing but got their credentials because Kalyani Alshi did their homework?
Mark Space - 18 Oct 2006 19:08 GMT >> public class ArrayWithEvenNo { > Do you > want to work with programmers who know nothing but got their > credentials because Kalyani Alshi did their homework? No, but I don't mind the lack of competition either. ;-)
Tor Iver Wilhelmsen - 17 Oct 2006 16:58 GMT > but its not quite working out for me....any help is appreciated ty Well, there are 15 numbers to assign. If you let a variable count from 1 to 15, what do you get when you take those values times 2?
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|