Solved

# help writing a function

Posted on 2000-05-14
206 Views
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
Question by:castles11
• 4
• 3
• 2
• +5

LVL 9

Expert Comment

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

Author Comment

Edited text of question.
0

LVL 6

Expert Comment

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

LVL 33

Expert Comment

total = base;
while ( --power )
{
total *= base;
}
0

LVL 84

Expert Comment

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

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

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

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

First stab, include the "math.h" file and use the pow() function.
0

LVL 9

Expert Comment

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 );

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

0

LVL 18

Expert Comment

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

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

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

vl_arunkumar earned 50 total points
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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.