initializing array with even numbers

S

Sirius

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.length; j++)
{
Arr2d[j] = j+2;
}
}

but its not quite working out for me....any help is appreciated ty
 
L

Lew

Sirius said:
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.length; j++)
{
Arr2d[j] = j+2;
}
}

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
 
S

Sirius

Lew said:
Sirius said:
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.length; j++)
{
Arr2d[j] = j+2;
}
}

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


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.length; j++)
{
if(j%2 == 0)
{
Arr2d[j] = j+2;
}
else
{
Arr2d[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...
 
D

Daniel Dyer

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.
 
M

Mark Jeffcoat

Sirius said:
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.length; j++)
{
Arr2d[j] = j+2;
}
}

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[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.
 
S

Sirius

Mark said:
Sirius said:
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.length; j++)
{
Arr2d[j] = j+2;
}
}

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[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....
if(j%2 == 0)
{
Arr2d[j] = j+2;
}
else
{
Arr2d[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.
 
R

Richard F.L.R.Snashall

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.length; j++)
{
if(j%2 == 0)
{
Arr2d[j] = j+2;
}
else
{
Arr2d[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...


Instead of the inner 8 lines, maybe:

Arr2d[j] = (i+j)^(i-j);-)
 
P

Patricia Shanahan

Sirius said:
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[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
 
L

Lew

Sirius said:
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.length; j++)
{
Arr2d[j] = j+2;
}
}

Mark Jeffcoat wrote:
There is an obvious logical problem with what you have posted,
though:look at the assignment 'Arr2d[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....
if(j%2 == 0)
{
Arr2d[j] = j+2;
}
else
{
Arr2d[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.


What is the maximum value j can achieve?

- Lew
 
J

Jussi Piitulainen

Patricia said:
Sirius said:
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[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.


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;
}
}
 
K

kelly

Sirius said:
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.length; j++)
{
Arr2d[j] = j+2;
}
}

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
 
P

PofN

kelly said:
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?
 
T

Tom Forsmo

Jussi said:
Patricia said:
You are thinking about the wrong question when you talk about "only even
numbers" and arbitrary functions with even values.

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;
}
}

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
 
T

Tor Iver Wilhelmsen

Sirius said:
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?
 
M

Mark Space

PofN said:
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. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,089
Members
48,773
Latest member
Kaybee

Latest Threads

Top