0

I have a question on the method and process of how to look at these generated arrays. Basically I want to create an array of [a,b,c,(a+b+c)] and as well as a second array of [d,e,f,(d+e+f)] and if the third element in array1 and array2 are the same, display the arrays to strings.

int num = 10;
for(int a = 0; a < num; a++){
   for(int b = 0; b < num; b++){
      for(int c = 0; c < num; c++){
         if(a<=b && b<=c){
           arrayOne[0] = a;
           arrayOne[1] = b;
           arrayOne[2] = c;
           arrayOne[3] = (a+b+c);
         }
      }
   }
}

for(int d = 0; d < num; e++){
   for(int e = 0; e < num; e++){
      for(int f = 0; f < num; f++){
         if(d<=e && e<=f){
           arrayTwo[0] = d;
           arrayTwo[1] = e;
           arrayTwo[2] = f;
           arrayTwo[3] = (f -(d+e));
         }
      }
   }
}

as you can see I am beyond stump.I am not quite sure where i can get each iteration of the arrays and compare the values by matching the sums in each array and as well as displaying the respective array they are in. Thank you all in advanced.

4
  • Can you give an example of exactly what you want the arrays to look like? Right now it looks like you have two arrays of length 4 and you're just repeatedly overwriting the values without ever using them. Commented Oct 5, 2013 at 5:11
  • 1
    right now you are looping over your a,b,c independently, then looping over d,e,f. By the time the first nested for finishes you have [num, num, num, 3*num] as your array. In principle, every iteration of the second loop generates the same data as the corresponding iteration of the first loops - and the nested loop will exit with the same value. Not sure what you are trying to achieve? Commented Oct 5, 2013 at 5:12
  • i am trying to create two arrays at their own different run times and in the end I want to scan all the arrays I generated by looking if and only if both arrayOne and arrayTwo have the same sum in the third element of each array and then display the respected arrays Commented Oct 5, 2013 at 5:30
  • You need to store the first set of arrays somewhere. I suggest storing the arrays with the same "third element" (I guess you actually mean the element with index 3) together. So you could for example use an array of lists. Commented Oct 5, 2013 at 5:48

2 Answers 2

1

If I understand your question correctly if a=1, b=3, c=4 and d=2, e=3, f=3 you'd like to print something along the lines of 1 + 3 + 4 = 8 = 2 + 3 + 3. First, what you're doing right now is creating two arrays like Floris described in the comment. What you want to do is store all the values in one array of arrays, as follows:

int max; \\ To determine the value of max see the edit below.
int array[][] = new int[max][num];
int index = 0;
for (int a=0; a < num; a++) {
    for (int b=a; b < num; b++) {
        for (int c=b; c < num; c++) {
            array[index][0] = a;
            array[index][1] = b;
            array[index][2] = c;
            array[index][3] = a + b + c;
            index++;
        }
    }
}

for (int i = 0; i < max; i++) {
    for (int j = i; j < max; j++) {
        if (array[i][3] == array[j][3]) {
            string outString = array[i][0] + " + " + array[i][1] + " + " + array[i][2] + " = " + array[i][3] + " = " + array[j][0] + " + " + array[j][1] + " + " + array[i][2];
            System.out.println(outString);
        }
    }
}

You can see that I improved performance by starting b from a and c from b since you are throw out all the values where b < a or c < b. This also should eliminate the need for your if statement (I say should only because I haven't tested this). I needed to use an independent index due to the complexities of the triple nested loop.

Edit 2: Ignore me. I did the combinatorics wrong. Let An,k be the number of unordered sets of length k having elements in [n] (this will achieve what you desire). Then An,k = An-1,k + An,k-1. We know that An,1 = n (since the values are 0, 1, 2, 3, 4, ..., n), and A1,n = 1 (since the only value can be 11111...1 n times). In this case we are interested in n= num and k = 3 , so plugging in the values we get

A_num,3 = A_num-1,3 + A_num,2

Apply the equation recursively until you come to an answer. For example, if num is 5:

A_5,3 = A_4,3 + A_5,2
      = A_3,3 + A_4,2 + A_4,2 + A_5,1
      = A_3,3 + 2(A_4,2) + 5
      = A_2,3 + A_3,2 + 2(A_3,2) + 2(A_4,1) + 5
      = A_2,3 + 3(A_3,2) + 2(4) + 5
      = A_1,3 + A_2,2 + 3(A_2,2) + 3(A_3,1) + 2(4) + 5
      = 1 + 4(A_2,2) + 3(3) + 2(4) + 5
      = 1 + 4(A_1,2) + 4(A_2,1) + 3(3) + 2(4) + 5
      = 1 + 4(1) + 4(2) + 3(3) + 2(4) + 5
      = 5(1) + 4(2) + 3(3) + 2(4) + 5

It looks like this may simplify to (num + (num - 1)(2) + (num - 2)(3) + ... + (2)(num - 1) + num) which is binomial(num, num) but I haven't done the work to say for sure.

Sign up to request clarification or add additional context in comments.

3 Comments

I know the string concatenation is kind of messy. You can change that to something else if you prefer.
The if condition in the second for loop should contain == and not = I guess, though never tested it, but still this is assignment, when you need to compare two values :-) +1 for the rest, though the question is still a bit unclear to me.
Thanks @nIcEcOw! I made changes according to your correction :)
1
int givenNumber = 10;
int []arrayOne = new int [4]; 
int []arrayTwo = new int [4];
int count = 0;

for ( int i = 0; i < givenNumber; i ++)
{    
    for ( int x = 0; x < givenNumber; x ++ )
    {
        for ( int a = 0; a < givenNumber; a++ ){
            arrayOne[0] = (int)(a * java.lang.Math.random() + x);
            arrayOne[1] = (int)(a * java.lang.Math.random() + x);
            arrayOne[2] = (int)(a * java.lang.Math.random() + x);
            arrayOne[3] = (int)(arrayOne[0]+arrayOne[1]+arrayOne[2]);
        }

        for ( int b = 0; b < givenNumber; b++ ){
            arrayTwo[0] = (int)(b * java.lang.Math.random() + x);
            arrayTwo[1] = (int)(b * java.lang.Math.random() + x);
            arrayTwo[2] = (int)(b * java.lang.Math.random() + x);
            arrayTwo[3] = (int)(arrayTwo[0]+arrayTwo[1]+arrayTwo[2]);
        }


        if (arrayOne[3] == arrayTwo[3])
        {
            for ( int a = 0; a < 2; a++ )
            {
                System.out.print(arrayOne[a] + " + ");
            }   System.out.print(arrayOne[2] + " = " + arrayOne[3] + " = ");

            for ( int a = 0; a < 2; a++ )
            {
                System.out.print(arrayTwo[a] + " + ");
            }   System.out.print(arrayTwo[2]);      

            System.out.println("\n");
            count += 1;
        }   
    }

}
        if (count == 0)
            System.out.println(
                "\nOops! you dont have a match...\n" +
                    "Please try running the program again.\n");

1 Comment

Ok, this seems to work but i would like to change the math operations. arrayOne[3] = (long)Math.pow(a,5)+(long)Math.pow(b,5)+(long)Math.pow(c,5) arrayTwo[3] = (long) Math.pow(f, 5) - ( (long) Math.pow(d, 5) + (long) Math.pow(e, 5) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.