Thread: sorting four numbers

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    7

    sorting four numbers

    I'm trying to take a users input and break it up into four separate numbers, then take those numbers and arrange them from smallest to largest.

    So far I can't seem to get them working right, and i'm running out of ideas if anyone could give me an idea, or even a nudge in the right direction, I would greatly appreciate it.


    Code:
    # include <stdio.h>main ()
    {
    int inputVariables[4]; //where userinput goes after being broken up
    int arrangedValues [4];// the user values arranged lowest to highest
    int i;
    int j;
    int userInput;
    int remainders; //used to break up input
    int numberX; // users input arranged forwards
    int numberY; // users input arranged forwards
    
    
    
    
    printf ("Please state a number from 1000 - 9999, excluding numbers with four identical digits i.e 9999 \n");
    scanf (" %d",&userInput);
    
    
    remainders = userInput % 1000;
    inputVariables[0] = userInput / 1000;
    userInput = remainders;
    
    
    remainders = userInput % 100;
    inputVariables[1] = userInput / 100;
    userInput = remainders;
    
    
    remainders =userInput % 10;
    inputVariables[2] = userInput / 10;
    userInput = remainders;
    inputVariables[3] = remainders;
    
    
    printf ("%d \n",inputVariables[0]);
    printf ("%d \n",inputVariables[1]);
    printf ("%d \n",inputVariables[2]);
    printf ("%d \n",inputVariables[3]);
    printf ("%d \n", remainders);
    
    
    do 
    {
    
    
        for (i = 0 ; i < 4; i++)
        {
        for (j = 0 ; j < 4; j++)
            {
                if (inputVariables[i] < inputVariables[j])
                {
                    arrangedValues[i] = inputVariables[j];    
                }
                    
            }
        }
    }    
    while (arrangedValues[0] >= arrangedValues[1] >= arrangedValues[2] >= arrangedValues[3]);
    
    
    printf(" %d %d %d %d \n",arrangedValues [0],arrangedValues [1],arrangedValues [2],arrangedValues [3]);
    printf(" %d %d %d %d \n",arrangedValues [3],arrangedValues [2],arrangedValues [1],arrangedValues [0]);
    
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    I've been searching online and I found this code and it looks like it does what I want, and that I am not too far off, but I just can't wrap my head around how it works.

    I went through it using a pen and paper (using the numbers 5 2 7, to walk through it) and I get to seven and I just can't wrap my head around what happens next.

    Code:
    void BubbleSort(int a[], int array_size)
    {
         int i, j, temp;
         for (i = 0; i < (array_size - 1); ++i)
         {
              for (j = 0; j < array_size - 1 - i; ++j )
              {
                   if (a[j] > a[j+1])
                   {
                        temp = a[j+1];
                        a[j+1] = a[j];
                        a[j] = temp;
                   }
              }
         }
    }

    if someone could at least explain how this works I can understand and adapt my own coding.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Fear_Impurity View Post
    if someone could at least explain how this works I can understand and adapt my own coding.
    There's a distinct reason it's called "Bubble sort". While there are values with higher index and lower value ( or higher value, depending on the sorting order ), exchange them. This results in an extremely slow but rather trivial sort algorithm where, if each value is seen as a bubble, it rises slowly to its proper sorted place.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    Quote Originally Posted by Fear_Impurity View Post
    I've been searching online and I found this code and it looks like it does what I want, and that I am not too far off, but I just can't wrap my head around how it works.

    I went through it using a pen and paper (using the numbers 5 2 7, to walk through it) and I get to seven and I just can't wrap my head around what happens next.

    Code:
    void BubbleSort(int a[], int array_size)
    {
         int i, j, temp;
         for (i = 0; i < (array_size - 1); ++i)
         {
              for (j = 0; j < array_size - 1 - i; ++j )
              {
                   if (a[j] > a[j+1])
                   {
                        temp = a[j+1];
                        a[j+1] = a[j];
                        a[j] = temp;
                   }
              }
         }
    }

    if someone could at least explain how this works I can understand and adapt my own coding.
    I understand how the bubble sort concept works, but why does it require two loops, from going over the code, everything seems to happen only in the second loop.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Run it with only the second loop to see what happen. Try more than one inputs.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-10-2012, 01:14 AM
  2. sorting numbers
    By Sky_Daughter in forum C++ Programming
    Replies: 6
    Last Post: 10-15-2011, 06:36 PM
  3. Sorting the numbers
    By minyoungan in forum C Programming
    Replies: 3
    Last Post: 09-29-2010, 11:10 AM
  4. help with sorting numbers
    By artistunknown in forum C Programming
    Replies: 12
    Last Post: 02-07-2010, 12:00 AM
  5. Sorting Random Numbers
    By kid kash in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2002, 04:47 AM