Help Me?

This is a discussion on Help Me? within the C Programming forums, part of the General Programming Boards category; I need to use the pow() function here so that m and n are integers and n in nonegative, the ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question Help Me?

    I need to use the pow() function here so that m and n are integers and n in nonegative, the cal pow(m,n) returns m raised to the nth power: I need to use the following code:

    product = 1;
    for (i = 1; i< = n; ++i)
    prooduct *= m;

    Here is what i have come up with so far. Can anyone get get me to solve this problem, please.

    #include<stdio.h>
    #include<math.h>

    int main(void)
    {

    double m, n, i, product;

    printf("\n%s\n%s\n%s\n\n",
    "Input m as a positive integer and n as a negative integer.",
    "The square root of m and n raised",
    "to the n power will now be computed.");

    while (0)
    scanf("%lf", &n);
    product = 1;
    for (i = 1; i <= n; ++i)
    product *= m;
    printf("\n%lf", m);
    return 0;
    }

    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Could you describe your problem more clearly, I'm having trouble parsing it. And consider your thread titles more carefully; "help me" just doesn't cut it.

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

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Perlude to a kiss!

    And i thought you were one with the universe. I practically wrote the whole book out for everyone to see my problem but as said said sarcastically with a s -n- m tone. Help me just dont cut it.
    later prelude
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >And i thought you were one with the universe.
    Only in my mind.

    >I practically wrote the whole book out for everyone to see my problem
    Then I'll need clarification. Are you supposed to write your own power function or use the standard library pow in math.h? Assuming the user is supposed to enter two integers, one for the value and one for the power, and you need to do the calculations without calling the standard pow, your function may look like this:
    Code:
    static double power ( int m, int n )
    {
      double product = 1.0;
      while ( n-- > 0 )
        product *= m;
      return product;
    }
    That way you simply take the input and then call power with it.

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

  5. #5
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    I am suppose to use the library <math.h> and from there write a simple program that does as stated above.
    This stuff is tough and i am not even a computer science major.
    WOWSER!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I understand now, I just needed it drilled into my head a bit. I suppose it should be something like this:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    static double power ( int m, int n )
    {
      double product = 1.0;
      while ( n-- > 0 )
        product *= m;
      return product;
    }
    
    int main ( void )
    {
      int m, n;
      printf ( "Input m as a positive integer and n as a negative integer: " );
      if ( scanf ( "%d %d", &m, &n ) == 2 ) {
        printf ( "The square root of m and n raised to the "
                 "n power will now be computed.\n" );
        printf ( "%f\n", sqrt ( power ( m, n ) ) );
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

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