Question

Exercise 2.10. Write a function to compute the power a where n 0. It should have the following prototype: 1 /r Sets *p to the n th power of a and returns 0, ez cept 2 when n or p is NULL, in which case it returns -1. 4 int power (int a int n int p) Write a unit test in a main function to test various values. The following code sequence illustrates how to use printf to provide informative output: int x 3, y 5, pow; 2 power (x, y, & pow) i 3 print (%d %d. %d\n. x, y, pow) n, C language
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
/* Function to find a^n */

int main()
{
int x=2,y=5,pow;
powerOfn(x,y,&pow);
printf("%d ^ %d = %d \n", x , y, pow);
return 0;
}
int powerOfn(int a, int n, int *p)
{
int power=1;
if (n < 0 || !p)
return -1;
while (n > 0)
{
power = power * a;
n=n-1;
}
*p=power;
return 0;
}

C (Gcc-4.9.2) 1 #include<stdio.h> 2 Function to find an / 4 int main() int x-2,y=5,pow; powerOfn (x,y, &pow); printf(%d ^ %d

Add a comment
Know the answer?
Add Answer to:
C language Write a function to compute the power a^m, where n greaterthanorequalto 0. It should...
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
  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • c language. A complex number N is defined as: N = x + iy , where...

    c language. A complex number N is defined as: N = x + iy , where x is the real part and y is the imaginary part. The power of N is real and defined as: N^2 = x^2 + y^2, Write a function called cpower that returns the power of complex number. The function takes in x and y as double values and returns the power as a double value.

  • language is C. not C++ a) Write the function header for a function called myswap that...

    language is C. not C++ a) Write the function header for a function called myswap that takes two pointers to integer numbers x and y as parameters and returns a Boolean value. b) Write the function prototype for the function in part (a).

  • Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[],...

    Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[], char destination[], int n); This function copies string source to string destination. Parameter n represents the size of array destination. If the latter array is not sufficiently large to hold the whole source string then only the prefix of the string which has room in the latter array should be copied. Note that after copying, the null character should also be included to mark...

  • help me solving this both please in c++ language 6 Write function max(int al Lint n)...

    help me solving this both please in c++ language 6 Write function max(int al Lint n) that receives an array of integer and its length, it returns the maximum number in that array. Test your function in main. 7) Write functin reverse (char x[ .int n) that reieves an array of characters and reverse the order of its elements. Test this function in main.

  • /*    Write a function that takes as an argument the value x,    and returns...

    /*    Write a function that takes as an argument the value x,    and returns the result of the expression: 20x^4 - 12x^3 - 3x^2 + 15x + 3    Note: No importing; use the power function given to you, and use it    as a reference for writing functions (note also that main itself        if a function!). */ public class Exercise9_1 { public static void main(String[] args) { int num = 5, pow = 2; System.out.println(num...

  • write in C language In = { 3.3 The Jacobsthal Function The Jacobsthal sequence is very...

    write in C language In = { 3.3 The Jacobsthal Function The Jacobsthal sequence is very similar to the Fibonacci sequence in that it is defined by its two previous terms. The difference is that the second term is multiplied by two. 0 if n=0 if n = 1 Jn-1 + 2Jn-2 otherwise Write a recursive function to compute the n-th Jacobsthal number. Write a main function that reads in an integer n and outputs Jn. Test your program for...

  • Write a function, polar_to_rec, that uses the structures defined in Problem 2. Test your function with...

    Write a function, polar_to_rec, that uses the structures defined in Problem 2. Test your function with various angles and magnitudes. Problem 2: #include<stdio.h> #include<math.h> struct RectangularCordinate{ float x; float y; }; struct PolarCordinate{ float radius; float theta; }; struct PolarCordinate rect_to_polar(struct RectangularCordinate rect){ float radius = pow(rect.x*rect.x + rect.y*rect.y,0.5); float angle = atan(rect.y/rect.x); struct PolarCordinate pol = {radius,angle}; return pol; } int main(){ struct RectangularCordinate rect = {3,4}; struct PolarCordinate pol = rect_to_polar(rect); printf("Radius: %.2f\n", pol.radius); printf("Angle: %.2f\n", pol.theta); }

  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

  • In C++, write a recursive function power that takes two positive integers base and n as...

    In C++, write a recursive function power that takes two positive integers base and n as inputs and computes base to the n power. Example power(3, 2) is 9. Do not use any loops in your program. This is what I got so far but I'm not sure what I'm doing wrong: #include <iostream> using namespace std; int calc(int x, int y); int main() {       calc(2, 3);    return 0; } int calc() {          cout...

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