C++ Program to Calculate X^Y

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • way87
    New Member
    • Sep 2007
    • 8

    C++ Program to Calculate X^Y

    Question:
    Write a Program using Functions that calculates X^Y where both X and Y are doubles and X is positive. (Hints: Call the function pow() in <math.h>)
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    So what have you tried? Where are you stuck?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by way87
      Question:
      Write a Program using Functions that calculates X^Y where both X and Y are doubles and X is positive. (Hints: Call the function pow() in <math.h>)
      I don't consider that a hint; I consider that a complete spoiler. If you don't recognize
      a hint like that you wouldn't recognize a hint even if it were dancing on a piano
      wearing a purple tutu singing "I am a hint; I am a hint".

      kind regards,

      Jos

      Comment

      • way87
        New Member
        • Sep 2007
        • 8

        #4
        i jus started to do programming since last month and i still a newbie =(
        i cant solve the question ,it's hard for me =(

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

          Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

          Then when you are ready post a new question in this thread.

          Comment

          • way87
            New Member
            • Sep 2007
            • 8

            #6
            is it ok?
            [code=c]
            #include <stdio.h>
            int square(int y);

            int main (void)
            {
            int x;
            for (x = 0;x>=0;x++) {
            printf ("%d",square (x) );
            }

            printf("\n");

            return 0;
            }

            int square (int y)
            {
            return y*y;
            }[/code]
            Last edited by way87; Sep 25 '07, 07:55 PM. Reason: correction

            Comment

            • kreagan
              New Member
              • Aug 2007
              • 153

              #7
              Originally posted by way87
              Question:
              Write a Program using Functions that calculates X^Y where both X and Y are doubles and X is positive. (Hints: Call the function pow() in <math.h>)
              (define x x)
              (define y y)
              (pow x y)

              hehehehe.

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by way87
                tis is wat i hav done ,but it seems not correct =(
                [code=c]
                #include <stdio.h>
                int square(int y);

                int main (void)
                {
                int x;
                for (x = 1;x>=0;x++) {
                printf ("%d",square (x) );
                }

                printf("\n");

                return 0;
                }

                int square (int y)
                {
                return y*y;
                }[/code]
                Why aren't you using the easy method your teacher suggested? Why are you reinventing something that has already been created?

                Also, why use a for loop? Was that a requirement as well? FYI - you declare them as ints, not doubles.

                Comment

                • kreagan
                  New Member
                  • Aug 2007
                  • 153

                  #9
                  Originally posted by way87
                  tis is wat i hav done ,but it seems not correct =(
                  [code=c]
                  for (x = 1;x>=0;x++) {
                  printf ("%d",square (x) );
                  }
                  }[/code]
                  X is equal than 1, continue while X is greater than 0, after each iteration, increase X by 1.

                  I hope you see atleast 1 problem with this for loop.

                  Comment

                  • way87
                    New Member
                    • Sep 2007
                    • 8

                    #10
                    Originally posted by way87
                    is it ok?
                    [code=c]
                    #include <stdio.h>
                    int square(int y);

                    int main (void)
                    {
                    int x;
                    for (x = 0;x>=0;x++) {
                    printf ("%d",square (x) );
                    }

                    printf("\n");

                    return 0;
                    }

                    int square (int y)
                    {
                    return y*y;
                    }[/code]
                    i still have no idea about doubles =(

                    Comment

                    • sicarie
                      Recognized Expert Specialist
                      • Nov 2006
                      • 4677

                      #11
                      Originally posted by way87
                      i still have no idea about doubles =(


                      If you are having trouble with something that basic, we are going to have a difficult time helping you with your programs.

                      Ask to sit down and spend an hour with your teacher or TA going over variables, program structure, and control structures (loops), or you are going to have a LOT of trouble very quickly.

                      Comment

                      • way87
                        New Member
                        • Sep 2007
                        • 8

                        #12
                        [code=c]
                        #include <stdio.h>
                        #include <math.h>

                        int main ()
                        {
                        double x = 1;
                        double y = 1;
                        double result;

                        for ( x = 1; x <= 10; x++ ){
                        printf("%f\n",r esult = pow(x,y) );
                        }


                        return 0;
                        }
                        [/code]
                        i did it but the output is seems not correct:
                        1
                        2
                        3
                        4
                        5
                        6
                        7
                        8
                        9
                        10
                        Last edited by sicarie; Sep 25 '07, 08:26 PM. Reason: Code tags

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by way87
                          #include <stdio.h>
                          #include <math.h>

                          int main ()
                          {
                          double x = 1;
                          double y = 1;
                          double result;

                          for ( x = 1; x <= 10; x++ ){
                          printf("%f\n",r esult = pow(x,y) );
                          }


                          return 0;
                          }

                          i did it but the output is seems not correct:
                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          10
                          That output is perfectly correct because for every x != 0, x^1 == x.

                          kind regards,

                          Jos

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #14
                            Originally posted by way87
                            [code=c]
                            #include <stdio.h>
                            #include <math.h>

                            int main ()
                            {
                            double x = 1;
                            double y = 1;
                            double result;

                            for ( x = 1; x <= 10; x++ ){
                            printf("%f\n",r esult = pow(x,y) );
                            }


                            return 0;
                            }
                            [/code]
                            i did it but the output is seems not correct:
                            1
                            2
                            3
                            4
                            5
                            6
                            7
                            8
                            9
                            10
                            Okay, let's trace through one of these

                            x=1
                            y=1

                            for x =1
                            while x<10 // while 1< 10
                            print pow(x,y) // print x^y -> print 1^1(1)
                            x++ // x=2
                            while x< 10 // while 2 < 10
                            print pow (x,y) // print x^y -> print 2^1 (2)
                            etc...

                            What you are printing out is correct math. Set y to something else and you will see it.

                            Comment

                            • way87
                              New Member
                              • Sep 2007
                              • 8

                              #15
                              it should not be correct..
                              becoz the output should be:
                              1
                              4
                              9
                              16
                              25
                              36
                              49
                              64
                              81
                              100

                              ?

                              Comment

                              Working...