# Exponent question

Discussion in 'C Programming' started by Joseph Aldred, Apr 17, 2004.

1. ### Joseph AldredGuest

I am new to C and I am writing a program for a class I am taking. I need to
use a exponent but I can not figure how to do it. I figure I need to use
exp() or pow() but I can not figure out how to do it. Any help that anyone
can give me would be appreciated greatly.

Thanks,
Joseph

Joseph Aldred, Apr 17, 2004

2. ### Frane RojeGuest

You can use pow()
pow(base, exponent)

HTH

--
Frane Roje

Have a nice day

Remove (*dele*te) from email to reply
"Joseph Aldred" <> wrote in message
news:05ggc.42752\$...
> I am new to C and I am writing a program for a class I am taking. I need

to
> use a exponent but I can not figure how to do it. I figure I need to use
> exp() or pow() but I can not figure out how to do it. Any help that

anyone
> can give me would be appreciated greatly.
>
> Thanks,
> Joseph
>
>

Frane Roje, Apr 17, 2004

3. ### Joseph AldredGuest

Thank you for the help.

Joseph Aldred, Apr 17, 2004
4. ### Joseph AldredGuest

Ok I am still having problems so I must be doing something wrong. What I am
supposed to be doing is writing a problem to solve the polynomial

3x^3 - 5x^2 + 6
for x=2.55

What I came up with for a program is:

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

main ()
{
double a ;

a = 2.55

printf ("3 * pow(%f,3) - 5 * pow(%f,2) + 6\n", a);

return 0;
}

or I also tried this

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

main ()
{
float a = 2.55;

float b = 3 * pow(%f,3) - 5 * pow(%f,2) +6, a, a;

printf ("3 * pow(%f,3) - 5 * pow(%f,2) + 6 = ", a, a);

return 0;
}

On the second one I keep getting parse error before '%' token

Does anyone have any suggetions as to what I am doing wrong?

I appreciate any help anyone is willing to offer.

Thank you,
Joseph

Joseph Aldred, Apr 17, 2004
5. ### Ben PfaffGuest

"Joseph Aldred" <> writes:

> Ok I am still having problems so I must be doing something wrong. What I am
> supposed to be doing is writing a problem to solve the polynomial
>
> 3x^3 - 5x^2 + 6
> for x=2.55

I think you mean that you want to "evaluate", not "solve", the
polynomial.

> What I came up with for a program is:
>
> #include <stdio.h>
> #include <math.h>
>
> main ()

You should write
int main(void)
explicitly.

> {
> double a ;
>
> a = 2.55

Missing semicolon.

> printf ("3 * pow(%f,3) - 5 * pow(%f,2) + 6\n", a);

This is misguided. What is inside the parentheses will be output
verbatim, except that %f will be expanded to the value of the
floating-point arguments (you supplied one too few of them, by
the way) and \n will be expanded to a new-line character.

> return 0;
> }
>
>
> or I also tried this
>
> #include <stdio.h>
> #include <math.h>
>
> main ()
> {
> float a = 2.55;
>
> float b = 3 * pow(%f,3) - 5 * pow(%f,2) +6, a, a;

This is also misguided. % is only used in this way in the format
string argument to printf() and similar functions. Here you want
to write
float b = 3.0 * pow(a, 3) - 5.0 * pow(a, 2) + 6.0;
or
float b = 3.0 * a * a * a - 5.0 * a * a + 6.0;
Also, I'd recommend using double instead of float.

> printf ("3 * pow(%f,3) - 5 * pow(%f,2) + 6 = ", a, a);

This will print the polynomial, but you omitted printing the
result.

> return 0;
> }

Maybe you should buy a C textbook?
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz

Ben Pfaff, Apr 17, 2004
6. ### Joseph AldredGuest

Thank you Ben,

I have a book called "Programming in ANSI C". by Stephen G Kochan. It is
the textbook for the class I am taking. I am not sure that I like it very
well, my instructor seems pretty critical of the way the material is
presented. I look in the index and there is only one place for exponents
and all that has to do with is scientific notation.

But thank you for your help. I am so glad that there are people here that
can give help to us newbies.

J

Joseph Aldred, Apr 17, 2004
7. ### MalcolmGuest

"Joseph Aldred" <> wrote in
>
> I look in the index and there is only one place for exponents
> and all that has to do with is scientific notation.
>

We use exponents in two places. Firstly, where numbers are so large or so
small that decimal point notation is not human-readable. For instance
100,000,000,000 is best written as 1 * 10 ^11. The second place is where the
program logic calls for a value to be raised to the power of another value.
For instance, say we have a virus multiplying in a large population, and
each infected person infects another five on average before dying. How many
people will be infected after N rounds? The answer is 5^N, or, in C,
pow(5.0, N).
For mathematical reasons which are too involved to go into here the
expression e^x has special characteristics, so it has its own function,
exp(). The expression x^0.5, or sqrt(x) is also special and has its own
function - here it is easy for a non-mathematician to see the value of this.

Malcolm, Apr 18, 2004
8. ### Martin AmbuhlGuest

Joseph Aldred wrote:

> Ok I am still having problems so I must be doing something wrong. What I am
> supposed to be doing is writing a problem to solve the polynomial
>
> 3x^3 - 5x^2 + 6
> for x=2.55
>
> What I came up with for a program is:

[misuse of printf() as if it were a Basic interpreter ignored, since the
relevant errors otherwise are repeated below.]

> or I also tried this
>
> #include <stdio.h>
> #include <math.h>
>
> main ()

main returns an int. You should say so
int main(void)

> {
> float a = 2.55;
>
> float b = 3 * pow(%f,3) - 5 * pow(%f,2) +6, a, a;

float b; /* but you really want doubles; whatever */
b = 3*pow(a,3) - 5*pow(a,2) + 6; /* pow() takes an argument.
This isn't a lambda expression. */
But, obviously, this is avoids the function call:
b = 3*a*a*a - 5*a*a +6;
And better is
b = 6 + a*a*(3*a - 5);

> printf ("3 * pow(%f,3) - 5 * pow(%f,2) + 6 = ", a, a);

^^ ^
%f\n , b
return 0;
> }
>
>
> On the second one I keep getting parse error before '%' token
>
> Does anyone have any suggetions as to what I am doing wrong?

Using a format specifier instead of an argument.

Martin Ambuhl, Apr 18, 2004
9. ### CBFalconerGuest

Joseph Aldred wrote:
>
> I am new to C and I am writing a program for a class I am taking.
> I need to use a exponent but I can not figure how to do it. I
> figure I need to use exp() or pow() but I can not figure out how
> to do it. Any help that anyone can give me would be appreciated
> greatly.

Your answer is somewhere among the following references.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)

CBFalconer, Apr 18, 2004
10. ### CBFalconerGuest

Joseph Aldred wrote:
>
> Ok I am still having problems so I must be doing something wrong.
> What I am supposed to be doing is writing a problem to solve the
> polynomial
>
> 3x^3 - 5x^2 + 6
> for x=2.55

This is a different question. Start by applying some algebra:

y = 3 * x^3 - 5 * x^2 + 6
= ((((3 * x) - 5) * x) + 0) * x + 6

Do you see a pattern?

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)

CBFalconer, Apr 18, 2004

## Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.