-3

I am in the process of writing code to build an algorithm for the JPEG predictive rule #5 which states:

I'[i,j] = I[i, j-1] + I[[i-1, j] + I[i-1, j-1]]/2

I have previously built an algorithm that satisfies the condition of rule #4 and the line of code I used was:

arrayshift[i][j] = originalarray[i - 1][j - 1];

The code copies the cell from the position at i-1 and j-1 to i,j in the array.

Can anyone tell me how to write, if not explain how to write the line of code for the rule mentioned above. I wrote the following but received errors;

arrayshift[i][j] = originalarray[i][j-1] + array[[i-1][j]+[i-1][j-1]]/2;

Thanks for any suggestions provided.

  • 5
    `I wrote the following but received errors;` what errors? Where is the source for the formula you are trying to implement? – amit Apr 08 '15 at 09:07
  • 1
    `array[[i-1][j]+[i-1][j-1]]/2;` This doesn't look like valid java syntax – Balázs Édes Apr 08 '15 at 09:11
  • array index should be integer. how this `array[[i-1][j]+[i-1][j-1]]` is valid? – Prashant Apr 08 '15 at 09:13
  • Since 12 minutes have passes and we didn't get clarification from OP, I am voting to close as "Unclear what you are asking". – amit Apr 08 '15 at 09:18

1 Answers1

-2

You probably get ArrayIndexOutOfBoundExceptions.

With your first iteration (i=0), i-1 would equal -1, where your Array is out of bounds.

Always check for your Array boundaries, and have a plan what to do if you are out of bounds

if((i-1) < 0 || (i+1) > arraysize) //for example here you should stop your iteration 
//or do something else

Another possible issue could be the following line:

arrayshift[i][j] = originalarray[i][j-1] + array[[i-1][j]+[i-1][j-1]]/2;, where you state at one point originalarray... but on the other hand you use array for the second operand, where you probably want to use the originalarray as well

Your Array Index is wrong as well.

Your formula states the following:

I[I[i-1,j] + I[i-1,j-1]]/2 So you probably want something like this:

originalarray[originalarray[i-1,j]+originalarray[i-1,j-1]]/2

But you should save those values in variables

int val1 = originalarray[i-1,j];
int val2 = originalarray[i-1,j-1];

arrayshift[i][j] = originalarray[i][j-1] + originalarray[val1+val2]/2;

For the second part of this formula, your second index is missing. So you need to update the formula to include some kind of second index (maybe val1 and val2 are the indexes?)

arrayshift[i][j] = originalarray[i][j-1] + originalarray[val1][val2]/2;
Loki
  • 4,065
  • 4
  • 29
  • 51
  • Pretty sure there is a compilation error. Don't jump into conclusions and give generic answers. This answer is valid only if you can back it up with the place where error out of bound occurs, and why. In this case, the syntax of `array[[i-1][j]+[i-1][j-1]]/2` really smells to me - but I doubt you bothered to read it. – amit Apr 08 '15 at 09:10
  • I bothered to read it, though it didn't jump into my eye at first glance. I updated my answer to include this as well, and explained a possible out of bounds example – Loki Apr 08 '15 at 09:13
  • the code will not compile because of Array's index value. – Prashant Apr 08 '15 at 09:14
  • Even after I specifically mentioned what's not compiling, your edit doesn't cover it. There is no need to rush into no informative answers, and the "You might have X" (where it is most likely not the case) is a perfect example of it. – amit Apr 08 '15 at 09:16
  • Hi I appreciate your assistance in helping me to clarify this issue, I tried some of the techniques you stated and again I apologise for not posting my issue / errors. – user2162224 Apr 08 '15 at 20:05
  • Hello again, thank you everyone for your help. I tried all the advice given but ran into some errors. 'Amit' I apologise but you are right I should have posted the errors I obtained. I tried the advice given but encountered some errors, after trouble-shooting I got part of the equation to work: newarray[i][j] = (char) (array[i][j-1]+ (char)(array[i-1][j]+array[i-1][j-1])/2); I tried this and the program executes and I get the solution I wanted. Thanks again for all your support and help and again I apologise for the poor manner in which my question was initially asked. – user2162224 Apr 08 '15 at 20:27