This site requires JavaScript, please enable it in your browser!
Greenfoot back
Unknown6415
Unknown6415 wrote ...

2017/7/18

How do i set the value of a number in an array?

1
2
Unknown6415 Unknown6415

2017/7/21

#
"a 64 in the (256,0) cell" It should set it to 4 because of 256/64=4 actually I don't get it... Ohh I got it now, I made a mistake, I correct it and answer later
Unknown6415 Unknown6415

2017/7/21

#
Ok I changed it to:
Every[k][0]= (k%256)/64;
            Every[k][1]= (k%64)/16;
            Every[k][2]= (k%16)/4;
            Every[k][3]= k%4;
            Every[k][4]= Value[0][(k%256)/64]+Value[1][(k%64)/16]+Value[2][(k%16)/4]+Value[3][k%4];
But now there is an error in
Every[k][0]= (k%256)/64;
"java.lang.ArrayIndexOutOfBoundsException:4"
Super_Hippo Super_Hippo

2017/7/21

#
Add this line right in front of it and report back what it prints out:
System.out.println("k="+k+", size="+Every.length+", (k%256)/64="+((k%256)/64)));
Maybe you already see what happens yourself then.
danpost danpost

2017/7/21

#
Line 38 was (re)given incorrectly. It should be:
for (int k=0; k<Every.length; k++)
(without the equal sign in the middle).
Unknown6415 Unknown6415

2017/7/21

#
Oh yeah thank you. Now the next error is in line 65
Task[o]=Solution[o];
java.lang.NullPointerException Thank you very much, I could never solve it by myself
danpost danpost

2017/7/21

#
I think you can just replace lines 63 through 67 with this:
Task = Solution;
Unknown6415 Unknown6415

2017/7/21

#
It worked, thank you very much
Unknown6415 Unknown6415

2017/7/21

#
Ok I generalized it, so the array Value can have as many numbers as I want
import greenfoot.*;
import java.awt.Color;

public class AnzeigePersonen extends Actor
{
    public static int Number=7;
    public static int[] Task;
    public static int[][] Every;
    public static int[][] Value =
    {
        {6,6,1,1,2,4,8},
        {1,9,7,5,9,8,8},
        {2,10,1,1,7,5,10},
        {4,3,3,6,9,4,3},
        {2,8,10,1,8,2,3},
        {4,1,2,2,3,8,3},
        {1,5,4,7,9,9,9}
    };
    /*
    {
        {6,6,1,1,2,4,8,9,10},
        {1,9,7,5,9,8,8,7,1},
        {2,10,1,1,7,5,10,2,5},
        {4,3,3,6,9,4,3,7,4},
        {2,8,10,1,8,2,3,9,8},
        {4,1,2,2,3,8,3,5,9},
        {1,5,4,7,9,9,9,8,2},
        {7,1,6,5,9,9,2,2,10},
        {9,7,9,8,7,10,4,2,7}
    };
    */
    
    private int Pow(int a, int b)
    {
        int result=1;
        for (int z=1; z<=b; z++)
        {result=result*a;}
        return result;
    }
    
    private int Fak(int a)
    {
        int result = 1;
        for (int i = 1; i <= a; i++) 
        {
            result *= i;
        }
        return result;
    }

    private void Every()
    {
        for (int k=0; k<Every .length; k++)
        {
            for (int b=0; b<Number;b++)
            {
                if (b!=Number-1)
                {Every[k][b]= (k%Pow(Number,Number-b))/Pow(Number,Number-1-b);}
                else {Every[k][b]= k%Number;}
            }
            for (int c=0; c<Number;c++)
            {
                Every[k][Number]+=Value[c][Every[k][c]];
            }
        }
    }
    
    private void Solution()
    {
        int[] Solution=new int[Number];
        int SolutionValue=0;
        for (int m=0; m<=Every .length-1; m++)
        {
            int counts=1;
            int Zahl=Every[m][Number];
            for (int r=0; r<Number; r++)
            {
                for (int s=0; s<Number; s++)
                {
                    if (r!=s)
                    {
                        if (Every[m][r]==Every[m][s])
                        {counts=0;}
                    }
                }
            }
            if (Zahl>SolutionValue&&counts==1) 
            {  
                for (int w=0; w<Number; w++)
                {
                    Solution[w]=Every[m][w];
                    SolutionValue=Every[m][w+1];  
                }
            }
        }
        Task = Solution;
        
    }
    
     public void act() 
    {
        String persons = "Persons";
        for (int i=1; i<=Number; i++)
        {
            persons = persons+System.getProperty("line.separator")+i;
        }
        
        if ( Every == null)
        {
            Every = new int[Pow(Number, Number)][Number+1];
            Every();
            Solution();
        }
        
        setImage(new GreenfootImage(  persons
        , 36, Color.BLACK, new Color(0, 0, 0, 0)));
    }    
}
I tried it with 7x7 numbers and it worked, but with 9x9 it gave me many errors: java.lang.ArrayIndexOutOfBoundsException: 256 line 48 java.lang.OutOfMemoryError: Java heap space line 100 java.lang.OutOfMemoryError: Java heap space line 110 So are there simply too many integers?
Super_Hippo Super_Hippo

2017/7/21

#
The out of index is not in 48 in the current version. Where is it? 9^9 is like 387 million. That's what causing the java heap space error... I am still pretty sure that there is a more simple way of doing what you are trying to do, but I don't really understand your goal.
Unknown6415 Unknown6415

2017/7/21

#
yes, I am pretty sure there is a way more easy way. My version has 9^9 possibilities, but only 9! are even a real possibility. I want to have every combination of the numbers in the array Every, while two numbers from the same "column" are not allowed, but I didn't know how to do that, so I did every possibility and then just remove the ones with numbers from the same column. "The out of index is not in 48 in the current version. Where is it?" I don't understand that, I just wrote what was written on the error window
Super_Hippo Super_Hippo

2017/7/22

#
Line 48 in your last post was:
return result;
No chance there is an out of bounds. What is a combination? I mean you have this
Every[k][b]= (k%Pow(Number,Number-b))/Pow(Number,Number-1-b);}
but what is it representing?
Unknown6415 Unknown6415

2017/7/22

#
there are these integers
{6,6,1,1,2,4,8},
        {1,9,7,5,9,8,8},
        {2,10,1,1,7,5,10},
        {4,3,3,6,9,4,3},
        {2,8,10,1,8,2,3},
        {4,1,2,2,3,8,3},
        {1,5,4,7,9,9,9}
one possible combination would be the first integer from the first row, the second from the second row... the seventh integer from the seventh row not allowed is: the first integer from the first row and the first integer from the second row... so always an integer from every row, but never a "used position"
for (int k=0; k<=Pow(Number, Number); k++)
        {
            Every[k][0]= k/64;
            Every[k][1]= k/16;
            Every[k][2]= k/4;
            Every[k][3]= k%4;
            Every[k][4]= Value[0][k/64]+Value[1][k/16]+Value[2][k/4]+Value[3][k%4];
        }
this worked just for Number=4, so only 4 rows of integers this is just the same like before but generalized:
for (int k=0; k<Every .length; k++)
        {
            for (int b=0; b<Number;b++)
            {
                if (b!=Number-1)
                {Every[k][b]= (k%Pow(Number,Number-b))/Pow(Number,Number-1-b);}
                else {Every[k][b]= k%Number;}
            }
            for (int c=0; c<Number;c++)
            {
                Every[k][Number]+=Value[c][Every[k][c]];
            }
        }
danpost danpost

2017/7/22

#
So, basically, what you want to have is an array of all combination of indices for a given two dimensional array where one set of indices would indicate the elements with respect to the positions of the indices. For example, for a 5x5 array, you would need all the different orderings of the values in the set {0, 1, 2, 3, 4}. I came up with the following algorithm:
/** fields */
int[][] combos; // your 'Every'
int n = 4; // your 'Number'

/** main code */
int[] vals = new int[n]; // create an array for the values
for (int i=0; i<n; i++) vals[i] = i; // load 'vals' array
combos = getCombos(vals); // compute combinations

/** method to compute factorials */
private int fac(int num)
{
    int m = 1;
    for (int i=0; i<num; i++) m *= (i+1);
    return m;
}

/** method to compute combinations */
private int[][] getCombos(int[] set)
{
    if (set.length == 1) return new int[][] { set }; // a single value has only one combination
    int[][] arr = new int[fac(set.length)][set.length]; // for combinations
    int num = 0;
    for (int i=0; i<set.length; i++) // for each number in set
    {
        int[] set2 = new int[set.length-1]; // creates an array for a set without the current number
        for (int j=0; j<set.length; j++) // loads set2
        {
            if (j == i) continue; // to not include current number
            set2[j-(j>i ? 1 : 0)] = set[j];
        }
        for (int[] parts : getCombos(set2)) // for each combination in smaller set
        {
            arr[num][0] = set[i]; // begin with current number
            for (int k=0; k<parts.length; k++) arr[num][k+1] = parts[k]; // add one of the smaller combinations
            num++; // ready for next combination
        }
    }
    return arr; // return the computed combinations
}
Please note that it is not advisable for a novice programmer to have a method call itself as the 'getCombos' method does. One must be very careful that it is done in such a way that all calls will be returned from at some point. Otherwise, you could have the method continuously calling itself without end. I surprised myself when this ran perfectly on my first attempt. Wow -- not expected.
Unknown6415 Unknown6415

2017/7/22

#
Ok your solution is probably right. Also I need to know the value of each combination, because I want to get the combination the the biggest value (the value is the sum of all the integers of the combination) And has every combination the length of Number? So if the Every array has 10 rows, the combinations must have 10 numbers Well you seem to be very skilled
danpost danpost

2017/7/23

#
Something like this should have 'hi' end up with the highest sum and 'cbo' be the index of the combination that produced it:
int hi = 0;
int cbo = 0;
for (int i=0; i<combos.length; i++)
{
    int sum = 0;
    for (int j=0; j<combos[i].length; j++) sum += Value[combos[i][j]][j];
    if (sum > hi)
    {
        hi = sum;
        cbo = i;
    }
}
You need to login to post a reply.
1
2