Question

2.1 Specification Write an ANSI-C program that reads input from the standard input, which contains two integers representing2.3 Sample Inputs/Outputs red 117 a.out enter base and power: 10 2 100.0000 pow my_pow: 100.0000 enter base and power : 10 4

#include <stdio.h>

..

int main(int argc, char *argv[])
{
        
   int base, power;  
   printf("enter base and power: ");
   scanf("%d %d", &base, &power);

   while (base != -100){
        double res = pow(base, power);
        double res2 = my_pow(base, power);
        printf("pow:    %.4f\n", res);
        printf("my_pow: %.4f\n", res2);


         ....


    }
        
    return 0;
}

// this function should be RECURSIVE
// should not use any loop here
double my_pow(double base, double p)
{

}

lab4pow.c file contains:

1 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is my code to solve your problem. I have checked outputs on all the given inputs, and everything is working fine. I have attached the screenshot of code below. If you are satisfied with the answer please like.

CODE:

############################

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

// this function should be RECURSIVE
// should not use any loop here
double my_pow(int base, int p)
{
if(p<0) return 1.0/my_pow(base, -1*p);
if(p==1) return base;
  
double val = my_pow(base, p/2);
if(p%2==1) {
return val*val*base;
} else {
return val*val;
}
}

int main(int argc, char *argv[])
{
  
int base, power;

printf("enter base and power: ");
scanf("%d %d", &base, &power);

while (base != -100){
double res = pow(base, power);
double res2 = my_pow(base, power);
printf("pow: %.4f\n", res);
printf("my_pow: %.4f\n", res2);
  
printf("enter base and power: ");
scanf("%d %d", &base, &power);

}
  
return 0;
}

#############################

OUTPUT:

enter base and power: 10 2 100.0000 pow: my pow: 100.0000 enter base and power: 10 4 pow: 10000.0000 my_pow: 10000.0000 enter

Add a comment
Know the answer?
Add Answer to:
#include <stdio.h> .. int main(int argc, char *argv[]) { int base, power; printf("enter base and power:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT