Discuss Simple c code in the C Programming forum on Dev Shed. Simple c code C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 2,476
Time spent in forums: 1 Month 2 Weeks 2 Days 5 h 26 m 31 sec
Reputation Power: 2194
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!
Posts: 3
Time spent in forums: 1 h 10 m 58 sec
Reputation 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.
Posts: 3,357
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 6 m 43 sec
Reputation Power: 383
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.
__________________
[code]Code tags[/code] are essential for python code!
Posts: 3
Time spent in forums: 1 h 10 m 58 sec
Reputation Power: 0
Quote:
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.
Posts: 1,936
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
Quote:
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.
Posts: 3
Time spent in forums: 19 m 33 sec
Warnings Level: 10
Number of bans: 1
Reputation 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 11:25 AM.
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,383
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 49 m 30 sec
Reputation Power: 4080
^^^^
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.
__________________ 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
Last edited by Scorpions4ever : September 15th, 2012 at 11:31 AM.