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

This is a discussion on Simple Function but......I NEED HELP within the C Programming forums, part of the General Programming Boards category; Hi, I am a beginning Programmer. I do not yet know pointers and was wondering if there was a way ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Post 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

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Post your attemped code to solve the problem. Then we will help.

    Mr. C.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Post 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]

  5. #5
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    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.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    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.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Cool 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);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21