Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

help writing a function

I'm a grade 8 student who is trying to learn c programing.

I need help writing a function that generates powers (3^5) using while, or do-while statements.  I can't use multiple if-else statements.

thank you.
0
castles11
Asked:
castles11
  • 4
  • 3
  • 2
  • +5
1 Solution
 
GivenRandyCommented:
By "powers", do you mean the form  3^5 (243) or do you mean factorial 3! (6).
0
 
castles11Author Commented:
Edited text of question.
0
 
graham_kCommented:
int total;
int number, power;

perhaps prompt the user for the number & the power to which to raise it?

total = number
for i = 0 to power-1
 total = total * number

of course, it won't compile - we don't write your code for you - but it shows you the logicv you want to use.

Graham

p.s don't forget the speical case. Any number to the power zer0 is one.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hongjunCommented:
total = base;
while ( --power )
{
    total *= base;
}
0
 
ozoCommented:
You'd be better off working it out yourself, than copying suggestions containing sublte bugs.
On the other hand, maybe you can learn from their mistakes.
Being able to fix bugs will be an essential part of learning c programing.
0
 
graham_kCommented:
he he! I like that. Now I'm going to have to go puzzle through what I wrote! I'll let you know what I find.

(First thought is the speical case of negative powers)

(Second thought is that I should have said
for i = 1 to power -1)

any more?

p.s Randy, start this small problem the same way that you would start any large project - with design documentation.

In this case an algorithm will suffice. Just write in plain english what you want to achieve, step by step. When that is complete & verified, translate each step from english to C.
0
 
deightonCommented:
This is an integer only implementation, in which negative powers are meaningless, so my function doesn't handle them.

Hope it does the job!  I couldn't fathom out how to get the while function in there.  Good luck!



long power(int base, unsigned int value)
{

        return value ? power(base,value - 1) * base : 1;

}

main()
{
        printf("\n%li",power(2,10));
}



Do you require a function to work on floating points?  

How about complex numbers?  What would you want -1 to the power of one half be?

BTW what is 'eighth grade'?  My school years only went up to 5 in senior school, or six for people who stayed on.



0
 
GivenRandyCommented:
Graham wrote:

>p.s Randy, start this small problem
>the same way that you would start any
>large project - with design
>documentation.

Don't you start with requirements first?  That's what I did.  Doesn't sound like you have those down yet.  For example, is it required to handle negative numbers in either operator.  For example, are the numbers whole numbers only (e.g., is 2.3 ^ 3.9 allowed).

0
 
GivenRandyCommented:
First stab, include the "math.h" file and use the pow() function.
0
 
GivenRandyCommented:
The Microsoft Visual C++ documentation for pow() function, with code:

pow
Calculates x raised to the power of y.

double pow( double x, double y );

Routine Required Header Compatibility
pow <math.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

pow returns the value of xy. No error message is printed on overflow or underflow.

Values of x and y Return Value of pow  
x < > 0 and y = 0.0 1
x = 0.0 and y = 0.0 1
x = 0.0 and y < 0 INF


Parameters

x

Base

y

Exponent

Remarks

The pow function computes x raised to the power of y.

pow does not recognize integral floating-point values greater than 264, such as 1.0E100.

Example

/* POW.C
 *
 */

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

void main( void )
{
   double x = 2.0, y = 3.0, z;

   z = pow( x, y );
   printf( "%.1f to the power of %.1f is %.1f\n", x, y, z );
}


Output

2.0 to the power of 3.0 is 8.0


Floating-Point Support Routines

See Also   exp, log, sqrt
0
 
deightonCommented:
Given that he's been told to use 'while' it is innevitable that the power is an integer.

Hence the following.  But don't submit it as your homework solution.  Work out how to do it using while.

double power(double base, int value)
{

        if (value < 0)
        return power(base,value + 1) / base;
        return value ? power(base,value - 1) * base : 1;

}

main()
{
        printf("\n%lf",power(2,3));
}
0
 
ntdragonCommented:
i understand that you can't use more then one if statment
sorry if there is any bugs i wrote it here

int power(int a,int b){
   int pwr=1;
   if (b>=0){
     while(i>0){
        pwr*=a;
        i--;
     }
   else
     while(i<0){    
        pwr*=1/a;
        i++;
      }
   return pwr;
}
0
 
graham_kCommented:
hey, guys,  if it's homework we all know that we can't write his code for him. It can get both him & the poster thrown off of EE.

Standard o-perating procedure is for the questioner to show what code he has so far & for us to offer advice.

So - what code do you have so far?
0
 
vl_arunkumarCommented:
hello castles11,

                      I hope the following code solve ur problem
enjoy

#include<stdio.h>
#include<stdlib.h>
float powers(float,int);
main()
{
float base;
int power;
printf("Enter base value: ");
scanf("%f",&base);
printf("Power value: ");
scanf("%d",&power);
if(power!=0 && power%1==0)
printf("%f",powers(base,power));
if(power==0)
printf("1.0");
}
float powers(float base,int power)
{
      float temp=1;
      do
      {
            temp=temp*base;
            power--;
      }while(power);
      return temp;
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now