Calculate a sum of an array in Arduino

  arduino, arduino-c++, arduino-uno, c++

I am trying to code something where you store 5 integers in an array and then calculate the sum of the 5 stored values. Then the 5 values ​​are printed and also the sum.

I tried this:

const int sizeOFarray = 5;
int b[sizeOFarray] = {10, 20, 30, 40, 50};
int sum = 0;

void setup ()
{
  Serial.begin(9600);
}
void loop ()
{
  // sum of array b
  for ( int i = 0; i < sizeOFarray; i++ )
    sum += b[ i ];
  Serial.print('Sum of total elements of an array:') ;
  Serial.print(sum) ;
}

but it gives me a weird output:

720031034735031034750031034765031034780031034795031034810031034825031034840031034855031034870031034885031034900031034915031034930031034945031034960031034975031034990031034100503103

and it never stops.

Source: Windows Questions C++

LEAVE A COMMENT