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/18

#
I want to set the value of the part of the Storage with the number k the value value.
private void EveryPossibility(int[][] givenArray, int[] Storage)
    {
        int value;
        for (int k=0; k<=pow(Number,Number-1); k++)
        {
            Wert =   givenValue[0][k/pow(Number,Number-1)]
                    +givenValue[1][k/pow(Number,Number-2)]
                    +givenValue[2][k/pow(Number,Number-3)]
                    +givenValue[3][k%pow(Number,Number-3)];
            Storage[k] = value; 
        }
    }
The error is in the tenth lane, but I don't know why this shouldn't work.
danpost danpost

2017/7/19

#
All I can tell from what is given is: (a) the 'value' int is not initialized (cause of error); (b) the value of 'Wert' will be whatever value it receives on the last iteration of the loop; (c) the 'Storage' int array is lost after the method is done executing; and (d) the 'givenArray' array is not utilized within the method; Since you did not give any background info, it is difficult to determine what 'value' should be set to and where it should be set.
Unknown6415 Unknown6415

2017/7/20

#
ok sry, but would you tell me how a code for the following problem would look like? I want to set the value of Every as shown in the code.
 private void Every(int[][] Value, int[][] Every, int Number)
    {
public static int[][] Value =
    {
        {1,5,5,5},
        {2,1,5,3},
        {3,1,5,5},
        {4,3,3,2}
    };
public static int[][] Every;
public static int Number=4;
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];
        }
}
I really don't understand the error in the line "Every= k/64;". This is driving me crazy!
Unknown6415 Unknown6415

2017/7/20

#
Ok I just sent the whole code.
import greenfoot.*;
import java.awt.Color;

public class One extends Actor
{
    public static int Number=4;
    public static int[] Task;
    public static int[] Storage;
    public static int[][] Every;
    public static int[][] Value =
    {
        {1,5,5,5},
        {2,1,5,3},
        {3,1,5,5},
        {4,3,3,2}
    };
    
    private int Pow(int a, int b)
    {
        int result=1;
        for (int z=1; z<=b; z++)
        {result=result*a;}
        return result;
    }
    
    private int Fac(int a)
    {
        int result = 1;
        for (int i = 1; i <= a; i++) 
        {
            result *= i;
        }
        return result;
    }

    private void Every(int[][] Value, int[][] Every, int Number)
    {
        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];
        }
    }
    
    private void Solution(int[][] Every, int Number)
    {
        int[] Solution={0,0,0,0};
        int SolutionValue=0;
        for (int m=0; m<=Every .length-1; m++)
        {
            int Zahl=Every[m][4];
            if (Zahl>SolutionValue) 
            {  
                Solution[0]=Every[m][0];
                Solution[1]=Every[m][1]; 
                Solution[2]=Every[m][2]; 
                Solution[3]=Every[m][3]; 
                SolutionValue=Every[m][4];  }
        }
        for (int o=0; o<=Number-1; o++)
        {
            Task[o]=Solution[o];
        }
        
    }
    
     public void act() 
    {
        String persons = "Persons";
        for (int i=1; i<=Number; i++)
        {
            persons = persons+System.getProperty("line.separator")+i;
        }
        
        int one=0;
        if (one==0) 
        {
            eins++;
            Every(Value, Every, Number);
            Solution(Every, Number);
        }
        
        setImage(new GreenfootImage( persons
        , 36, Color.BLACK, new Color(0, 0, 0, 0)));
    }    
}
Super_Hippo Super_Hippo

2017/7/20

#
What is the purpose of this class? There are so many weird things like lines 78 and 79 for example. I also don't get why you have those public static ints and always try to use them as parameters in the methods. They are saved in the class, you can just use them. You should also note that if you have
private void Every(int[][] Value, int[][] Every, int Number)
the Value, Every and Number you use in the method are the ones which are passed to the method and NOT the public static ints. And obviously there is the problem that most of the int arrays are null.
danpost danpost

2017/7/20

#
I believe there are other problems besides what Hippo noted. The limit of the loop in the Every method, for one (it is 4 to the 4th power, or 256). The fact that 'eins' on line 81 seems to come from nowhere (not declared anywhere), for another. Also, the java.lang.Math package has a 'pow' method for computing a number to a power. You did not have to create your own (however, I do not think you need that function at all as I feel that you meant Pow(Number, 2) -- not Pow(Number, Number) on line 38 (which could be written as 'Number*Number').
Unknown6415 Unknown6415

2017/7/20

#
line 81 should be one++ it was just the fastest solution so it does it only once the void every should set the values for the array every "the Value, Every and Number you use in the method are the ones which are passed to the method and NOT the public static ints." yes, but I wrote them in the brackets below in line 82 "And obviously there is the problem that most of the int arrays are null." what do you mean by that? how should I change it? "The limit of the loop in the Every method, for one (it is 4 to the 4th power, or 256)." Yes I know, that was on purpose And I meant "Pow(Number, Number)"
Unknown6415 Unknown6415

2017/7/20

#
"And obviously there is the problem that most of the int arrays are null." I thought, if the brackets are empty like in line 9, I can set the values later, so what do you mean by "null"?
danpost danpost

2017/7/21

#
Unknown6415 wrote...
"And obviously there is the problem that most of the int arrays are null." I thought, if the brackets are empty like in line 9, I can set the values later, so what do you mean by "null"?
Arrays are Object type objects. All object references are null until you assign something to them. That is, declaring an array as in line 9 only shows your intent on creating an array -- it does not create it. It is like saying:
Actor actor;
You cannot set the location of 'actor' until you assign some Actor object to 'actor'. Similarly, you cannot assign values to the elements of 'Every' until you create an array for 'Every' to reference. Remove line 78, change line 79 to:
if (Every == null)
(so it only does it once) and replace line 81 with this:
Every = new int[Pow(Number, Number)][5];
Now you can change line 38 to:
for (int k=0; k<=Every.length; k++)
Then, remove all parameters from the Every and Solution methods (lines 36 and 48) and the arguments in the calls to those methods (lines 82 and 83).
Super_Hippo Super_Hippo

2017/7/21

#
Unknown6415 wrote...
line 81 should be one++ it was just the fastest solution so it does it only once
That's not true because 'one' will always be 0 when it is checked even if you increase it there.
yes, but I wrote them in the brackets below in line 82
True, but this way, you don't pass the variables, you pass the values of the variables. So when you change Every in the method, you will only change the one that you passed (which is removed when the method is finished) and not the one saved in the class.
Unknown6415 Unknown6415

2017/7/21

#
Now I get an error in line 44
Every[k][4]= Value[0][k/64]+Value[1][k/16]+Value[2][k/4]+Value[3][k%4];
Super_Hippo Super_Hippo

2017/7/21

#
What error is it? Did you change what danpost suggested? In case it is an array out of bounds: If Number is 4, then k is 4^4=256 and k/4 is 64 meaning that it tries to find the 64th index in the array which it can't. Which leads back to the question what you are trying to do there.
Unknown6415 Unknown6415

2017/7/21

#
I did everything danpost said how do I see what error it is? The Terminal Windows says: java.lang.ArrayIndexOutOfBoundsException: 4 If the error is because of the k/64, why isn't the error in the 40. lane?
danpost danpost

2017/7/21

#
You have your array indices backwards. It should be:
Every[k][4]= Value[k/64][0]+Value[k/16][1]+Value[k/4][2]+Value[k%4][3];
Super_Hippo Super_Hippo

2017/7/21

#
Unknown6415 wrote...
If the error is because of the k/64, why isn't the error in the 40. lane?
Line 40:
Every[k][0]= k/64;
It sets one "cell" in the array to k/64, so for k=256 that's a 64 in the (256,0) cell. You can put in any int number not depending on the index. The k/64 already causes the error (256/64=4 > 3), the k/4 is even worse (256/4=64 > 3). The 3 comes from the size of the array - 1. The size of Value is 4x4, you can't change a cell with an index higher than 3 (in "row" or "column"), because it doesn't exist. The k/64 doesn't "cause the error", trying to use it as an index does because it exceeds the size. (Hopefully you understand what I mean.)
There are more replies on the next page.
1
2