By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,818 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,818 IT Pros & Developers. It's quick & easy.

Power in C

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a

"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

P: n/a
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

P: n/a
>>
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.