do..while statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tricubix
    New Member
    • Sep 2007
    • 6

    do..while statement

    Hi friends, I'm a newbie into C/C++ world. So i hope you can help me with this question. I need to do a program using do..while statement in C languages to produce the following output


    0 1
    1 4
    2 16
    3 64
    4 256
    5 1024
    6 4096

    i have done something with this program and i only manage to get the following output

    0 0
    1 1
    2 4
    5 25

    using this program :
    [code=c]
    #include<stdio. h>
    int x=0;
    int x2, x;
    void main()
    {
    do
    {

    x2 = x;
    x = x*x;
    printf("\n%d %d", x2, x);
    x += 1;
    } while (x<=21);

    getch();
    }
    [/code]
    can anyone help me?
    Last edited by sicarie; Sep 27 '07, 01:00 PM. Reason: Code tags
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by tricubix
    Hi friends, I'm a newbie into C/C++ world. So i hope you can help me with this question. I need to do a program using do..while statement in C languages to produce the following output


    0 1
    1 4
    2 16
    3 64
    4 256
    5 1024
    6 4096

    i have done something with this program and i only manage to get the following output

    0 0
    1 1
    2 4
    5 25

    using this program :

    #include<stdio. h>
    int x=0;
    int x2, x;
    void main()
    {
    do
    {

    x2 = x;
    x = x*x;
    printf("\n%d %d", x2, x);
    x += 1;
    } while (x<=21);

    getch();
    }

    can anyone help me?
    Ok, so basically you need a variable to hold the left side (1, 2, 3 ...) and a variable to hold the right side (which I assume you multiple by 4 (1, 4, 16 ...)):
    [code=cpp]
    int count = 0; // left
    int x = 1; // right

    do {
    printf("%d %d\n", count, x);
    // now do whatever you need to get the next value for 'count' and 'x'
    } while (count <= 21);
    [/code]

    Comment

    • PieCook
      New Member
      • Jul 2007
      • 9

      #3
      Originally posted by tricubix
      do
      {
      x2 = x;
      x = x*x;
      printf("\n%d %d", x2, x);
      x += 1;
      } while (x<=21);

      One problem I noticed is that you have x = x*x inside your loop. In most cases, you should not modify the loop counter except while incrementing it, although there are a few peculiar exceptions, of course. I assume you meant to say x2 = x*x instead.

      Now if you notice the mathematical relationship between each number in the left and the corresponding value on the right, you'll notice that they're not squares (2*2 != 16, 3*3 !=64, etc.), so x*x will not work.

      Any computer scientist should be familiar with the powers of 2 (1,2,4,8,16,32, 64,128,256,512, 1024 at the minimum). It appears that all the required numbers in the right column are in our list (if you extend it to 1024,2048,4096) . So in your code, you should have some powers of 2. Don't forget to include the Math library.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        And you use getch() without declaring its library. Either remove getch() or run the risk of your teacher's compiler not being able to run your code and include conio.h.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Actually this doesn't look like powers of 2:

          1, 4, 16, 64, 256, 1024, 4096...

          Certainly, these are numbers that are powers of 2, but I'm not seeing 2, 8, 32, 128...etc. So that can't be the answer. What other patterns do you see?

          Comment

          • tricubix
            New Member
            • Sep 2007
            • 6

            #6
            Originally posted by ilikepython
            Ok, so basically you need a variable to hold the left side (1, 2, 3 ...) and a variable to hold the right side (which I assume you multiple by 4 (1, 4, 16 ...)):
            [code=cpp]
            int count = 0; // left
            int x = 1; // right

            do {
            printf("%d %d\n", count, x);
            // now do whatever you need to get the next value for 'count' and 'x'
            } while (count <= 21);
            [/code]
            hi ilikepython, your note look great, but i still need your favour. As you provide me the coding i can't see what i have to put under the printf?? Really need your help as i really2 confuse right now. TQ in advanced

            Comment

            • tricubix
              New Member
              • Sep 2007
              • 6

              #7
              Originally posted by Ganon11
              Actually this doesn't look like powers of 2:

              1, 4, 16, 64, 256, 1024, 4096...

              Certainly, these are numbers that are powers of 2, but I'm not seeing 2, 8, 32, 128...etc. So that can't be the answer. What other patterns do you see?
              what can i say that... 4 power to the left no.

              4 power to 0 (left no) equal to 1
              4 power to 1 (left no) equal to 4
              4 power to 2 (left no) equal to 16
              bla..bla..bla.. .ect..

              i hope you can understand that..TQ

              Comment

              • Studlyami
                Recognized Expert Contributor
                • Sep 2007
                • 464

                #8
                He gave you the answer
                to hold the right side (which I assume you multiple by 4 (1, 4, 16 ...)
                so take your left value and add one to it (x++)
                take the right value and multiply it by 4.

                also you initialize x twice.
                int x=0;
                int x2, x; //could reinitialize x so x would no longer be zero

                the number on the left and the number on the right isn't always x^4. Are you trying to find a relation between the number on the left to the one on the right? If so, is that even needed. Just find the pattern of the left, find the pattern of the right and keep track of the two separately.

                Comment

                Working...