Thread: Number sorting

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    24

    Number sorting

    Hi all,
    I wrote a program for sorting a set of numbers but it not working.Please check.

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
    
    long int count1,count2,copy,a[5]; clrscr(); scanf("%ld", &a[5]); count1=1; for(count1=1,count2=2;count2<=5;count1++,count2++) {
    a[count1]=copy; if(a[count1]>a[count2]) {
    a[count1]=a[count2]; a[count2]=copy;
    }
    } printf("%ld", a[5]); getch(); return 0;
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes, the algorithm is completely wrong, I suggest you google sorting algorithms and start with an easy one like selection sort.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > scanf("%ld", &a[5]);
    This doesn't input 5 integers to your array.

    > printf("%ld", a[5]);
    This doesn't print them either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well there too many bugs to point out. But let me just point out one for a start. The way you read the input from the user is not right, you need to get five values of the users to even sort. The way you read them from the user is using either while or gor loops. Latter method shown bellow

    Code:
    int i;
    for(i=0;i<5;i++)
       scanf("%d", &a[i]);
    I'll leave you to figure out the while loop usage. If you can get this stage past. Perhaps we could start looking into sorting.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. C number sorting array help
    By manfun in forum C Programming
    Replies: 3
    Last Post: 03-09-2009, 06:04 AM
  3. Number sorting program
    By switchback in forum C Programming
    Replies: 7
    Last Post: 07-25-2008, 05:22 PM
  4. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  5. sorting by name or number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 12-07-2001, 01:36 PM