Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6126877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:25:57+00:00 2026-05-23T16:25:57+00:00

here is the code void main() { int x,y,i=0,o,p; printf(enter the command); scanf(%s,dir); printf(enter

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:25:58+00:00Added an answer on May 23, 2026 at 4:25 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
Here is my code!(sorry for my poor english) #include<stdio.h> int convert(char ch); int main(void)
Here is my code : public static void main(String[] args) { // System.setProperty( //
Here's the code snippet: public static void main (String[]arg) { char ca = 'a'
Here is a sample code: public class TestIO{ public static void main(String[] str){ TestIO
here is code snippet void F (int a, int *b) { a = 7
I'm new to C. Here's my code: /* Using scanf() */ #include <stdio.h> int
Here is my code: - (void)loadView { //hard coded array of content for each
Here is my code: partial void OnisApprovedChanging(bool value) { this.dateApproved = DateTime.Now; } 'dateApproved'
Here's the code in AlertTableView: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSInteger index = 12345; NSLog(@AlertTableView:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.