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

  • SEARCH
  • Home
  • 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 5932587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:51:17+00:00 2026-05-22T14:51:17+00:00

I am using Turbo C, and I’ve some query about my code. I’m just

  • 0

I am using Turbo C, and I’ve some query about my code. I’m just perplexed… The program first asks for a list of numbers(you shouldn’t type more than 20). As the user types in the numbers, they are placed in the array list[]. Once the user terminates the list by typing 0*(which is not placed on the list)*, the program then calls the sort() function, which sorts the values in the list. At the last part, which has a comment of /*I AM NOW CONFUSED WITH THIS PART*/, is the part where I need your help… Kindly help me out.

enter image description here

       File   Edit   Run   Compile   Project   Options   Debug   Break/watch
    ╒════════════════════════════════════ Edit ════════════════════════════════════╕
    │      Line 1     Col 43  Insert Indent Tab Fill Unindent * C:NONAME.C         │
    │                                                                              │
    │ #define MAXSIZE 20                            /* size of buffter */          │
    │ void sort(int[], int);                        /* prototype */                |
    │                                                                              |
    │ main()                                                                       |
    │ {                                                                            |
    │     static int list[MAXSIZE];                 /* buffer for numbers */       |
    │     int size = 0;                             /* size 0 before input */      |
    │     int dex;                                  /* index of array */           |
    │     do                                        /* get list of numbers */      |
    │     {                                                                        |
    │         printf("Type number: ");                                             |
    │         scanf("%d", &list[size]);                                            |
    │     }                                                                        |
    │     while(list[size++] != 0);                 /* exit loop on 0 */           |
    │                                                                              |
    │     sort(list,--size);                        /* sort nubmers */             |
    │     for(dex=0; dex<size; dex++)               /* print sorted list */        |
    │         printf("%d\n", list[dex]);                                           |
    │                                                                              |
    │      getche();                                                               |
    │ }                                                                            |
    │                                                                              |
    │ void sort(int list[], int size)                                              |
    │ {                                                                            |
    │     int out, in, temp;                        /* I AM NOW CONFUSED */        |
    │                                                                              |
    │     for(out=0; out<size-1; out++)             /* IN THIS PART! */            |
    │         for(in=out; in<size; in++)                                           |
    │              if(list[out] > list[in])                                        |
    │              {                                                               |
    │                  temp=list[in];                                              |
    |                  list[in]=list[out];                                         |
    │                  list[out]=temp;                                             |
    │              }                                                               |
    │ }                                                                            |
    │                                                                              |
    │                                                                              |
    ├─────────────────────────────────── Watch ────────────────────────────────────┤
    │                                                                              │
    └──────────────────────────────────────────────────────────────────────────────┘
     F1-Help  F5-Zoom  F6-Switch  F7-Trace  F8-Step  F9-Make  F10-Menu   NUM
  • 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-22T14:51:18+00:00Added an answer on May 22, 2026 at 2:51 pm

    It’s just code that is meant to sort the array elements but it’s never going to work in its current form since:

    temp=list[in];
    list[out]=temp;
    

    will overwrite list[out] with list[in] without ever preserving the original contents of list[out].

    The best way to swap two variables is with something like:

    temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
    

    And, please, for the love of whatever deity you believe in, don’t do this 🙂

    So, if your question is how to sort the data, then the following pseudo-code should help. I’d provide C code but, on the off chance that this is homework, you should do some of the work yourself a.

    def sort (arr[], sz):
        swapped = true                       # Force loop entry.
        while swapped:                       # Loop until a pass had no swaps.
            swapped = false
            for idx goes from 1 to sz-1:     # For all but the first element.
                if arr[idx-1] > arr[idx]:    # If order is wrong.
                    swapped = true           # More passes will be needed.
                    temp = arr[idx-1]        # Swap
                    arr[idx-1] = arr[idx]    #   the
                    arr[idx] = temp          #     elements.
    

    This is a bubble sort variation which exits as soon as the list is sorted (well, after one pass with no swaps). Some naive variants will simply keep going for roughly n2 times regardless.


    a If you’d like to indicate in a comment that it’s not homework, I’d be happy to provide the C code. Just be aware (if you’re planning to lie to me) that your educators will almost certainly be able to see that code and you will probably fail in that case (or be expelled for blatant plagiarism).


    And, since you’ve stated it’s not homework, here’s a complete C program illustrating it:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define FALSE (1==0)
    #define TRUE  (1==1)
    
    static void sort (int arr[], int sz) {
        int idx, temp, swapped;
    
        swapped = TRUE;                        // Force loop entry.
        while (swapped) {                      // Loop until a pass had no swaps.
            swapped = FALSE;
            for (idx  = 1; idx < sz; idx++) {  // For all but the first element.
                if (arr[idx-1] > arr[idx]) {   // If order is wrong.
                    swapped = TRUE;            // More passes will be needed.
                    temp = arr[idx-1];         // Swap
                    arr[idx-1] = arr[idx];     //   the
                    arr[idx] = temp;           //     elements.
                }
            }
        }
    }
    

     

    int main (int argc, char *argv[]) {
        int sz, i, *vals;
    
        sz = argc - 1;
        if (sz < 1)
            return 0;
        if ((vals = malloc (sz * sizeof (int))) == NULL) {
            printf ("ERROR: Cannot allocate memory.\n");
            return 1;
        }
    
        for (i = 0; i < sz; i++)
            vals[i] = atoi (argv[i+1]);
    
        printf ("Numbers before:");
        for (i = 0; i < sz; i++)
            printf (" %d", vals[i]);
        printf ("\n");
    
        sort (vals, sz);
    
        printf ("Numbers after :");
        for (i = 0; i < sz; i++)
            printf (" %d", vals[i]);
        printf ("\n");
    
        free (vals);
        return 0;
    }
    

    Running this with:

    $ ./testprog 3 1 4 1 5 9 2 6 5 3 5 8 9
    

    gives you the output:

    Numbers before: 3 1 4 1 5 9 2 6 5 3 5 8 9
    Numbers after : 1 1 2 3 3 4 5 5 5 6 8 9 9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am using Turbo C... I just have some query, I found a
I remember some years ago, when I learned C using Turbo C, it had
My code is pasted below.When I run this program,it keeps on calculating.I am using
I just want to ask something about my code. #define LIM 40 main() {
I am using Turbo C++ 3.0 Compiler While using the following code .. char
wondering all about C, can you demystify this I am using turbo C I
I'm running a graphical program in Turbo C++ using DosBox on Windows 7 64
I always find that some people (a majority from India) are using turbo C.
I am using borland turbo C++ complier (4.5). This is my code but i
I am trying to write data stored in a binary file using turbo C++.

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.