# Simple addition works out wrong (drives me nuts...)

In a sketch I use the folowing line:
total = total + Value
in stead of getting the expected addition I get the double value of Value. What am I doing wrong?

``````int value;                   // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the values to 0:
for (int i = 0; i < 2; i++)
value[i] = 0;
}

void loop() {
for (int i = 0; i < 2; i++){
//Move all values 1 down in the array
value[i]=value[i+1];
// value= value: value=value
}
// read input and assign to highest array member
// add the input value to the total:
total = total + value;
//subtract the first array value from the total (FIFO)
total = total -value;
// calculate the average:
average = total / 2;
delay(500);
}
``````

a sample result of it is :
value = 0
value = 0
value = 0
total+ value = 0
total- value = 0
average =0
value = 0
value = 0
value = 0
total+ value = 0
total- value = 0
average =0
value = 0
value = 0
value = 23
total+ value = 46
total- value = 46
average =23
value = 0
value = 46
value = 83
total+ value = 166
total- value = 166
average =83
As you can see the value of total+value is allways double of value ?!?!
The sketch was adapted to get these values out.
```
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the values to 0:
for (int i = 0; i < 2; i++)
value[i] = 0;
}

void loop() {
for (int i = 0; i < 2; i++){
//Move all values 1 down in the array
value[i]=value[i+1];
Serial.print(“value[”);
Serial.print(i);
Serial.print("] = ");
Serial.println(value[i]);
// value= value: value=value
}
// read input and assign to highest array member
Serial.print("value = ");
Serial.println(value);
// add the input value to the total:
total = total + value;
Serial.print("total+ value = ");
Serial.println(total);
//subtract the first array value from the total (FIFO)
total = total -value;
Serial.print("total- value = ");
Serial.println(total);
// calculate the average:
average = total / 2;
Serial.print(“average =”);
Serial.println(average);
Serial.println();
delay(500);
}

__```**__

Indexing for array access is zero based. I.e., value refers to the third int in value. However, value is only declared to hold two ints. Change the declaration of value to make it large enough for three.

I know but that does not explain why total holds a value before it is assigned one.
If I print the value of Total before it is added to itself and the value than it already hold this value?

Why not? Writing beyond the bounds of an array is undefined. Anything could happen.

It works when the array is . So that is solved (got some grey hairs from it) but it's strange that no error is generated and the results are quite "bizar"
I assumed that "int Value" gave me Value,  and  to work with. Thanks.

I know but that does not explain why total holds a value before it is assigned one.

Every variable in a computer program is located on a memory address and every memory address holds a value as there are bits in it to hold a value. Maybe meaningless but still.
It is how (memory)chips work. That is also the reason why you better initialize your variables with a known value.

but it's strange that no error is generated

No it is not strange. There is no array bounds checking in a compiled language like C. That sort of thing only happens in interpreted languages like Java and Python.

No it is not strange. There is no array bounds checking in a compiled language like C. That sort of thing only happens in interpreted languages like Java and Python.

Not quite correct. Many compiled languages use range checking, including Java(compiled), C#, Basic(compiled) and so on.

C and C++ do no range checking because they are used by system programmers and designed for pure speed with fast pointer arithmetic.
That is also why their array bounds start with 0. SteveJLV should not feel too bad, we've all made this same mistake.

Joe

Many compiled languages use range checking, including Java(compiled), C#, Basic(compiled) and so on.

Have you noticed that you have to put compiled in brackets next to those. They are all interpreted languages that have been bodged.
Run time array checking is a severe handicap to any language especially in a real time environment like the arduino.

Try Ada, then - a nice strong compiled language that I use daily within a real-time safety-involved/critical environment.

There's no justification for claiming that array range checking is the sole preserve of an interpreted language.

Doddy:
There's no justification for claiming that array range checking is the sole preserve of an interpreted language.

Agreed.

There's no excuse for not controlling your own code.

Don't blame your own compiler for not having safety rails. The real problem is you writing or using code that you don't understand.

At least this thread started out with a simple direct example. There's lot of threads where the misunderstood part is buried in a pile of code with libraries and hardware needs as well.

There is no array bounds checking in a compiled language like C.

When using array bounds checking [ABC] all access of the array needs to be checked, except those that can be determined compile time (in theory). Doing ABC runtime adds code like this for every place where you access an array.

``````// suppose your original statement
x = array[ i * 4 ];

extents too

index = i*4;
if (index < array.minIndex || index > array.maxIndex)
{
exit("Array out of bounds error: ", array.name, index, ...);
}
else
{
x = array[index]; // for example.
}
``````

This slows code down like sand in a gearbox (OK maybe a bit less) and causes a bigger footprint.
In Turbo Pascal and I think in Delphi too, you have directives that can be used in the code to switch on/off the ABC. I do not know such switch for C/C++ …
Within C the choice was made to make it the responsibility of the programmer and go for performance and small footprint.

Most important argument against ABC is that ABC in fact allows bugs to exist in your code.
You do not want that, no matter which language you use, compiled or interpreted.

There are static code checkers like LINT that can do partial ABC, but still only those with fixed values.

Fortunately C++ allows you to wrap arrays in a class and add bounds checking to them, and you can even add runtime switches to use ABC or not
// such a switch is almost as expensive as the check itself, but OK it’s possible.

Any beginner can learn to color inside the lines:

``````#define NAMES 4
char name[ 4 ][ 8 ] = { "Mathew", "Mark", "Luke", "John" };

.....

for ( i = 0; i < NAMES; i++ )  Serial.println( name[ i ] );

i = 0;
while ( i < NAMES )  Serial.println( name[ i++ ] );
``````

I am sure that other examples will come to mind.