# Arithmetic Operations on 2D array on Arduino DUE/Mega

I am working on Image processing program that will divide certain columns with other columns.

Check out my sample code & its results!

The problem is that, I run this code on both arduino Mega & DUE.
Arduino Mega give the correct division while DUE fails & shows only zeros.
I don’t know why Arduino DUE isn’t dividing properly, I have tried many other mathematical equations but Arduino DUE don’t work properly. Please help me out!

Beside mathematical calculations, Arduino Due works finely!
P.S Both arduinos are healthy

My code:

``````int narray[5][7];

void setup() {
Serial.begin(9600);

}

void loop() {
int h[5][28] =        {{0,  0,  0,  255,  0,  0,  0,0,  0,  0,  255,  0,  0,  0,0,  0,  0,  255,  0,  0,  0,0,  0,  0,  255,  0,  0,  0},
{0, 255,  255,  255,  255,  255,  0,0, 255,  255,  255,  255,  255,  0,0, 255,  255,  255,  255,  255,  0,0, 255,  255,  255,  255,  255,  0},
{0, 255,  0,  255,  0,  0,  0,0, 255,  0,  255,  0,  0,  0,0, 255,  0,  255,  0,  0,  0,0, 255,  0,  255,  0,  0,  0},
{0, 255,  255,  0,  255,  255,  0,0, 255,  255,  0,  255,  255,  0,0, 255,  255,  0,  255,  255,  0,0, 255,  255,  0,  255,  255,  0},
{0, 255,  255,  255,  255,  255,  0,0, 255,  255,  255,  255,  255,  0,0, 255,  255,  255,  255,  255,  0,0, 255,  255,  255,  255,  255,  0}, };

int k=0;
for( int f=0; f<5; f++)
{
for(int e=0; e<21; e++)
{

h[f][k]=h[f][k]/h[f][e+7];
delay(50);
if(k==7)
{
k=0;
delay(50);
}
else k++;

}
}

for (int c=0; c<5;c++)
{
for(int d=0; d<7; d++)
{
narray[c][d]=h[c][d];
Serial.print(narray[c][d]); //send the byte to serial port is ok; show the byte in HEX format where u r receiving it
Serial.print("\t");
}
Serial.print("\n");
}

}
``````

Arduino DUE results:

``````0	0	0	0	0	0	0
0	0	0	0	0	0	0
0	0	0	0	0	0	0
0	0	0	0	0	0	0
0	0	0	0	0	0	0
``````

Arduino Mega results

``````0	0	-1	1	-1	0	0
1	0	-1	0	-1	-1	-1
0	0	-1	1	-1	0	0
1	0	0	0	0	0	-1
0	0	0	0	0	-1	0
``````

`        h[f][k]=h[f][k]/h[f][e+8];`What do you think is supposed to happen when the array index goes out-of-bounds?

jremington:

``````        h[f][k]=h[f][k]/h[f][e+8];
``````

What do you think is supposed to happen when the array index goes out-of-bounds?

Dear Leave this logic. I know it will go out of limits but both arduinos Should give same results!

P.S i forgot to edit that logic before posting. Now i will change its limits in Post.

both arduinos Should give same results!

No, because you were probably accessing uninitialized memory.

umer427:
Dear Leave this logic. I know it will go out of limits but both arduinos Should give same results!

No, they should not. Reading/writing beyond the bounds and ANY data element is a recipe for unpredictable operation. There is not a reason in the world to expect EITHER processor to give you the correct result, much less to expect BOTH to give the same result. There isn't even reason to expect either will always, or even ever, give the same result. FIX your code, and both will work correctly, always.

Regards,
Ray L.

You are engaging in undefined behaviour. The program can do anything, and since the architectures are different, there is no guarantee that things happen the same way. At the very least things are moved around because of the different width of an int.

RayLivingston:
No, they should not. Reading/writing beyond the bounds and ANY data element is a recipe for unpredictable operation. There is not a reason in the world to expect EITHER processor to give you the correct result, much less to expect BOTH to give the same result. There isn't even reason to expect either will always, or even ever, give the same result. FIX your code, and both will work correctly, always.

Regards,
Ray L.

Dear I think I you go run this program then post its result. Accessing beyond limits will not either Explode me or arduino. I wrote in my post that it is just a sample program. I have tried all the possible limits, simple addition, subtraction on 2d matrix but DUE fails to give results! I invite you to just create a simple two 2d arrays & merge them or add them in your own, Then burn that code on Arduino DUE & on anyother Arduino. Compare the results & post them!
Thanku!

jremington:
No, because you were probably accessing uninitialized memory.

hahaha. Can you please initialize it

KeithRB:
You are engaging in undefined behaviour. The program can do anything, and since the architectures are different, there is no guarantee that things happen the same way. At the very least things are moved around because of the different width of an int.

yapp right! But anyway out? all other Arduinos giving same results! only Arduino DUE isn't

umer427:
Dear I think I you go run this program then post its result. Accessing beyond limits will not either Explode me or arduino. I wrote in my post that it is just a sample program. I have tried all the possible limits, simple addition, subtraction on 2d matrix but DUE fails to give results! I invite you to just create a simple two 2d arrays & merge them or add them in your own, Then burn that code on Arduino DUE & on anyother Arduino. Compare the results & post them!
Thanku!

No, I'm not going to waste my timing running code that has very obvious problems. YOU need to FIX the code so it does not use un-initialized memory! If you're not willing to do that, then you're wasting your time, and ours, and you're on your own. But you better get used to the code behaving unpredictably, because it will.

Regards,
Ray L.

RayLivingston:
No, I'm not going to waste my timing running code that has very obvious problems. YOU need to FIX the code so it does not use un-initialized memory! If you're not willing to do that, then you're wasting your time, and ours, and you're on your own. But you better get used to the code behaving unpredictably, because it will.

Regards,
Ray L.

Ahan. Thanku!

Also, if the largest element in the array is 255, why make it an int. You can cut the memory required in half by using a byte.

I just lost my last post - so here is the short version
Use “blink without delay” , initialize (!) your array PROPERLY (?) , fix “out of bound stuff” and Google for
DIVIDE BY ZERO
and than fix some silly error in the last Calculated output.

I just wonder what other irrelevant stuff you will be chastised for next.

Thanks for fun post.

``````h[c][d]int narray[5][7];

void setup() {
Serial.begin(115200);

}

void loop() {
int h[5][28] =        {{0,  0,  0,  255,  0,  0,  0, 0,  0,  0,  255,  0,  0,  0, 0,  0,  0,  255,  0,  0,  0, 0,  0,  0,  255,  0,  0,  0},
{0, 255,  255,  255,  255,  255,  0, 0, 255,  255,  255,  255,  255,  0, 0, 255,  255,  255,  255,  255,  0, 0, 255,  255,  255,  255,  255,  0},
{0, 255,  0,  255,  0,  0,  0, 0, 255,  0,  255,  0,  0,  0, 0, 255,  0,  255,  0,  0,  0, 0, 255,  0,  255,  0,  0,  0},
{0, 255,  255,  0,  255,  255,  0, 0, 255,  255,  0,  255,  255,  0, 0, 255,  255,  0,  255,  255,  0, 0, 255,  255,  0,  255,  255,  0},
{0, 255,  255,  255,  255,  255,  0, 0, 255,  255,  255,  255,  255,  0, 0, 255,  255,  255,  255,  255,  0, 0, 255,  255,  255,  255,  255,  0},
};

//Serial.println("Orignal");
//  for (int c = 0; c < 5; c++)
//  {
//    for (int d = 0; d < 7; d++)
//    {
//      narray[c][d] = h[c][d];
//      Serial.print(narray[c][d]); //send the byte to serial port is ok; show the byte in HEX format where u r receiving it
//      Serial.print("\t");
//    }
//    Serial.print("\n");
//  }

int k = 0;
for ( int f = 0; f < 5; f++)
{
for (int e = 0; e < 21; e++)
{
Serial.print("1 calcualtion  ");
Serial.println(  h[f][k] ,HEX);
Serial.print("2 calcualtion  ");
Serial.println( h[f][e + 7] ,HEX);
//delay(3000);

h[f][k] = h[f][k] / h[f][e + 7];
Serial.print("result  ");
Serial.println(  h[f][k] ,HEX);

//      delay(50);
if (k == 7)
{
k = 0;
delay(50);
}
else k++;

}
}

Serial.println("Calcualted");

for (int c = 0; c < 5; c++)
{
for (int d = 0; d < 7; d++)
{

Serial.print(h[c][d],HEX );
Serial.print(" ");
narray[c][d] = h[c][d];
Serial.print(narray[c][d],HEX ); //send the byte to serial port is ok; show the byte in HEX format where u r receiving it
Serial.print("\t");
}
Serial.print("\n");
}
for(;;);

}
``````