0

I need to sort the digits of a multiple digit integer (eg. 587328) without using an array on C. I've tried to make it but it doesn't work and I'm not really sure why because there's apparently no errors.

int main(){
    int n, dig, temp;
    printf("\nPlease ingress a number: ");
    scanf("%d", &n);
    for (dig=0; dig<9; dig++){
        for(temp=n; temp>10; temp/=10){
            if(temp%10==dig){
                printf("%d", dig);
                printf("\n");
            }
        }
    }
    return 0;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
Juana
  • 3
  • 1
  • 7
    What is "sort an integer"? Please give us some examples of input and expected output. – MikeCAT Sep 22 '20 at 14:03
  • 1
    Please elaborate more on the problem; it makes no sense to sort a single number, perhaps there is some misunderstanding. Please give an example input related to an example output. – Codor Sep 22 '20 at 14:04
  • 2
    First thing I noticed (without running the code): the loop `for (dig=0; dig<9; dig++)` will miss digit `9` – pmg Sep 22 '20 at 14:04
  • 2
    Also the loop `for(temp=n; temp>10; temp/=10)` will miss the top digit. – MikeCAT Sep 22 '20 at 14:05
  • 3
    It appears you may want to sort the DIGITS of a number? For example: `573` sorted would be `357`? Please clarify if that is what you intend? – abelenky Sep 22 '20 at 14:05
  • Do you mean a series of space or comma delimited numbers: `4 5 7` or `4,5,7`, or just a string of digits? `457`? – ryyker Sep 22 '20 at 14:07
  • Not all C variables expressed using array notation, are arrays. eg. this: `char *a = malloc(10*sizeof(*a));` creates a memory location on the heap comprise of 10 contiguous `char` sized locations, but cannot be accurately referred to as a `C` array. while `char a[10];` does the same, but it _is_ called an array – ryyker Sep 22 '20 at 14:23
  • 1
    I guess `dig<9` should be `dig<=9` (or `dig<10`), and `temp>10` should be `temp>0` (or just `temp`). This won't work for non-positive numbers. – Ian Abbott Sep 22 '20 at 15:27
  • @Codor - You edited this yesterday. And I just made some additional edits to clarify intent. ("_it makes no sense to sort a single number,_..."). Would you consider a vote to re-open? – ryyker Sep 23 '20 at 15:03
  • @Andreas Wenzel - Details and clarifications of the stated problem have been edited in. Will you please consider re-evaluating the post and vote to re-open? – ryyker Sep 23 '20 at 19:04
  • @Orel - Details and clarifications of the stated problem have been edited in. Will you please consider re-evaluating the post and vote to re-open? – ryyker Sep 23 '20 at 19:04
  • @Luis Colorado - Details and clarifications of the stated problem have been edited in. Will you please consider re-evaluating the post and vote to re-open? – ryyker Sep 23 '20 at 19:05

1 Answers1

2

I need to sort an integer (A number tbh) without using an array on C

Method 1:
I suppose there are other, more elegant methods but the following method does follow the constraint of not using arrays or any kind of purposefully created contiguous blocks of memory to store elements, only individually created variables.

Without the use of an array, or any other form contiguous memory it becomes increasingly messy to assign, organize and sort a collection of individual values. (See case 10: below for illustration) The larger the collection, the messier the task becomes. The following method(s) is limited to <=10 digits.

The following method (limited to 10 digits) uses a combination of the following:

  • pre-defined set of 10 int variables
  • switch() within a for() loop to separate and store digits
  • Simple sorting network and a SWAP macro in a switch()

Output of following:

6539345654
sorted
3344555669
        int a = -1;
        int b = -1;
        int c = -1;
        int d = -1;
        int e = -1;
        int f = -1;
        int g = -1;
        int h = -1;
        int i = -1;
        int j = -1;

        int tmp;
        long long num = 4565439356;//input value for illustration

    int main(void)
    {
        char buf[11] = {0};
        sprintf(buf, "%lld", num);
        int len = strlen(buf);

        //separate and store digits
        //for(int x=0; num; num /= 10, ++x)//reads digits from LSD to MSD
        for(int x=9; num; num /= 10, --x)//reads digits from MSD to LSD

        {
            switch(x)  {
                case 0:
                    a = num%10;
                    break;
                case 1:
                    b = num%10;
                    break;
                case 2:
                    c = num%10;
                    break;
                case 3:
                    d = num%10;
                    break;
                case 4:
                    e = num%10;
                    break;
                case 5:
                    f = num%10;
                    break;
                case 6:
                    g = num%10;
                    break;
                case 7:
                    h = num%10;
                    break;
                case 8:
                    i = num%10;
                    break;
                case 9:
                    j = num%10;
                    break;
            }
        }
        //sort appropriate number of digits
         switch(len)  {
            case 1:
                a;
                break;
            case 2:
                printf("%d%d\n", a,b);
                SORT(a,b);
                printf("sorted\n%d%d\n", a,b);
                break;
            case 3:
                printf("%d%d%d\n", a,b,c);
                SORT(a,b);
                SORT(a,c);            
                SORT(b,c);
                printf("sorted:\n%d%d%d\n", a,b,c);
                break;
            case 4:
                printf("%d%d%d%d\n", a,b,c,d);
                SORT(a,b);
                SORT(a,c);                    
                SORT(a,d);          
                SORT(b,c); 
                SORT(b,d); 
                SORT(c,d); 
                printf("sorted:\n%d%d%d%d\n", a,b,c,d);
                break;
            case 5:
                printf("%d%d%d%d%d\n", a,b,c,d,e);
                SORT(a,b);
                SORT(a,c);                    
                SORT(a,d);          
                SORT(a,e);                      
                SORT(b,c); 
                SORT(b,d); 
                SORT(b,e); 
                SORT(c,d); 
                SORT(c,e);
                SORT(d,e); 
                printf("sorted:\n%d%d%d%d%d\n", a,b,c,d,e);
                break;
            case 6:
                printf("%d%d%d%d%d%d\n", a,b,c,d,e,f);
                SORT(a,b);
                SORT(a,c);                    
                SORT(a,d);          
                SORT(a,e);                      
                SORT(a,f); 
                SORT(b,c); 
                SORT(b,d); 
                SORT(b,e); 
                SORT(b,f); 
                SORT(c,d); 
                SORT(c,e); 
                SORT(c,f); 
                SORT(d,e); 
                SORT(d,f); 
                SORT(e,f); 
                printf("sorted:\n%d%d%d%d%d%d\n", a,b,c,d,e,f);

                break;
            case 7:
                //and so on
                break;
            case 8:
                i = num%10;
                //and so on
            case 9:
                //and so on
                break;
            case 10:
                printf("%d%d%d%d%d%d%d%d%d%d\n", a,b,c,d,e,f,g,h,i,j);
                SORT(a,b);
                SORT(a,c);                    
                SORT(a,d);          
                SORT(a,e);                      
                SORT(a,f); 
                SORT(a,g);
                SORT(a,h);                    
                SORT(a,i);          
                SORT(a,j);                      
                SORT(b,c);                    
                SORT(b,d);          
                SORT(b,e);                      
                SORT(b,f); 
                SORT(b,g);
                SORT(b,h);                    
                SORT(b,i);          
                SORT(b,j);                      
                SORT(c,d);          
                SORT(c,e);                      
                SORT(c,f); 
                SORT(c,g);
                SORT(c,h);                    
                SORT(c,i);          
                SORT(c,j);                      
                SORT(d,e);                      
                SORT(d,f); 
                SORT(d,g);
                SORT(d,h);                    
                SORT(d,i);          
                SORT(d,j);                      
                SORT(e,f); 
                SORT(e,g);
                SORT(e,h);                    
                SORT(e,i);          
                SORT(e,j);                      
                SORT(f,g);
                SORT(f,h);                    
                SORT(f,i);          
                SORT(f,j);                      
                SORT(g,h);                    
                SORT(g,i);          
                SORT(g,j);                      
                SORT(h,i);          
                SORT(h,j);                      
                SORT(i,j);                      
                printf("sorted\n%d%d%d%d%d%d%d%d%d%d\n", a,b,c,d,e,f,g,h,i,j);
                break;
        }
        return 0;
    }



***EDIT - alternate method***  

Some words about the _array_ thing...  
Not all `C` variables expressed using array _notation_, are arrays. eg. this:   

    char *a = malloc(10*sizeof(*a)); 

creates a memory location in heap memory comprised of `10` contiguous `char`   
sized locations, and the areas of memory of `a` can be expressed as   

    a[0], ... a[9]  

But `a` is not a _C array_, it is a pointer. The declaration:

    char a[10];   

_is_ a C array, and with the exception of where it is stored (on the stack), does the same.  

More on C arrays:    

[Arrays ref 1](https://www.tutorialspoint.com/cprogramming/c_arrays.htm)    
[Arrays ref 2](https://www.cs.nmsu.edu/~rth/cs/cs271/notes/Pointers.html)       
[Arrays ref 3](https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1069897882&id=1073086407)     

The following uses pointers, not C Arrays.    

***Method 2***  

Output of following:  

    345654
    sorted:
    344556

Example code:   

    char *digits = "456543";
    void main(void)
    {
        
        int len = strlen(digits);
        int num = atoi(digits);
        char *elements = calloc(len, sizeof(*elements)); 
        if(elements)
        {
            for(int i=0; num; num /= 10, ++i)
            {
                *(elements +i) =(char)(num%10);
            }
        }
        for(int i = 0;i<len;i++)
        {
            printf("%d", *(elements + i));
        }
        printf("\nsorted\n");
        char *sorted = sort(elements, len);
        for(int i = 0;i<len;i++)
        {
            printf("%d", (char)(*(sorted + i)));
        }
        free(elements);
        
    }

    char * sort( char *numbers, int count )
    {
        int i, j;

        char temp;
        do{
            j = 0;  
            for (i = 0;i<count-1;i++){
                    if (*(numbers+i) > *(numbers+i+1)){//this was numbers[k], which was an error
                        j = 1;
                        temp = *(numbers+i);
                        *(numbers+i) = *(numbers+i+1);
                        *(numbers+i+1) = temp;
                    }
                }
        } while (j == 1);
        return numbers;
    }


ryyker
  • 22,849
  • 3
  • 43
  • 87