Thread: Converting decimals to individuals

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    32

    Converting decimals to individuals

    long integer number provided by the user and displays it to the filestream stream as an integer. This requires separating the number into individual characters, converting each character into its digit value, and then displaying the resul

    I'm trying to write a decout function that seperates the given long number to individual characters and the converting each character into its digit value and displaying the result. I got the algorithm done. here's what I have
    Code:
    void decout (unsigned long number, FILE * stream) 
    {
        int last_digit, second_digit, first_digit, exit_loop, COUNT;
        long digit_count = 0;
    
    
        while(digit_count == COUNT)
        {
            last_digit = number % 10 + '0'; //last digit
            //add number to array
            last_digit = number / 10;
            second_digit = last_digit % 10 +'0' //second digit
            //add number to array
            first_digit = last_digit / 10;
            first_digit % 10 = exit_loop;
            exit_loop = COUNT;
        }
        
            
    }
    digits, is declared as const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; by the way.

    I know there is a flaw to the algorithm but I feel like it's just a constant repetition over and over. How will I just summarize the algorithm so it works for all cases and not just three digits as my algorithm does?

    Thank you so much!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since you have a loop, use it to generate one character at a time until finished.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    something of this sense?

    Code:
        while(digit_count == COUNT)
        {
            digit = number % 10 + '0';
            number = number / 10 + '0';
            number = exit_loop;
            exit_loop = COUNT;
        }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Except presumably you should do something with digit other than just compute it. Like, say, print it to the file.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    i think i still slightly have a trouble understanding the proper algorithm. is there a pseudo-code I can see? I'm fairly proficient in Java but new to C and I might overlooking a c method..say fputc?

    Code:
        while(digit_count == COUNT)
        {
            digit = number % 10 + '0';
            fputc(stream,digit);        
            number = number / 10 + '0';
            number = exit_loop;
            exit_loop = COUNT;
        }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What you have is mostly fine. Your exit condition is not quite right (your exit condition is when you're out of digits, i.e. when your number is zero). Also you may have noticed that you are printing out your number backwards.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Code:
        while(digit_count == 0)
        {
            digit = number % 10 + '0';
            fputc(stream,digit);        
            number = number / 10 + '0';
            number = exit_loop;
            if(exit_loop == 0)
                digit_count = 0;
        }
    oh that was going to my question. My idea is to store them in an array and I have several ways to implement this..but adding them backwards and reading them from the back seems error-prone. But any idea if there is an recursive way to do it? Or a way to improve my array method?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Recursion is a fairly obvious way to do it. digit_count is not your number.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    ohh how will I do it recursively? nothing seems to pop-up. I don't think I should use it since i'm not comfortable with it? How about using arrays? store them and read them backwards?

    Code:
        while(number == exit_loop)
        {
            digit = number % 10 + '0';
            fputc(stream,digit);        
            number = number / 10 + '0';
            number = exit_loop;
            if(exit_loop == 0)
                break;
        }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to use an array, then yes you will have to store it and read it backwards (and also in that case you will need your digit_count so you know where to start printing from).

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    something of this sort?

    Code:
        int digit
        long digit_count = 0;
        int array[] = 50;
    
        while(number == exit_loop)
        {
            digit = number % 10 + '0';
            fputc(stream,digit);
            array[50] = digit;  
            number = number / 10 + '0';
            array[49--] = number
            number = exit_loop;
            if(exit_loop == 0)
                break;
        }

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Code:
        int digit
        long digit_count = 0;
        int array[] = 50;
    
        while(number == exit_loop)
        {
            digit = number % 10 + '0';
            fputc(stream,digit);
            array[50] = digit;  
            number = number / 10 + '0';
            array[49--] = number
            number = exit_loop;
            if(exit_loop == 0)
                break;
        }
    is this the correct exit method?
    and how about adding the arrays?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Have you even tried to compile that code? It will tell you several things that aren't even a little bit correct about it.

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    the array is incorrect ahh..

    i drew out a pseudo-code to try and figure it out but it seems like there will always be an error if there is a zero and it will always read it incorrectly.

    ignore all the array parts, I'll try to start over..but im curious of the recursive method? how will I implement that?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Recursion is pretty trivial. You need a base case as to when not to continue (in this case, single-digit number). You peel off the left digit, and then call the function again first and print the number later (so as to get the numbers in the right order).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-20-2009, 07:39 AM
  2. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 04:13 PM
  3. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  4. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM