1

My code is intended to take in an array of ints and return an array of ints with a length of twice the original array's length, minus 2.

The returned array should have values 1/3 and 2/3 between any given two values in the original array.

For example input array:

{400, 500, 600}

will return:

{400,433,466,500,533,566,600}

The code I have is as follows:

public static void main(String[] args){
            System.out.println("Problem 9 tests");
            int[] arr7={300, 400, 500};
            System.out.println(highDef(arr7));
            System.out.println(" ");

public static int[] highDef(int[] original) {
        int[] newarr = new int[original.length*3-2];
        newarr[0]=original[0];
        int count=0;
        while (count!=newarr.length) {
                int increment=(original[count+1]-original[count])/3;
                newarr[count+1]=original[count]+increment;
                newarr[count+2]=original[count]+(count*increment);
                count+=1;
        }
        return newarr;
6
  • What does the exception say? Commented Mar 7, 2016 at 22:24
  • while (count!=newarr.length -2 ) add "-2" to the while condition, you can't get newarr[count+1] when count == newarr.length Commented Mar 7, 2016 at 22:26
  • 2
    Look within your loop - imagine when count == newarr.length - 2... but then you're accessing newarr[count+2] in the body of the loop. (I'd strongly advise you to use for loops for this sort of thing, by the way...) Commented Mar 7, 2016 at 22:26
  • error message? please copy paste Commented Mar 7, 2016 at 22:26
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Lab2a.highDef(Lab2a.java:186) at Lab2a.main(Lab2a.java:26) Commented Mar 7, 2016 at 22:27

2 Answers 2

1

Haven't looked good enough the first time :) Here you go:

public static void main(String[] args) {
    System.out.println("Problem 9 tests");
    int[] arr7 = {300, 400, 500};
    System.out.println(Arrays.toString(highDef(arr7)));
    System.out.println(" ");
}

public static int[] highDef(int[] original) {
    int[] newarr = new int[original.length * 3 - 2];
    newarr[original.length * 3 - 3] = original[original.length-1];

    for(int i=0; i<original.length-1; i++){
        newarr[i*3] = original[i];
        int increment = (original[i+1] - original[i])/3;
        newarr[i*3+1] = original[i] + increment;
        newarr[i*3+2] = original[i] + increment*2;
    }

    return newarr;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Still receiving the same out of bounds exception :(
This was very helpful in helping me understand what the exception means, but for some reason adding -3 to the while condition still did not fix the error. I'm not sure why
0

With array out of bound exceptions, try to think of the last case.

In your example:

original.length = 3

newarr.length = (original.length * 3) - 2 = 7

while (count!=newarr.length) // this means: while count does not equal to 7. Therefore *Count* value can go upto 6
// imagine the last case, count = 6
newarr[count+2]=original[count]+(count*increment); // the problem is here
// count(6) + 2 = 8, which is out of bounds

same issue with this line: int increment=(original[count+1]-original[count])/3;

count will go upto 6 but original[count+1] = original[7] which is out of bound as well

Comments

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.