# Need help urgently!!

Can anybody explain this code for me!!!

double result = 0;
float BATT1[5];
float BATTERY[21]; // J from 1 to 20
for(int J=1;J<=20;J++)
{
for(int k=0;k<10;k++)
{
BATT1[k] = getAnalog(J);
result += BATT1[k];
}

result = result / 10;
BATTERY[J] = result;
result = 0;
}

Looks to me that for each of 20 batteries it takes 10 readings, averages them, and saves that as the voltage for that battery.

but i'd change... float BATT1[5]; to float BATT1[10];

Looks to me as though there is a semi-colon missing

Yep, but float BATT1[5] is only 5 long so it over writes something else. BATT1 is not required.

Mark

Thanks for the reply zimboZA! But I don't really understand how is it done.

Does it do the inner loop first and if the condition is not true, i.e k=11, it will continue to divide by 10?

Does it do the inner loop first and if the condition is not true, i.e k=11, it will continue to divide by 10?

Stick a Serial.print() statement in there and learn for yourself.

``````  double result = 0;
float BATT1[10];
float BATTERY[21];

// loop Battery J from 1 to 20
for(int J=1;J<=20;J++)
{
// take 10 readings from Battery J and store sum in "result"
for(int k=0;k<10;k++)
{
BATT1[k] = getAnalog(J);
result += BATT1[k]; // short way of doing result=result+BATT1[k]
}

// store average of "result" back into Battery J location
result = result / 10;
BATTERY[J] = result;

// reset "result" for next battery
result = 0;
}
``````

abrookfield:

``````  double result = 0;
``````

float BATT1[10];
float BATTERY[21];

// loop Battery J from 1 to 20
for(int J=1;J<=20;J++)
{
// take 10 readings from Battery J and store sum in “result”
for(int k=0;k<10;k++)
{
BATT1[k] = getAnalog(J);
result += BATT1[k]; // short way of doing result=result+BATT1[k]
}

// store average of “result” back into Battery J location
result = result / 10;
BATTERY[J] = result;

// reset “result” for next battery
result = 0;
}

Thanks alot!!