Coding help desperately needed!!!

Discussion in 'Computer Science & Culture' started by Adam, Mar 22, 2002.

Thread Status:
Not open for further replies.
  1. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Coding help please. These three little things keep returning 0.0. What am I doing wrong with each?

    PS: the stdio.h is there, but won't show up here because of the BBS codes.

    ---------------------

    ONE

    #include <stdio.h>

    double fun_double(double, int);

    void main(void)
    {
    double x;
    int n;
    printf("\n\n\nEnter a number, a space, and the power to raise the first number by: ");
    scanf("%f %d", &x, &n);
    printf("\nThe result is: %0.2f\n\n", fun_double(x, n) );
    }

    double fun_double(double x, int n)
    {
    int i = 0;
    double y = 1.0;
    for(i = 0; i < n; i++)
    {
    y *= x;
    }
    return (y);
    }

    ------------------

    TWO

    #include <stdio.h>

    double fun_double(double, int);

    void main(void)
    {
    double x;
    int n;
    printf("\n\n\nEnter a number, a space, and the power to raise the first number by: ");
    scanf("%f %d", &x, &n);
    printf("\nThe result is: %0.2f\n\n", fun_double(x, n) );
    }

    double fun_double(double x, int n)
    {
    double z, y = 1.0;
    z = x;
    while(n)
    {
    if(n & 1)
    {
    y *= z;
    }
    z *= z;
    n >>= 1;
    }
    return (y);
    }

    ------------------

    THREE

    #include <stdio.h>

    double powerfunction(int, int);

    void main(void)
    {
    int base, power;
    printf("\n\nEnter base, space, power, and hit ENTER: ");
    scanf("%d %d", &base, &power);
    printf("\n\nResults is: %d\n\n", powerfunction(base, power) );
    }

    double powerfunction(int base, int power)
    {
    int i;
    double result = 1.0;
    for(i = 0; i < power; i++)
    {
    result *= base;
    }
    return result;
    }
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Well, never mind, all fixed up now. I had a few silly things here and there, and in the end I wrote up something entirely different anyway. Yes, as you can see, I'm still getting started on this stuff.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
Thread Status:
Not open for further replies.

Share This Page