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.

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

  • C - Language Write a loop that sets each array element to the sum of itself...

    C - Language Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 +...

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

  • C++ language Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ language Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "C" "" "" "a" "d" "e" O Full Screen 1 code.cpp + New #include <string> 2 using namespace std; 3 4- int...

  • Write a function that can return the length of a string in C language. Please leave...

    Write a function that can return the length of a string in C language. Please leave comments to explain the code and verify that it runs properly... Example: Input String: China The length of the string is 5. #include <stdio.h> int length(char * p) {     /write your code here } main(){     int len;     char str[20];     printf("Input string:");     scanf("%s", str);     len = length(str);     printf("The length of string is %d. ", len);     getchar();    ...

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