2D-Arrays:Keep columns, while changing all other values

  • Thread starter John Inge =?UTF-8?B?RWxsaW5nc8OmdGVy?=
  • Start date
J

John Inge =?UTF-8?B?RWxsaW5nc8OmdGVy?=

How do I do this??

I have a table [j] that are filled with random integers on each round of
a loop, but for a certain user input I want to keep column[n] while all
other numbers are changed.

I guess this is quite easy, but I just can't seem to figure it out.

Thanks in advance,
John Inge E


If to any help, this is the code for filling the table with integers:
(some norwegian words in there, sorry about that)


for (int column = 0; column < antRader; column++) {
radNr = radNr+1;
System.out.print("\t L" + radNr + " | ");

for (int row = 0; row < (antRader); row++) {
tabell[column][row] = ((int)(Math.random()*(antRandom)+1));
System.out.print(tabell[column][row]+" - ");
}
}
 
D

Dirk Michaelsen

Hi John Inge,
I guess this is quite easy, but I just can't seem to figure it out.

yes it's very easy. Just test the column number before entering the
inner for-loop:

if (column != n) {
// put your inner loop here
}

cu
Dirk
 
R

Roedy Green

tabell[column][row] = ((int)(Math.random()*(antRandom)+1));

there are much better ways of doing random numbers. See
http://mindprod.com/jgloss/pseudorandom.html

Your question amounts to how do you loop through just the elements you
want to change. If you can't solve that, try a loop that goes through
everything, and decided on a case by case basis whether to make the
change. Once you have that code working, your brain might clue into
how to code the efficient version. Just eliminate the wasteful parts
of the loop a bit at a time.
 

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
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top