[Solved] how to make this program efficient in c? [closed]


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:

switch (dir[i]) {
    case 'l':
        ...
        break;
    case 'r':
        ...
        break;
    default:
        ....
        break;
}

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.

char newa (char olda) {
    if(olda=='n') return 'w';
    if(olda=='w') return 's';
    if(olda=='s') return 'e';
    return 'n';
}

See? Easier on the eye even with plain if’s 🙂
That way the inner block becomes

a = newa(a);

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

The placement of text farther to the
right to separate it from surrounding
text.

And I’m incredibly puzzled that this concept seems foreign to you.

2

solved how to make this program efficient in c? [closed]