# Converting decimals to individuals

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-01-2009
clag
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!
• 10-01-2009
tabstop
Since you have a loop, use it to generate one character at a time until finished.
• 10-01-2009
clag
something of this sense?

Code:

```    while(digit_count == COUNT)     {         digit = number % 10 + '0';         number = number / 10 + '0';         number = exit_loop;         exit_loop = COUNT;     }```
• 10-01-2009
tabstop
Except presumably you should do something with digit other than just compute it. Like, say, print it to the file.
• 10-01-2009
clag
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;     }```
• 10-01-2009
tabstop
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.
• 10-01-2009
clag
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?
• 10-01-2009
tabstop
Recursion is a fairly obvious way to do it. digit_count is not your number.
• 10-01-2009
clag
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-01-2009
tabstop
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).
• 10-01-2009
clag
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;     }```
• 10-02-2009
clag
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?
• 10-02-2009
tabstop
Have you even tried to compile that code? It will tell you several things that aren't even a little bit correct about it.
• 10-02-2009
clag
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?
• 10-02-2009
tabstop
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).
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last