Shop OBEX P1 Docs P2 Docs Learn Events
Counting to 200 than back to zero — Parallax Forums

Counting to 200 than back to zero

Hello, this code can count to 200, however, i need to modify it to count to 200 and then back to zero. I just can't seem to figure it out.


int main() // main function
{
for(int n = 0; n <= 200; n = n + 5) //...Count to 200
{
print("n = %d\n", n); // Display name & value of n
pause(500); // 0.5 s between reps
}
}

Comments

  • You need to look in your C reference in the description of the for statement for examples of how you'd count backwards. Look at how the for statement is translated into a loop.
  • I understand how to count backward. It's just I don't understand how to count to 200 then reverse that and count to zero.
  • Two loops. First one counts 0..200, second one counts 200..0.
  • Can you post the actual wording of the homework assignment? What is the goal? Is the loop supposed to count to 200 then back to 0 or are the printed values supposed to go to 200 and back to 0?
  • Yes so this is what I have and I keep getting an error. I am clearly missing something I just don't know what. I am three weeks into learning this program so it's hard for me to know where each statement should be.

    int main() // main function
    {
    for(int n = 0; n <= 200; n = n + 5) //Count to 200
    {
    print("n = %d\n", n); // Display name & value of n
    pause(500); // 0.5 s between reps
    }
    for(int n = 200; n >= 0; n = n + 10) //Count to 0
    {
    print("n = %d\n", n); // Display name & value of n
    pause(500); // 0.5 s between reps
    }

    }
  • In your second loop, what would be the value of n the second time? What needs to change to make n get smaller each time?

    tom
  • Would it be putting a minus sign instead of a plus in the second loop? I just tried that and get an error saying,


    error: 'n' undeclared (first use in this function)
    count to 200 by 5s.c:18:8: note: each undeclared identifier is reported only once for each function it appears in
    Done. Build Failed!
  • The actual wording is "Modify your program so that it counts up to 200 in steps of 5, then down to 0 in steps of 10".
  • I was able to figure it out using the while functions.

    Thanks for all the feed back
  • Did you forget to include simpletools.h?
    #include "simpletools.h"
    
    Your code worked fine for me when I added the #include... and changed the + 10 to -10
Sign In or Register to comment.