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
  • #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1;...

    #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function  

  • The operating system is Ubuntu 18.04 hello.c #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello...

    The operating system is Ubuntu 18.04 hello.c #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello world!\n"); return 0; } syscall-hello.c #include <unistd.h> #include <sys/syscall.h> char *buf = "Hello world!\n"; int main(int argc, char *argv) { size_t result; /* "man 2 write" to see arguments to write syscall */ result = syscall(SYS_write, 1, buf, 13); return (int) result; }Download and compile hello.ce and syscall-hello.com. Compile them statically and dynamically. How do the library and system calls produced by them compare...

  • lab4C.c file contains: #include <stdio.h> ..... #define SIZE 10 #define SIZE2 40 int main(int argc, char *argv...

    lab4C.c file contains: #include <stdio.h> ..... #define SIZE 10 #define SIZE2 40 int main(int argc, char *argv[]) { char input[SIZE2]; char name[SIZE]; .... char resu[SIZE2], resu2[SIZE2], resu3[SIZE2]; printf("Enter name, age and wage (exit to quit): "); fgets(input, 40, stdin); while (...) { /* use fgets to read again */ printf("Enter name, age and wage (exit to quit): "); fgets(input, 40, stdin); } return 0; }3.1 Specification Develop an ANSI-C program that reads user information from the standard inputs, and outputs...

  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

  • #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='...

    #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='z';      pc=&(c[8]);      pk=&(k[0]);      for (i=0; i<9; i++)     {           *pc=a-(char)i;           pc--;           *pk=(int)a-i;           pk++;           }                     return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...

  • #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] +...

    #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] + strText[1] + strText[2]; int nTextLen = strlen(strText); int count = 0; printf("Welcome to token generator!\n"); printf("Enter a word to use in the token generator.\n You may enter as many words as you like. \n Press q and key when finished.\n"); scanf("%c", &strText[i]); //compute when to stop loop //check nTextLen == 1 and strText[0] == 'q' for(i=0;i< nTextLen;i++) if ((strlen(strText) != 1) || (strText[0] !=...

  • 1 Rewrite the following program so that it will use function. include <stdio.h> Int main) printf...

    1 Rewrite the following program so that it will use function. include <stdio.h> Int main) printf ("Hello!") return 0 2 Assume a program has the following declarations: short s- 4 int i--5; long m-3 float f-19.3f; double d-3.6; What are the following values? (a) s+m (b) (float) d (c) d s (e) (int) f 3 What will be the output of the following program: int x = 3 float a 2.76; y-2* x; print f("X-2d, y*ta, z»%d", x, y, z);...

  • #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to...

    #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to sort in ascending order:\n");       for(i = 0; i < 10; i++)             scanf("%d", &data[i]);       /* Sorting process start */     ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ******       printf("After sort\n");       for(i = 0; i < 10; i++)             printf("%d\n",data[i]);       return 0; } Looking for alternative solutions for the program code.

  • #include <stdio.h> int function() { int x, y; printf("Enter the value of x:"); scanf("%d", &x); y...

    #include <stdio.h> int function() { int x, y; printf("Enter the value of x:"); scanf("%d", &x); y = 3*x*x*x*x*x + 2*x*x*x*x - 5*x*x*x - x*x + 7*x - 6; printf("%d", y); return y; } int main() { function(); } Modify the program of Programming Project 5 so that the polynomial is evaluated using the following formula: ((((3x+2)x-5)x+7)x-6

  • C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished)...

    C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished) { printf("Enter n > 0 or quit\n"); scanf("%10s", buffer); if (strcmp(buffer, "quit") == 0) { finished = true; } else { // Convert input to number and compute n-th prime num = atoi(buffer); if (num > 0) { res = nth_prime(num); printf("Prime #%d is %d\n", num, res); }...

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