Hi all,

I got a problem.I got to make a program that sorts the digits inside a number given by the user, but I can't use an array. I know that the easiest way is with the bubble sort but I can't seem to make a switch between two digits inside the number,let's say you need to change the position between the digits without using swap function. Can anyone give me some pointers? Thanks in advanced.

First be as precise in you question as you can. For example: If given the int 15287 I am supposed to arrange the digits in ascending order to get 12578 without using an array. How would I do that?

Bubble sorts work on arrays.

Can you use a list or a set?

How about writing your own swap funciton instead of using a prewritten one?

Couple of options.

1) Read it into a string and work from there
2) Split the digits and work from there
- to split the digits you can use the divide and modulus operator

And I am guessing your avoiding arrays because either your assignment says you cannot use them or the more probable reason is that you don't know how to use them well. If so then you need to seriously reconsider learning them and redoing this problem using array. BTW if you use method 1 above then internally your still using an array.

first of all thanks for your fast answer and support,

@Lerner, yes you are right i need to sort in an ascending order without using arrays.And i can also make my own swap function.I can't use list or set either

@firstPerson, the second method is what i need, also i've learned array, i've done it with arrays but the teacher said that there is an other method without arrays and it's too tricky for us and i want to prove him wrong.

It's more of a logical problem that an syntax problem.

I thought of using the power of 10 to swap.So let's say pow(10,1)-first digit, pow(10,2) second digit etc. and using the modulo will give you the digits.

How do i know when to stop?And where do i store the new number?

OK just done it i think.
Use a while loop for the length of the number.
Then sort the number checking each digit against the next (bubble sort) after separating them into 2 variables using / % and pow. If they need swapped remove the variables multiplied by the corresponding pow - then add by multiplying the variables by the correct pow and they should have swapped places. Using a nested loop to go right through the number.
Sorry i don't think i've explained it very well.

EDIT
Just ran it again and zeros are an issue. Doesn't work with them.
That can be fixed by checking the length at the end against the length at the start and outputting the appropriate number of zeros before the number.(UGLY i know)
Except leading zeros but who uses those.

I'm guessing that the easiest solution involves recursion, and specifically that it is closely related to the integer-to-string algorithm: you accumulate the changes on the stack and rejoin them as the function calls return.

@frogboy77 i can't seem to grasp this

Then sort the number checking each digit against the next (bubble sort) after separating them into 2 variables using / % and pow. If they need swapped remove the variables multiplied by the corresponding pow - then add by multiplying the variables by the correct pow and they should have swapped places

int lenght;
  while(true){
        int z = 78;
        if ( z > 0){
            z = n%10;
            n = n/10;
            lenght++;
        }
        else{
            break;
        }
    }
    for (int i = 0; i < lenght; i++ ){
        int n1 = n % pow( 10,i );
        int n2 = n / pow( 10, i );
          if ( n1 > n2){
                cout<<"I'm stuck";
          }
    }

ok i can't seem to understand the swapping bit, i mean how do you change the pow bit, can you like write me that. Just one or two lines and don't put it in any loop, just write them outside the syntax.I want to make it as much as i can myself.

ok so number =3145
you need a nested loop so you can check pos1vpos2 pos1vpos3 pos1vpos4 pos2vpos3 etc...(or work back from pos4vpos3 doesn't matter)
(google bubble sort if you need to)
then you need to isolate the 2 numbers into variables, say a and b
i started my outer loop with i=length-1 and decremented i.
so a=num/pow(10.0,i)
a=(int)a%10
the 2 lines are because pow requires pow(double,int). got some compiler warnings with this.(I told you it was ugly)
inner loop starts at j=i-1
so b= same as above but with j not i.
now you have the 2 numbers to compare
if they need to be swapped then take them away from number ie number-=3000,number-=100 then add back in number+=1000, number+=300 (leave that code to you)

commented: well written and usefull +1

Set up a while loop that finds the largest digit in num and creates sorted:

save num for later in numSave; set smallest to 10
Loop until num = 0
   get last digit (%)
   remove last digit (/)
   if digit > smallest, replace smallest

Now remove that digit:

copy numSave to num
zero numSave
loop until num = 0
   get last digit (%)
   remove last digit (/)
   if digit != smallest,  multipy numSave by 10 and add digit

Add smallest to sorted (properly).
If numSave != 0 Loop back to do this all again

I think you want to look at this.

Ok so I got this far, but it doesn't change the number one bit.

#include<iostream>
#include<cmath>

using namespace std;

int main(){
      
      int n, lenght = 0;
      cout<<"Write a number: ";
      cin>>n;
      int z = n;
      while ( z != 0 ){
            z = z/10;
            lenght++;
      }
      for ( int i = lenght -1; i > 0; i-- ){
            int a = n / pow(10.0, i );
            a = (int)a%10;
            for ( int j = i-1; j >= 0; j-- ){
                  int b = n/pow(10.0, j );
                  b = (int)b%10;
                  if ( b > a ) {
                        n = n - a*pow(10.0,i );
                        n = n + b*pow(10.0,i );
                        n = n - b*pow(10.0,j );
                        n = n + a*pow(10.0,j );
                  }
            }
      }
      cout<<n;
      int exit;
      cin>>exit;
      return 0;
}

this is the right answe, thanks frogboy77 and everyone for your support.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int num,len=0,len2=0;

cin>>num;
int cnum=num;

while(cnum!=0){cnum/=10;++len;}

for(int i=len-1;i>=1;--i)
{
for(int j=i-1;j>=0;--j)
{
int a,b;
a=num/pow(10.0,i);
a=(int)a%10;
b=num/pow(10.0,j);
b=(int)b%10;
if(a>b)
{
num-=(pow(10.0,i)*a);
num-=(pow(10.0,j)*b);
num+=(pow(10.0,i)*b);
num+=(pow(10.0,j)*a);
} 
}
}

cnum=num;
while(cnum!=0){cnum/=10;++len2;}

for(int z=(len-len2);z>0;--z) cout<<"0";

cout<<num<<endl;

return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.