POW FUNCTION....PLEASE HELP 
Author Message
 POW FUNCTION....PLEASE HELP

You must be very careful how you accept numbers from the user. Your textbook
may be full of examples of the use of scanf(), but you must ignore this. Try
this code instead:

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

double getNumber(char *prompt)
{
 char buff[128];
 double x;
 int converted;
 do {
  printf("%s: ",prompt);
  fgets(buff,128,stdin);
  converted = sscanf(buff,"%lf",&x);
  if(converted != 1) {
   printf("Input error. Please try again.\n");
  }
 }
 while(converted != 1);
 return x;

Quote:
}

int main ()
{
 double m,n;
 do{
  m = getNumber("Enter a number (> 0)");
  n = getNumber("Enter an exponent (> 0)");
  if((m > 0 ) && (n > 0)) {
   printf ("%f raised to the %f power is: %f.\n",m,n, pow(m,n));
  }
 }
 while ((m > 0) && (n> 0));
 return 0;

Quote:
}

--

Paul Lutus
www.arachnoid.com


Quote:
>Let me start off by saying that I am just beginning to learn C and that I
am
>only in the first chapter of my text.  My instructor wants me to write a
>program that reads positive integers m and n and then print m^n.

<snip>


Sat, 11 Aug 2001 03:00:00 GMT  
 POW FUNCTION....PLEASE HELP
Let me start off by saying that I am just beginning to learn C and that I am
only in the first chapter of my text.  My instructor wants me to write a
program that reads positive integers m and n and then print m^n.  Here is what
I came up with so far (the program compiles fine, but it always gives 1.000000
as an answer no matter what 2 integers I enter):

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

int main (void)
{
        double m,n;
   do{
        printf("Enter a positive integer m:\n");
        scanf ("%f",&m);
        printf("Enter a positive integer n:\n");
        scanf ("%f",&n);
        printf ("m raised to the n power is: %f\n", pow(m,n));}
        while (m>=1&&n>=1);
   return 0;  }

Could someone explain what I am doing wrong and try to point me in the right
direction.  Any help would be appreciated.
     Thanks, Mike



Sun, 12 Aug 2001 03:00:00 GMT  
 POW FUNCTION....PLEASE HELP

Quote:

> Let me start off by saying that I am just beginning to learn C and that I am
> only in the first chapter of my text.  My instructor wants me to write a
> program that reads positive integers m and n and then print m^n.  Here is what
> I came up with so far (the program compiles fine, but it always gives 1.000000
> as an answer no matter what 2 integers I enter):

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

> int main (void)
> {
>         double m,n;
>    do{
>         printf("Enter a positive integer m:\n");
>         scanf ("%f",&m);
>         printf("Enter a positive integer n:\n");
>         scanf ("%f",&n);
>         printf ("m raised to the n power is: %f\n", pow(m,n));}
>         while (m>=1&&n>=1);
>    return 0;  }

> Could someone explain what I am doing wrong and try to point me in the right
> direction.  Any help would be appreciated.
>      Thanks, Mike

Do you debug this code and see what values gets m and n ?You declared m & n as
double, wherefore

scanf (" %lf", &m );
scanf (" %lf", &n );
printf (" m( %lf ) raised to the n( %lf ) power is: %lf\n", m, n, pow( m,n ) );

I checked this work.



Sun, 12 Aug 2001 03:00:00 GMT  
 POW FUNCTION....PLEASE HELP
MStoner713 schrieb:

Quote:

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

> int main (void)
> {
>         double m,n;
>    do{
>         printf("Enter a positive integer m:\n");
>         scanf ("%f",&m);

Hi MStoner713,

Your text does not match your input statement, and it is indeed your input
statement that contains an error. The "%f" specification is defined as
reading a *float*, not a double. To read a double with the "scanf()"
function use the "%lf" specification.

(Yes, this differs from "printf()", that's the catch)

Stephan
(initiator of the campaign against grumpiness in c.l.c)
(-: A brandnew excellent FAQ version has been released !!! :-)
(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)



Sun, 12 Aug 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. POW Function...Please Help

2. POW Function...Please Help

3. help with the pow() function

4. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

5. PLEASE PLEASE HELP HELP...question on interleaving C functions

6. Please help!!!!Please help!!!!Please help!!!!

7. computing powers without the pow() function.

8. POW FUNCTION

9. pow function

10. a pow function

11. the pow() function

12. Trying to use pow() function...

 

 
Powered by phpBB® Forum Software