here is the code
void main()
{
int x,y,i=0,o,p;
printf("enter the command");
scanf("%s",dir);
printf("enter the limit");
scanf("%d%d",&o,&p);
printf("enter the place");
scanf("%d%d",&x,&y);
clrscr();
if(((x>0)&&(x<o))&&((y>0)&&(y<p)))
{
while(dir[i]!='\0')
{
for(i=0;i<strlen(dir);i++)
{
if(dir[i]=='l')
{
if(a=='n')
a='w';
else if(a=='w')
a='s';
else if(a=='s')
a='e';
else
a='n';
}
else if(dir[i]=='r')
{
if(a=='n')
a='e';
else if(a=='e')
a='s';
else if(a=='s')
a='w';
else
a='n';
}
}
}
printf("%d %d %c",x,y,a);
}
else printf("out of area");
getch();
}
how to make the above program efficient using some oops concept in c or by some way?
You should try to improve the program stepwise, and as it’s written in C making it OOPSy is probably not the highest priority. Eg start out by improving the selection logic by introducing a
switch()statement:You can also replace the inner if-chain with a switch, but that would get pretty messy. So, break that inner construction out in a function.
See? Easier on the eye even with plain if’s 🙂
That way the inner block becomes
Bonus points if you figure out how to fold the 3 very similar but not entirely identical inner blocks into 1 function!
And so on. Just keep on improving step-wise, making your program more readable at every iteration.
Ah yes, and while you’re at it, put some useful comments here and there, especially comments that will help you figure out in 3 months what the &!@%#& you tried to achieve with a certain function 🙂
Edit: according to Wikipedia indentation is
And I’m incredibly puzzled that this concept seems foreign to you.