Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

p505 Group Title

#include <stdio.h> int data[] = {1, 3, 5, 2, 4, 6, 7, 9, 8, 10}; int sum; int i; int done; void setup() { } void loop() { } int main() { setup(); while (!done) { loop(); } printf("Sum = %d\n", sum); return 0; }

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Step 2: Compile to make sure you entered it correctly, but don't run it; it is an innite loop and running it will make you nervous. Note that setup() is called by main() exactly once and loop() is called as often as necessary to make the "done" value be interpreted as true. Step 3: Add code to setup() to initialize "sum", "done" and "i" as necessary. Add code to loop() to compute the sum of the data. Each time it runs it needs to increment "i" and then use "i" to add an array value to sum. Run it to make sure the output is \Sum = 55". I ended up with three new lines of code in setup() and three other new lines of code in loop(). While you are working on this, your program may encounter an innite loop that you did not intend. If you don't get an output immediately, press Ctl-C (the Control key and the C key simultaneously). DO NOT modify main() in any way. DO NOT add code anywhere except to setup() and loop(). If you will perform the correct initializations in setup() and the correct computations, including setting "done" at the correct point, in loop(), this will work as required.

    • 2 years ago
  2. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Im not sure how to start this, I dont know how to add the array values. Any help would be much appreciated.

    • 2 years ago
  3. JoãoVitorMC Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    a simple way to do this: #include <stdio.h> int data[] = {1, 3, 5, 2, 4, 6, 7, 9, 8, 10}; int summ=0; int sum(int data[]){ for (i=0;i<10;i++){ summ += data[i]; } return summ; } int main(){ printf("Sum= %d",sum(data[])) }

    • 2 years ago
  4. JoãoVitorMC Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    return 0; before the last "}"

    • 2 years ago
  5. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Should it be i<11? How does this add a space to the array after this sum is calculated? What does summ += data[i] do exactly? Does it increment where you are adding data in the array?

    • 2 years ago
  6. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks for your help by the way.

    • 2 years ago
  7. JoãoVitorMC Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    look at this, now is better: #include <stdio.h> int data[]={1, 3, 5, 2, 4, 6, 7, 9, 8, 10}; int i,summ=0; int sum(int data[]){ for(i=0;i<10;i++){ summ += data[i]; } return summ; } int main() { printf("Sum= %d",sum(data)); return 0; }

    • 2 years ago
  8. JoãoVitorMC Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    it must be i<10, because we count the values of an array starting in position 0 so if we have an array with n elements i<n

    • 2 years ago
  9. JoãoVitorMC Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    got it?

    • 2 years ago
  10. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    ok, what code should go in loop and what code should go in setup? setup() { sum = 0; } loop() for (i=0; i<10; i++) { sum=sum+a[i]; } Like that?

    • 2 years ago
  11. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    a = data

    • 2 years ago
  12. JoãoVitorMC Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    the loop is right, but the setup i do not understand why it is necessary here

    • 2 years ago
  13. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I have to add code only in loop and setup, nowhere else.

    • 2 years ago
  14. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Can loop stay a void type?

    • 2 years ago
  15. JoãoVitorMC Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    yes sure, but it's better stay it alone becouse you can call this function any time in our code

    • 2 years ago
  16. JoãoVitorMC Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    hey, maybe the setup() is here to call the loop(), gets the result and check the value if it's right returning a bool value... i'm not sure

    • 2 years ago
  17. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Add code to setup() to initialize "sum", "done" and "i" as necessary. Add code to loop() to compute the sum of the data. Each time it runs it needs to increment "i" and then use "i" to add an array value to sum.

    • 2 years ago
  18. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    setup() sum=0; i=0; how do initialize done? loop() for (i=0; i<10; i++) { sum=sum+data[i]; } How does that look?

    • 2 years ago
  19. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I think all I need is a way to set "done" in loop() - what do you think?

    • 2 years ago
  20. JoãoVitorMC Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    yeah maybe... but loop() have to return the sum

    • 2 years ago
  21. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I got it, it compiled and returned sum=55, thanks Joao.

    • 2 years ago
  22. JoãoVitorMC Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    xD

    • 2 years ago
  23. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    @jakev8

    • 2 years ago
  24. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    @jakev8 .

    • 2 years ago
  25. p505 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I never initialized "done" - it still gave the correct output tho.

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.