Thread: Simple c code

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Simple c code


    Hey guys.. i‘m new into this stuff so help me out please!

    i need a simple code to do this:
    While we have condition 1, the program should do the following. if x is happening, do y. if x not happening, do w.


    But this must be a loop, it has to do y everytime x is happening and it has to do w if x not happening. but must happen again if x start again.


    understand?
    thanks in advance and sry for my monkey english.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,555
    Rep Power
    2338
    We help people that actually put in an effort themselves. So far all you've done is tell us your assignment. You need to post *your* attempt, in code tags, as well as any errors you might be getting, and then we can tell you what you've done wrong.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0
    Sorry. Ok here it goes.
    I'm trying to give commands to a robot. Basically, it will start to walk. When it hits something (a button will be pressed), it has to walk backwards. When it starts walking backwards, a light sensor will be turned on. The robot has to walk backwards until it detects a black line, then, it shall stop moving.

    I'm very noob at codes, so here's what I've done so far, didn't work properly.
    (I can't make tests, I only have access to the software and robot by friday mornings)
    I can get the robot to walk, and to start walking backwards when the button is pressed.

    Code:
    #include "API.h"
    #include "BuiltIns.h"
    
    void main(void)
    {
    int light; //this is the variable of the light sensor
    
    SetPWM (1,0);   // Motor 1, full rotation
    SetPWM (2,255); // Motor 2, full rotation (making it walk)
    
    {
    StartInterruptWatcher (1, FALLING_EDGE); 
    while (GetInterruptWatcher(1) !=1) //in case button is pressed
    Wait (10);
    
    SetPWM(1,255);
    SetPWM(2,0); //making it walk backwards
    
    {
    light=GetAnalogInput(1); //turning light sensor on
    if (light>=100) //light>=100 = sees a black line on floor
    {
    SetPWM (1,127);
    SetPWM (2,127); // motors off
    
    }
    }
    }
    }
    It will start walking backwards but won't advance to the new condition. I can't go any further with my little knowledge.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Code:
    #include<stdio.h>
    
    #define FOREVER for(;;)
    #define BIND(A,L,H) ((L)<(A)?(A)<(H)?(A):(H):(L))
    
    void y(void) {
      puts("y action");
    }
    
    void w(void) {
      puts("w action");
    }
    
    void(*function_table[])(void) = {y,w};
    
    int main(int ac,char*av[]) {
      extern volatile int x;
      int y;
      FOREVER {			/* While we have condition 1 */
        y = x;			/* the condition x */
        y = BIND(y,0,1);		/* x must be 0 or 1 */
        (*function_table[y])();	/* use y or w, depending */
      }
      return 0;			/* never happens */
    }
    Here's the program, simply dress the incomplete specifications and you're done.

    Comments on this post

    • bdb disagrees : Just say no (FOREVER sucks): #define BEGIN {
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,555
    Rep Power
    2338
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    #include<stdio.h>
    
    #define FOREVER for(;;)
    #define BIND(A,L,H) ((L)<(A)?(A)<(H)?(A):(H):(L))
    
    void y(void) {
      puts("y action");
    }
    
    void w(void) {
      puts("w action");
    }
    
    void(*function_table[])(void) = {y,w};
    
    int main(int ac,char*av[]) {
      extern volatile int x;
      int y;
      FOREVER {			/* While we have condition 1 */
        y = x;			/* the condition x */
        y = BIND(y,0,1);		/* x must be 0 or 1 */
        (*function_table[y])();	/* use y or w, depending */
      }
      return 0;			/* never happens */
    }
    Here's the program, simply dress the incomplete specifications and you're done.
    Thanks!! Gonna try it
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Anyway, the c idiom for "repeat forever" is

    for(;;)

    even if BDB disapproves of a specific macro.

    while(1)

    works, as would

    do
    while (1);

    or

    label:
    goto label; /* if c has such a thing. */
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by b49P23TIvg
    label:
    goto label; /* if c has such a thing. */
    C does have the infamous goto statement, but the others in your post are definitely more idiomatic. while (1) (in C) and while (true) (in C++) are what I always use.
  16. #9
  17. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by Lux Perpetua
    while (1) (in C) and while (true) (in C++) are what I always use.
    GCC used to (and may still do so) generate a "condition is always true" warning for that but not
    Code:
    for(;;)
    so for that reason alone I'd use the latter.
  18. #10
  19. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0
    Program is to append the contents of string str1 to the end of string str.
    My Code is as follows-
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <process.h>
    void main()
    {
    int a;
    char *str, *str1;
    printf("Enter any string:");
    gets(str);
    fflush(stdin);
    printf("Enter any string:");
    gets(str1);
    fflush(stdin);
    for(a=strlen(str);a<strlen(str)+strlen…
    {
    str[a]=str1[a-strlen(str)];
    fflush(stdin);
    }
    printf("After performing strcat(str,str1), str is now:");
    puts(str);
    getch();
    }


    [edited] Removed signature.[/edited]
    Last edited by Scorpions4ever; September 15th, 2012 at 12:25 PM.
  20. #11
  21. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,646
    Rep Power
    4248
    ^^^^
    Only reason I'm leaving the above post (after I removed the signature) is in case people want to point out how many things are wrong with it. Beginners, this is how NOT to write code.

    Oh dear. As I suspected, djdigitalgirl is an SEO wannabe and is cutting/pasting code from elsewhere, trying to look like an expert:
    http://in.answers.yahoo.com/question...7121736AA7aLHz

    Comments on this post

    • Lux Perpetua agrees : No joke - almost every line is incorrect! On top of that: no code tags and attempting to hijack the thread.
    Last edited by Scorpions4ever; September 15th, 2012 at 12:31 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo