Solved

help writing a function

Posted on 2000-05-14
14
213 Views
Last Modified: 2010-04-15
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
Comment
Question by:castles11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +5
14 Comments
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2809079
By "powers", do you mean the form  3^5 (243) or do you mean factorial 3! (6).
0
 

Author Comment

by:castles11
ID: 2809171
Edited text of question.
0
 
LVL 6

Expert Comment

by:graham_k
ID: 2809246
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
Independent Software Vendors: 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!

 
LVL 33

Expert Comment

by:hongjun
ID: 2809342
total = base;
while ( --power )
{
    total *= base;
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 2809456
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
 
LVL 6

Expert Comment

by:graham_k
ID: 2809762
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
 
LVL 18

Expert Comment

by:deighton
ID: 2810226
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
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2810693
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
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2810704
First stab, include the "math.h" file and use the pow() function.
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2810712
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
 
LVL 18

Expert Comment

by:deighton
ID: 2810946
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
 
LVL 1

Expert Comment

by:ntdragon
ID: 2812026
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
 
LVL 6

Expert Comment

by:graham_k
ID: 2812696
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
 

Accepted Solution

by:
vl_arunkumar earned 50 total points
ID: 2841203
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question