Simple loop increment question please help!

If someone could just point me in the right direction, it would be appreciated.

The given code is the framework* i must use.
I need to use the setup and loop sections to compute the total of the array (55)

How would i initialize, sum, done and i, in the void setup()
and add the coding to the void loop() to sum the array ?
im not sure how to set it up
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <stdio.h>

int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 9};
int sum;
int i;
int done;

void setup()
{
}
void loop()
{
}

int main ()
{
    setup();
    while (!done)
   {
        loop();
   }
    printf("Sum = %d\n", sum);
    return 0;
}

Last edited on
Bump!
How would i initialize, sum, done and i, in the void setup()

What value do they need to have initially? Assign those values in setup.


and add the coding to the void loop() to sum the array ?

How would you code the body of the loop if it weren't a function? Put it in loop().

(Don't bump posts so soon after they're made unless you, you know, made an attempt and have some code to update with.)
Ok, I guess initializing those values is what I'm not grasping.

int sum = ? - would need to be the total of the array elements ?

int i=0 < for my array indexer ?

done < unsure

For the loop, does it need to be a while loop since "while" is referenced later under "int main" ?

1
2
3

(int i=0; i < 10; i++)
     sum+= data[i];


sorry I'm an impatient newby I'll keep that in mind.
Last edited on
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?
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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.)
I see your having pun with this.. Ok, would an if else work for my "done" issue ?

Also in the global done is declared as int,
 
int done;

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.


1
2
3
4
5
6
7

if (sum = 55)
{
}
else (sum < 55)
{
} 


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'
Last edited on
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.
Topic archived. No new replies allowed.