ok lets say that we have the numbers 5,67 and 128 (the numbers are randomly entered)

i want in first loop read the last digit means 5,7,8
in the 2nd pass i want the tenths so will be 0,6,2
in the 3rd will be 0,0,1
and so on.

what i have think until now is this to count the digits

do
    {
        counter++;
        diviner=diviner*10;
    }while(number/diviner!=0);

    counter=counter-1;
    diviner=diviner/10;

i tried to do

digit=number%10

but this is for ones digit only in tenths fails.

i know how i can seperate the digits with modulus and divine
but i dont know how to fix it since the lenght of the number is unknown.

Recommended Answers

All 10 Replies

Couldn't you mod by 10 then 100 then 1000 and you would get the ones, tens, hundredths, thousandths that way?

You didn't post all of your code so it's hard to see where it all came from and what you are doing within your code prior to this loop.

accidental double post -.-

After you get the ones digit, divide the number by 10. Now the tens digit is ready for the %.

Couldn't you mod by 10 then 100 then 1000 and you would get the ones, tens, hundredths, thousandths that way?

No. 128 mod 100 = 28, not 2.

Hello ntrncx,
Lets take 67. 67 mod 10 will give you the ones place(7). Then divide 67 by 10. Since both are integers you will get the tenths place(6). Do this for 128 also and you will have the digits separated out.

what i try to do is the bucket sort exercise,

means that i have some numbers like 5,67,128 in an array and i have to copy them in a 2d array with criteria the last digit at first loop then read the 2d array and copy it back in the first array then at the 2nd loop put them back in 2d arrray with criteria the tens means 0,6,2,and so on

i know how to do the exercise if its 1 loop only :P

the only problem what i have is what i described.

int bucketsort(int a[],int[][size],int size)
{
    int number=size;
    int temp;
    int diviner=1;
    int counter=0;

    for (int x=0;x<size;x++)
    {
        number=a[x];

        do
        {
            counter++;
            diviner=diviner*10;
        }while(number/diviner!=0);

        for (counter;counter>0;counter--)
        {
            temp=number%10;
            cout<<temp<<endl;
            number=number/10;
        }
        counter=0;
        diviner=1;
        cout<<endl;
    }
}

what i did until now for the specific task is that.reads all the numbers i enter in the order i want,the problem is the 0 after the smallest numbers like 0 or 45.

now i see it again is totaly wrong, :(

i am genius :P hahaha i found out :P

i should really start using algorithms first and not going straight to compiler :D

any suggestions for improving are welcome

int bucketsort(int a[],int[][size],int size)
{
    int number=size;
    int temp;
    int diviner=1;
    int counter=0;
    int digits;
    int tempNumber;
    int max=0;
    
    //counts the max digits of the numbers
    for(int i=0;i<size;i++)
    {
        digits=0;
        tempNumber=a[i];
        do
        {
            digits++;
            diviner=diviner*10;
        }while(tempNumber/diviner!=0);

        if(max<digits)
            max=digits;

        diviner=1;
    }
    
    //take each number seperately first ones then tenths etc :D
    for (counter=0;counter<max;counter++)
    {
        for (int x=0;x<size;x++)
        {
            number=a[x];
            temp=number%10;
            a[x]=number/10;
          //  cout<<temp<<endl;
        }
    }
}

i am genius :P hahaha i found out :P

i should really start using algorithms first and not going straight to compiler :D

Yes you are a genius. Most students have to be told to do the desk work first. Now you see how important it is. Congrats!

:D
i agree but check my signature :P i have 1 small task to fix and i go to the next exercise :P
the only problem with that is i dont have the theoretical part of the lessons

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.