Power in C

P

Profetas

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
 
A

Allan Bruce

Profetas said:
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.

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
 
T

those who know me have no need of my name

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).
 
P

Profetas

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.
 
J

Jens.Toerring

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
 
C

CBFalconer

Profetas said:
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;
}
 
O

olaf

Profetas said:
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
 
P

Profetas

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
 
D

David Resnick

Profetas said:
Profetas Wrote

Allan Bruce 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


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top