Sum - would need to be the total of the array elements ?
That's what sum needs to be when the while loop is done. What does it need to be initially?
For the loop, does it need to be a while loop since "while" is referenced later under "int main" ?
It will be clearer to you if it is done in the form of a while loop.
1 2 3 4 5 6 7 8
int sum = /* initial value? Goes in setup! */ ;
int i = /* initial value? Goes in setup! */ ;
bool done = /* initial value? Goes in setup! */ ;
while ( !done )
{
// what goes here?
}
#include <stdio.h>
int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 9};
int sum;
int i;
int done;
void setup()
{
int sum = 0 /* because i see this a lot.. ha */ ;
int i = 0 /* array indexer to start at 0 ? */;
bool done = 55 /* this is the outcome i want the loop to stop at ? */;
}
void loop()
{
int i = 0;
while(i < 10) {
sum += data[i];
i++;
}
int main ()
{
setup();
while (!done)
{
loop(); // would this be the same loop as above?
}
printf("Sum = %d\n", sum);
return 0;
}
You have already defined the variables in the global namespace. You should be modifying those variables, not creating new ones:
1 2 3 4 5 6 7
void setup()
{
sum = 0;
i=0;
done = false; // boolean variables can be either true or false, and clearly we're not done
// if we're just starting.
}
In loop you need to set done to true when it is appropriate to do so. There should not be an actual loop in loop, since it is meant to be the "body" of a loop (and that probably would've been a better name for the function.)
Also in the global done is declared as int, int done;
So it is.
but further down it uses the (!) which means true/not true, do i need to declare it as "bool" or just set it equal to false.
You can just follow the convention that 0 is false and non-zero is true, so yes, setting it to false initially is fine.
I don't see why i need the done involved, wont the loop stop once it reaches 10?
So what is the purpose of using an "exit'
According to the framework you must work with, the while loop doesn't stop executing until !done evaluates to false, and that is the reason you need done involved.