Thread: Sorting the numbers

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    Sorting the numbers

    Hello!! Its been two weeks since I started to study programming. I need to program that asks the user to input three numbers, and then outputs the numbers from the largest to the smallest. For example, if the input is 23 5 15, the output is 23 15 5.

    Should I use bubble sort or qsort? I dont know where to begin. I need some friendly advices.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Qsort is overkill for sorting a very small number of items. Not the fastest sorter either.

    Here's an easy sorter (not quite bubble sort, but like it, and just a tad faster). To use it, you should have an array of int's with your numbers in a[0], a[1], and a[2], and #define SIZE 3
    just below the include file listing in your program

    Code:
    for(i=0; i<SIZE-1;i++) {
      for(j=i+1;j<SIZE; j++) {
        if(a[i] > a[j]) {
          temp = a[i];
          a[i] = a[j];
          a[j] = temp;
        }
      }
    }

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    Whats the error in the code below?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 3
    
    int intcmp(const void *v1, const void *v2);
     
    main()
    {
         int i, arr[MAX];
         
       
         //Asks the user to input three interger numbers
         
         printf("Enter %d integer values; press Enter after each.\n", MAX);
         
         for (i = 0; i < MAX; i++)
             scanf("%d", &arr[i]);
        
             
         //Sort the array into ascending order.
    
         qsort(arr, MAX, sizeof(arr[1]), intcmp);
         
         //Display the sorted array.
    
         for (i = 0; i < MAX; i++)
             printf("\n%d", i, arr[i]);
         
         
    }
    
    
    int intcmp(const void *v1, const void *v2)
    {
    	int cmpvalue1, cmpvalue2;
    
    	cmpvalue1 = *(int*)v1;
    	cmpvalue2 = *(int*)v2;
    
    	return cmpvalue1 - cmpvalue2;
    	
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by minyoungan
    Whats the error in the code below?
    Instead of just dumping code and asking what is the error, tell us why you think there is an error in the code, e.g., what is the actual output versus what is the expected output. Furthermore, remember to compile with warnings turned on, e.g., my compiler informed me that this line is problematic, and it is:
    Code:
    printf("\n%d", i, arr[i]);
    Quote Originally Posted by Adak
    What is the return when cmpvalue2 is greater than cmpvalue1? Will qsort() handle that ok?
    Yes, though there are potential problems due to overflow.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  2. Sorting numbers in a file
    By pxleyes in forum C Programming
    Replies: 19
    Last Post: 04-15-2004, 06:17 AM
  3. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 05:55 PM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM