Thread: Find Error in my Code(Ascending Order)

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    15

    Find Error in my Code(Ascending Order)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    void swap(int a,int b)
    {
        int x=a;
        a = b;
        b = x;
    }
    
    
    void ascending(int a[])
    {
        int i =0;
        while(a[i]!=0)
        {
            for(int m=i+1;a[m]!=0;++m)
            {
            if(a[i]>a[m])
                swap(a[i],a[m]);
            }
            printf(" %d",a[i]);
            i++;
        }
    }
    
    
    int main()
    {
        printf("\n\t\t\t\tASCENDING ORDER\n");
        int a[50],i=-1;
        do{
                i++;
            printf("\n\tEnter the Number: ");
            scanf("%d",&a[i]);
        }while(a[i]!=0);
      ascending(a);
    
    
        return 0;
    }
    Attached Files Attached Files
    • File Type: c main.c (606 Bytes, 101 views)

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    void swap(int a,int b)
    {
        int x=a;
        a = b;
        b = x;
    }
    The above code is wrong; there is a real reason that swap functions use pointers.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2020
    Posts
    15
    Code:
    void swap(int a,int b)
    {
        int x=a;
        a = b;
        b = x;
    }
    
    I did'nt get it my Friend(@stahta01). Actually I am a Newbie, Could you give me a much more clear expalnation.. With regards Xrosshair.
    Last edited by XrossHAIR; 12-15-2020 at 01:27 AM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Your second post to this thread is not visible to me.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, it was in the moderation queue but hard to see. Approved now.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You can only "return" a value from a function two ways.
    1. Using a return statement. Can only return a single value (which can be a structure)
    2. Passing the address of where you wish the result to be stored. This is where pointers are needed in swap statements.

    The swap function needs to be something like this.

    Code:
    void swap(int * a,int * b)
    {
        int x=*a;
        *a = *b;
        *b = x;
    }
    I think I got all the "*" in the right place; I did it from memory it might be wrong.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Dec 2020
    Posts
    15
    Thanks pal.. I got it now..
    Xrosshair

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  2. Ascending Numerical Order
    By Yizi in forum C Programming
    Replies: 7
    Last Post: 12-08-2009, 07:05 PM
  3. 3 integers in ascending order
    By hobilla in forum C Programming
    Replies: 7
    Last Post: 02-14-2009, 01:01 PM
  4. array in ascending order
    By galmca in forum C Programming
    Replies: 4
    Last Post: 10-25-2004, 11:44 AM
  5. 3 integers in ascending order-help
    By Allison23NY in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2001, 02:24 AM

Tags for this Thread