Help with permuted array/do while loop

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1090 Views - Last Post: 29 April 2012 - 08:06 PM Rate Topic: -----

#1 beastinpeace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-April 12

Help with permuted array/do while loop

Posted 28 April 2012 - 07:22 PM

public class RandomPermutation {
   
  int [] numberChoice = new int[10];
  int [] permuted = new int[10];
  
      
public static void main(String[] args) {
   RandomPermutation rp = new RandomPermutation();
   rp.displayPermutedArrays();

    
}


    void displayPermutedArrays(){

for(int i=1;i<=10;i++){
        
 

System.out.println( i + " " + permuted);
        
    }

}
    
    void generatePermutedArray(){
        
        
int sentinel=-1;
int index;


for (int k = 1; k <=10; k++) {
    do{
        index=(int)(Math.random()*10);}
         while(numberChoice[index]=sentinel);

}


        
    }
}





Hi guys, I am new to java, and I am working on a project that wants me to loop an array ten times with the numbers in all different orders. I am not sure how to use this do while loop that needs reference to my arrays. I keep getting an error right here :
while(numberChoice[index]=sentinel);


It says it is compiled with errors, and it will run however this shows up:

1 [I@5ade5cd9
2 [I@5ade5cd9
3 [I@5ade5cd9
4 [I@5ade5cd9
5 [I@5ade5cd9
6 [I@5ade5cd9
7 [I@5ade5cd9
8 [I@5ade5cd9
9 [I@5ade5cd9
10 [I@5ade5cd9

When what I want is something similar to : 1 [2, 3, 7, 1, 10, 8, 9, 5, 4, 6] and so on. I got it working with an ArrayList, but I was told not to use that, and to do it this way. Anyway, I am just not sure what I'm doing wrong, and if someone could guide me with this problem that would be great.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with permuted array/do while loop

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 12:18 AM

In the line you showed causing an error: '=' is the assignment operator while '==' is the comparison operator. I think you meant to use the comparison operator.

It's not clear to me if correcting that will result in the desired program behavior, so come back with an update if you're still not there.
Was This Post Helpful? 0
  • +
  • -

#3 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 02:46 AM

The output

[I@5ade5cd9


Is because that is the memory location your array variable is referencing too.

Need something more like
permuted[i]
, but expect that would also cause errors.

Have a play and repost your code.
Was This Post Helpful? 0
  • +
  • -

#4 beastinpeace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-April 12

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 07:48 AM

Okay, I was getting the error because I wasn't using "==" like mentioned above. However, I am still getting the memory problem(?) with weird jumbled results like I posted earlier.

public class RandomPermutation {
   
  int [] numberChoice = new int[10];
  int [] permuted = new int[10];
  
      
public static void main(String[] args) {
   RandomPermutation rp = new RandomPermutation();
   rp.displayPermutedArrays();
    }
  
    void displayPermutedArrays(){

for(int i=1;i<=10;i++){
System.out.println( i + " " + permuted);
        
    }

}
    
    void generatePermutedArray(){
            
int sentinel=-1;
int numberChoice;

for (int k = 1; k <=10; k++) {
    do{
        numberChoice=(int)(Math.random()*10);}
         while(permuted[numberChoice]==sentinel);

}


        
    }
}


I guess I just don't know which array needs to be accessed for what so it will work
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 08:20 AM

I think to obtain correctly printed results, you'll need to change the display method to:
void displayPermutedArrays(){

    for(int i=1;i<=10;i++){
        System.out.println( i + " " + permuted[i]);

    }

Problems remain:

The permuted array is all zeros, and
The do statement must be modified to prevent numberChoice busting the array limits.

Why is the permuted array filled with zeros? Because that's what it was correctly initialized to as an instance array variable, and it is never changed.

Why is it never changed? Because the generatePermutedArray() method doesn't do as you would like it to.

I don't know if this is allowed, but another approach would be to randomly pick from an array of 10 numbers, 1 through 10, moving the chosen number to the end and then reducing the available set of numbers that can be chosen. pbl uses this approach to randomly deal cards.

So the original set of numbers looks like:

{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

A random number generator is used to choose an index between 0 and 9 (inclusive). Let's say the index 4 is chosen which corresponds to the value 5. The set of choices is then modified to be:

{ 1, 2, 3, 4, 10, 6, 7, 8, 9, 5 }

and the next random number will be chosen between 0 and 8 (inclusive).

Just an idea.

Sets can also be used to determine uniqueness of randomly generated numbers.
Was This Post Helpful? 0
  • +
  • -

#6 beastinpeace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-April 12

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 12:37 PM

Yeah I was getting the zeros. I think I need this:
    
for (int i = 0; i <10; i++) {
permuted[i]=(i+1);


somewhere in my code, but I am not sure where to put it within my methods
Was This Post Helpful? 0
  • +
  • -

#7 beastinpeace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-April 12

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 02:10 PM

public class RandomPermutation {
   
   
   
  int [] numberChoice = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int [] permuted = new int [10];
  
  
  
public static void main(String[] args) {
    
   
   RandomPermutation rp = new RandomPermutation();
   rp.displayPermutedArrays();
   
  
    }
int index;
int i;
  
    void displayPermutedArrays(){

        

         
         
         for(int j=0;j<10;j++){
   
System.out.println(j +" " + index);}
    
   
    
    }

    
    void generatePermutedArray(){
          
            
int sentinel=-1;


for (int k = 1; k<=10; k++) {
    do{
        index=(int)(Math.random()*10);}
         while(numberChoice[index]==sentinel);
        permuted[i] = numberChoice[index];
        numberChoice[index] = sentinel;
}


        
    }
    }


Emailed my professor for some help, and he told me to add some stuff, and I did but now my results are:

0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0

Why am I just getting zeros?
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 02:18 PM

What's the purpose of your do/while loop. sentinel is set to -1, the do loop runs at least once, but will the while statement ever be true?

while( numberChoice[index] == sentinel )

When will the numberChoice[index] be -1?

Think about that, and let us know what your intent was there.

Edit: I just noticed that you never called the method generatePermutedArray() - ?

This post has been edited by GregBrannon: 29 April 2012 - 02:27 PM

Was This Post Helpful? 0
  • +
  • -

#9 beastinpeace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-April 12

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 02:36 PM

Well my professor told me to set the sentinel to -1 because that will help generate random numbers. I think he explained it as if..we assign the index value to -1, it wont be the same the next loop therefor generating a random batch of numbers 1-10. I can't explain it as well as him, but does that make sense? Also, I need to call that method in order for it to be relevant?
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 02:44 PM

If you

1. call generatePermutedArray(), and
2. correct displayPermutedArrays() so that it is actually printing the array,

you'll see that the do/while loop you've written is filling the first item in the permuted array with a random value. Something like:

0 9
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0

To fix displayPermutedArrays():

System.out.println( j +" " + permuted[j] );

Just read your next post:

Yes, you need to call the method for it to be relevant, and no, I'm not sure what your professor is suggesting. Fix the print statement, think a bit more, and we'll fix the displayPermutedArrays() method. Any ideas?

Here's a pseudo code version:

In a loop from 1 to 10 (or zero to 9 with some adjustments):
randomly choose an index 0 to the possible range, inclusive,
assign permuted[k - 1] = numberChoice[index],
swap the numberChoice[] value chosen with the last value in the numberChoice[] array,
decrement the possible range.
Was This Post Helpful? 0
  • +
  • -

#11 beastinpeace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-April 12

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 02:54 PM

Okay, I will try to explain it a little better. He said we want to replace the index value, say the index is 8 and the value is 9. He said that if we replace 9, with -1, that 9 can't be used in that place anymore and it will have to go to another index. He said -1 works because it is outside the bounds of the array so it will not recognize it and replace it with another number. I hope that makes more sense
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 03:01 PM

Yes, I understand, but I wouldn't do it that way. I like pbl's way better.

If you want to do it your prof's way - you may now feel like you have to - then:

in the k loop:
choose a random index
test if numberChoice[index] == sentinel
if no, permuted[k - 1] = numberChoice[index]
numberChoice[index] = sentinel
next k
if yes, return to choose a random index

(I haven't coded that, so it may require adjusting, but that's the general idea.)
Was This Post Helpful? 0
  • +
  • -

#13 beastinpeace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-April 12

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 03:02 PM

Also, when I call my generate method, I get this error:

run:
0 0
1 0
2 0
3 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
4 0
5 0
6 0
7 0
8 0
9 0
at RandomPermutation.generatePermutedArray(RandomPermutation.java:51)
at RandomPermutation.main(RandomPermutation.java:22)
Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 03:26 PM

Doing it your prof's way, you're actually very close. With just one or two minor changes, your generatePermutedArray() method should be:
void generatePermutedArray()
{
    int sentinel=-1;

    for (int k = 1; k<=10; k++)
    {
        do
        {
            index=(int)( Math.random() * 10 );
        }
        while( numberChoice[index] == sentinel );

        permuted[k - 1] = numberChoice[index];
        numberChoice[index] = sentinel;
    }
}

Was This Post Helpful? 0
  • +
  • -

#15 beastinpeace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-April 12

Re: Help with permuted array/do while loop

Posted 29 April 2012 - 04:08 PM

Now instead of 0s I get
list 1
list 2
list 3
list 4
list 5
list 6
list 7
list 8
list 9
list 10

Sigh. This is so aggravating I have no idea what I'm doing wrong. I'm sorry I'm having a hard time getting this resolved.

public class RandomPermutation {
   
   
   
  int [] numberChoice = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int [] permuted = new int [10];
  
  
  int index;
public static void main(String[] args) {
    
   
   RandomPermutation rp = new RandomPermutation();
   rp.displayPermutedArrays();
   rp.generatePermutedArray();
   
  
    }
    void displayPermutedArrays(){
        
 
         for(int j=0;j<numberChoice.length;j++){
             
   
System.out.println(" list " + numberChoice[j]);}
    
   
    
    }

    
    void generatePermutedArray(){
          
            
int sentinel=-1;
int index;

for (int k = 1; k<=10; k++) {
    do{
        index=(int)(Math.random()*10);}
         while(permuted[index]==sentinel);
            numberChoice[k-1] = permuted[index];
            permuted[index] = sentinel;
           
}


        
    
    }
    }


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2