473,388 Members | 1,213 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,388 software developers and data experts.

Power in C

Hi I am using this function to power numbers
int power (int m, int n);

int power (int base, int n) {
int i,
p;
p = 1;
for (i = 1; i <= n; ++i)
p *= base;
return p;
}

but my problem is that I need to power using 1.05 or 2.33
and this isn't working.

Should I use float?

another question.
is there any function call to clear the screen?
(I was using the system(clear); but it is not compatible
with other systems.

is there any way to read the user return?
like

printf("press return to go back to menu");
wait_return=getchar();
???
Thanks Profetas

Nov 14 '05 #1
9 1726

"Profetas" <xu*****@yahoo.com> wrote in message
news:56******************************@localhost.ta lkaboutprogramming.com...
Hi I am using this function to power numbers
int power (int m, int n);

int power (int base, int n) {
int i,
p;
p = 1;
for (i = 1; i <= n; ++i)
p *= base;
return p;
}

but my problem is that I need to power using 1.05 or 2.33
and this isn't working.

Should I use float?
#include <math.h> and use pow() which takes two doubles and returns a
double.

another question.
is there any function call to clear the screen?
(I was using the system(clear); but it is not compatible
with other systems.
not portably, you could either have a lot of '\n's or check which system you
are on with #ifdef. On most modern OSs system("clear"); or system("cls")
would work, but you would have to check which one you require as mentioned.

is there any way to read the user return?
like

printf("press return to go back to menu");
wait_return=getchar();
???


I dont undersstand what you mean.
Allan
Nov 14 '05 #2
in comp.lang.c i read:
Hi I am using this function to power numbers
int power (int m, int n); but my problem is that I need to power using 1.05 or 2.33
and this isn't working.

Should I use float?
well, 1.05 and 2.33 are floating point values of type double so using
double or float would make sense. is there a particular reason you
aren't using the pow function?
is there any function call to clear the screen?
standard c doesn't have any. you need something for your platform, which
would be off-topic in this group.
is there any way to read the user return?
like

printf("press return to go back to menu");
wait_return=getchar();


this seems to read from the user (in most cases).

--
a signature
Nov 14 '05 #3
>>
is there any way to read the user return?
like

printf("press return to go back to menu");
wait_return=getchar();
???


I dont undersstand what you mean.
Allan


I wanted some function that would wait
the user to press enter.
When I use getchar it expect a char and return won't
count as a char.

Nov 14 '05 #4
Profetas <xu*****@yahoo.com> wrote:

is there any way to read the user return?
like

printf("press return to go back to menu");
wait_return=getchar();
???
I dont undersstand what you mean.
Allan

I wanted some function that would wait
the user to press enter.
When I use getchar it expect a char and return won't
count as a char.


As the following little program will show you, the return key also
counts as a char:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
int key;

while ( 1 ) {
if ( ( key = getchar( ) ) == EOF )
return EXIT_SUCCESS;
printf( "Got a key: %d\n", key );
}

return EXIT_SUCCESS;
}

Even more, it will only returns after the user pressed <RETURN> (unless
you fiddled around too much with your terminal settings ;-) All you make
have to sure of is to read getchar() repeatedly until you find the '\n'
to empty the input buffer in case the user hit not only the <RETURN>
key. I.e. use something like

while ( ( key = getchar( ) ) != '\n' )
/* empty */ ;

Return, Jens

--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #5
Profetas wrote:
is there any way to read the user return? like

printf("press return to go back to menu");
wait_return=getchar();
???


I dont undersstand what you mean.


I wanted some function that would wait the user to press enter.
When I use getchar it expect a char and return won't
count as a char.


Yes it will. Try calling:

#include <stdio.h>

void awaitreturn(void)
{
int ch;

while (('\n' != (ch = getchar())) && (EOF != ch)) continue;
}

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Nov 14 '05 #6
"Profetas" <xu*****@yahoo.com> wrote in message news:<56******************************@localhost.t alkaboutprogramming.com>...
Hi I am using this function to power numbers
int power (int m, int n);

int power (int base, int n) {
int i,
p;
p = 1;
for (i = 1; i <= n; ++i)
p *= base;
return p;
}

but my problem is that I need to power using 1.05 or 2.33
and this isn't working.

Should I use float?

another question.
is there any function call to clear the screen?
(I was using the system(clear); but it is not compatible
with other systems.

is there any way to read the user return?
like

printf("press return to go back to menu");
wait_return=getchar();
???
Thanks Profetas


Hello

Use the standard c function

double pow(double x, double y)

it does exactly the things you what to do

Greetings Olaf
Nov 14 '05 #7
Profetas Wrote
Hi I am using this function to power numbers
int power (int m, int n);

int power (int base, int n) {
int i,
p;
p = 1;
for (i = 1; i <= n; ++i)
p *= base;
return p;
}

but my problem is that I need to power using 1.05 or 2.33
and this isn't working.

Should I use float?
Allan Bruce Wrote
#include <math.h> and use pow() which takes two doubles >and returns a
double.

I tried what you suggest and I got this error

/tmp/ccwFiFtL.o(.text+0x141): In function `mm':
: undefined reference to `pow'
collect2: ld returned 1 exit status
Thanks profetas

Nov 14 '05 #8
Profetas wrote:
I tried what you suggest and I got this error

/tmp/ccwFiFtL.o(.text+0x141): In function `mm':
: undefined reference to `pow'
collect2: ld returned 1 exit status


http://www.eskimo.com/~scs/C-faq/q14.3.html

Nov 14 '05 #9
"Profetas" <xu*****@yahoo.com> wrote in message news:<c2******************************@localhost.t alkaboutprogramming.com>...
Profetas Wrote
Hi I am using this function to power numbers
int power (int m, int n);

int power (int base, int n) {
int i,
p;
p = 1;
for (i = 1; i <= n; ++i)
p *= base;
return p;
}

but my problem is that I need to power using 1.05 or 2.33
and this isn't working.

Should I use float?


Allan Bruce Wrote
#include <math.h> and use pow() which takes two doubles >and returns a
double.


I tried what you suggest and I got this error

/tmp/ccwFiFtL.o(.text+0x141): In function `mm':
: undefined reference to `pow'
collect2: ld returned 1 exit status
Thanks profetas


You need to get a math library linked with your executable. Consult
your compiler's documenation for how to do this. Following example is
for gcc.

dresnick(1224)$ cat foo.c
#include <stdio.h>
#include <math.h>

int main()
{
double result = pow (4.0,0.5);

printf("result is %f\n", result);

return 0;
}
dresnick(1225)$ gcc -Wall -ansi -pedantic -lm -o foo foo.c
dresnick(1226)$ foo
result is 2.000000
Nov 14 '05 #10

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

Similar topics

6
by: Stephane Belzile | last post by:
Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
12
by: Roman Töngi | last post by:
I just want to raise a number to a power. e.g.: 16^2 The pow()-function of cmath seems to be not enough. I've read about valarray. Is it really so difficult? Can you give me an example. ...
0
by: Thiva Charanasri | last post by:
http://www.poweroflanguage.org Track: Computer Language 1st World Congress on the Power of Language: Theory, Practice and Performance Date: March 6 - 10, 2006 Bangkok, Thailand On this...
0
by: Thiva Charanasri | last post by:
http://www.poweroflanguage.org Track: Computer Language 1st World Congress on the Power of Language: Theory, Practice and Performance Date: March 6 - 10, 2006 Bangkok, Thailand On this...
1
by: Richard Fennell | last post by:
Am trying to do ASP.NET development on my XP Prof. box logged in as a power user (not the administrator I used to be). As an admin user all is OK. But as a power user, I have made sure my...
5
by: mathon | last post by:
hi, i develeoped a recursive function for the power and it works fine double power(double x, int n) { if (n < 0) return 1/power(x, -n); else if (n == 0) return 1.0;
9
by: mathon | last post by:
hi, i already posted an entry because of this problem, unfortunately i havent solved it so far..:( I have created a recursion for the calculation of the power like this: double...
6
by: maxthebaz | last post by:
Our machines have this requirement: if power failure occurs, many important variables are to be resumed from where they were interrupted after the machine is restarted (power on in this case). In...
3
by: greek | last post by:
the question is to calculate x^n(x power n) (whr n can be +ve or -ve) by using recursion. the algorithm is x= 1, n=0 1/x^n, n<0 x*x^(n-1), n>0 ...
8
by: skanemupp | last post by:
how do i solve power(5,1.3)? def power(nbr, po): if po==0: return 1 if po>0: return nbr*power(nbr, po-1) if po<0: return 1/power(nbr, -1*po)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.