# Simple Function but......I NEED HELP

• 09-29-2002
justin69enoch
Simple Function but......I NEED HELP
Hi, I am a beginning Programmer. I do not yet know pointers and was wondering if there was a way to create a loop or what not to help out in my program. I want to put this function in a file by itself, yet I don't even know where to begin. My problem: input: read in a series of intergers consisting of up to nine digits. The function will break the numbers apart and list them in reverse. Example: the numbers 123456789 are entered..
9 8 7
6 5 4
3 2 1

is outputed. How can I do this ??? any help would be greatly appreciated. I think you use 123456789 / 10 and 123456789 %10 but am not sure how to write the function. Thanks
• 09-29-2002
Mister C
Post your attemped code to solve the problem. Then we will help.

Mr. C.
• 09-29-2002
Prelude
First, you should read all of the values into an integer array, now you can work with it easily. To print the values in reverse, simply obtain the size of the array, a popular method is this:

size_t size = sizeof array / sizeof array[0];

This divides the total bytes that the array occupies in memory by the size of each item it holds, the result is the number of items. Now that you have the size you can loop and print each item at array[size] and then decrement size until it reaches 0. Be careful about going below zero with the size_t type, your loop will go on forever if you do. ;)

The formatting of three items and then a newline is a bit more complicated, you can either maintain a counter variable that counts to 3 and resets after you print a newline, or you can use size as the counter. By using size as the counter, check if size % 3 is equal to 0, if it is then you can print a newline.

I'm curious to see how close to my implementation you can get with just my explanation. Be sure to post your function when you get it working. :)

-Prelude
• 09-29-2002
justin69enoch
HERE's MY CODE that does'nt work... anyone...
Thanks for the matrix trick. I cannot use it though. I haven't got a clue what it is. The following code sorta does it but it doesn't work right..... does this help? ? ? Thanks again for the help thus far.
____________________________

Code:

```int main(void){ int numtosplit,counter,currdig; printf("\nEnter a positive integer (up to 9 digits in length);"); printf("\nhalt with a non-positive integer:\n"); scanf("%d",&numtosplit); /* Start outer loop:  makes program repeat */ while(numtosplit>0)  {     counter=3;         /* Inner Loop */         while(numtosplit >0){             if(counter ==3)  {             counter=0;             printf("\n              ");             }         currdig=numtosplit%10;         numtosplit=numtosplit/10;         counter++;         return currdig;         }     /* Get new number to split */  printf("\n\n"); printf("\nEnter a positive integer (up to 9 digits in length);"); printf("\nhalt with a non-positive integer:\n"); scanf("%d",&numtosplit); } return (0); }```
______________

[I] Code tags added by Kermi3]/I]
• 09-29-2002
kermi3
Code Tags
I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you ;)

For example:

Without code tags:

for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}

With Code Tags:
Code:

```for(int i=0;i<5;i++) {     cout << "This code is easy to read"; }```
This is of course a basic example...more complicated code is even easier to read with code tags than without.

I've added code tags for you this time. They can be added by putting [ code ] at the beginning of your code and [ /code ] at the end, only without the spaces. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.
• 09-29-2002
Prelude
Oh, you mean you want to reverse a number with nine digits? I thought you meant reverse nine separate numbers. Well, this is easier, simply loop until the number you read is 0 and print the N % 10, then divide by 10:
Code:

```#include <stdio.h> int main ( void ) {   long val = 123456789L;   printf ( "Before:\t%ld\n", val );   printf ( "After:\t" );   while ( val != 0 ) {     printf ( "%ld", val % 10 );     val /= 10;   }   printf ( "\n" );   return 0; }```
-Prelude
• 09-29-2002
justin69enoch
Thanks, Guys
Is there anyway to fix my code so it will work? I have worked on it for a while. Thanks for the heads up on the code tags and for the code. I am a newbie to this programming thing so please bare with me.
• 09-29-2002
justin69enoch
New Working/ updated code
Thanks guys soooo much. I got it to work.. It's AWESOME!!! I would like to thank everyone to help. Here is the code.... I left out a Stupid print statement.... LOL :)

Code:

```    #include <stdio.h>     int main(void)  {     int split_num,count,digit;     printf("\nEnter a positive integer (up to 9 digits in length);");     printf("\nhalt with a non-positive integer:\n");     scanf("%d",&split_num);     /* Start outer loop:  makes program repeat */         while(split_num>0)  {         count=3;         /* Inner Loop */         while(split_num >0){             if(count==3)  {             count=0;             printf("\n        ");             }         digit=split_num%10;         split_num=split_num/10;         printf("%d    ",digit);         count++;                 }     /* Get new number to split */  printf("\n\n"); printf("\nEnter a positive integer (up to 9 digits in length);"); printf("\nhalt with a non-positive integer:\n"); scanf("%d",&split_num); } return (0); }```