Thread: ***trouble with a loop***

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    ***trouble with a loop***

    i'm having the most difficulty writing this program (perhaps it's due to my week long hiatus from programming to try to set up a dual boot config)... here is the question and my code pasted below:

    5. Exercise 3 in Chapter 4 asked you to write a program that displays a two-digit number with its digits reversed. Generalize the program so that the number can have one, two, three, or more digits. H*I*N*T: Use a 'do' loop that repeatedly divides the number by 10, stopping when it reaches 0.

    here is the code from Exercise 3 (and the code for #5 below that):

    EX 3 -

    /* SEC4.1 EX3 */
    /* DATE: 07-21-02 */

    #include <stdio.h>

    int main()
    {

    int one, two;


    printf("Enter a 2-digit number: ");
    scanf("%d", &one);

    two = one % 10;
    one = one / 10;

    printf("The reversal is: %d%d\n",two, one);

    return 0;
    }

    --------------------------------------------
    # 5 -

    /* SEC6.2 EX5 */
    /* DATE: 08-01-02 */

    #include <stdio.h>

    int main()
    {

    int m, n, o, p, q ;


    printf("Enter a number: ");
    scanf("%d", &n);

    do {
    m = n % 10;
    o = n / 10;
    p = o % 10;
    q = o / 10;
    printf("The reversal is: %d%d\n", m, p, q);
    break;
    } while (n > 0);



    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Consider the following (since I am not going to write your homework for you):

    Code:
    number = 1234
    
    do
        display number % 10
        number = number / 10
    while number > 0
    Oh. Wait. That is all your homework needs...

    [edit]
    Let me expand this a bit. Programming is just a series of logical steps. All you have to do is break your program down into chunks. Think of it as writing an outline of a report.

    First, you have an overview of what you want. From there, you break it down into steps to get there. Then, as many times as you need, you break each step down into smaller steps:

    Overview: "A program that takes a number and displays it in revers."

    Steps:
    1) Ask the user for a number.
    2) Get a number from the user.
    3) Display it in revers.

    Assuming you know how to do the first two, you have to figure out how to do the last one.

    Overview: "Display a number in reverse order."


    Steps:
    1) Take the last diget, and display it.
    2) Repeat until we get to the first number.

    In mathmatics, how do you get the last digit? Well, you can divide by a number and get the remainder in standard division. In C, we have a modulus operator to do just that.

    Thus, to get rid of the last digit, we can divide by 10.

    See:

    100 / 10 = 10
    10 / 10 = 1

    And so on.

    Thus:

    123 / 10 / 12, remainder 3.

    So:

    123 % 10 = 3

    Repeat until you have nothing left to divide.
    [/edit]

    Quzah.
    Last edited by quzah; 08-01-2002 at 09:47 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Come on JohnMayer, 42 posts and ya still forgetting the code tags! Please use them next time.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Come on JohnMayer, 42 posts and ya still forgetting the code tags!
    Hehe, you're like I am with void main.

    -Prelude
    My best code is written with the delete key.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Hehe, you're like I am with void main.
    Yeah, well, if we say it often enough........
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Yeah, well, if we say it often enough........
    One would think so, but it doesn't seem to be helping too much.

    -Prelude
    My best code is written with the delete key.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >One would think so, but it doesn't seem to be helping too much.
    Don't ever give up though... don't let them grind you down. (not that I need to tell *you* that!)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    >Come on JohnMayer, 42 posts and ya still forgetting the code tags! Please use them next time.

    lol, well i'm a beginner to C programming (i only started about a month and a half ago... i got my programming book (that 'll be using this fall in college), but it's a little difficult at times when there's no lecture, no notes, and no prof

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >well i'm a beginner to C programming
    just to ensure there's no confusion here, the code tags I spoke of are specific to this forum. They help keep the format of the code as it is in you editor.

    Use this next time you post code:
    [code]
    /* Your code here */
    [/code]
    Last edited by Hammer; 08-05-2002 at 07:14 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    oooooh! thanks for the update (i've been posting codes for over 1 month and you are the 1st to correct me~!

Popular pages Recent additions subscribe to a feed