473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Exponent question

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
Nov 14 '05 #1
9 49560
You can use pow()
pow(base, exponent)

HTH

--
Frane Roje

Have a nice day

Remove (*dele*te) from email to reply
"Joseph Aldred" <ho***********@worldnet.att.net> wrote in message
news:05*******************@bgtnsc05-news.ops.worldnet.att.net...
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

Nov 14 '05 #2
Thank you for the help.
Nov 14 '05 #3
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

Nov 14 '05 #4
"Joseph Aldred" <ho***********@worldnet.att.net> 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
Nov 14 '05 #5
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
Nov 14 '05 #6
"Joseph Aldred" <ho***********@worldnet.att.net> 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.
Nov 14 '05 #7
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.

Nov 14 '05 #8
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)
Nov 14 '05 #9
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)

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Tmenke | last post by:
Is there a way to superscript a single character in a string for example to show a number raised to a exponent ? Eg lblResult.Caption = " 52 in4 " Where the 4 would be superscripted ? ...
5
by: sankar | last post by:
Hi, I am using a Q14.18 value. There are tables used in my program which are being indexed by the exponent and mantissa parts of the corresponding floating point value. So how can I get the...
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
1
by: Wayne Shu | last post by:
Hei everyone: Just see the output of the following program #include <iostream> #include <cstdlib> #include <limits> int main() { std::cout << "minimum exponent of double: " <<
11
by: Bartholomew Simpson | last post by:
I need to store two float values (each of which requires less 16 bit storage), in a double data type. Can anyone give me some macros to retrive/store values in the double ? something along the...
6
by: databyss | last post by:
I have a simple program and the output isn't what I expect. Could somebody please explain why? Here's the code: #simple program print "v = 2" v = 2 print "v**v = 2**2 =", v**v print...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, By default, the "g" format specifier seems to use 2 digits in the exponent if it decides to use the scientific format. I.e., Double.ToString("g"). How do I control the number of...
2
by: abrown07 | last post by:
Another noob question. How do i write out an exponent in my C code. I want to say Population= N*exponent(r)
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.