Question

4. Write a C definition (prototype) for a function called power which takes a float and...

4. Write a C definition (prototype) for a function called power which takes a float and an integer as inputs, and returns a float as a result.

5. Write the body of the function from (4). This function should raise the first argument to the power of the second, by multiplying it by itself that many times (don’t use the inbuilt pow function, write your own). Do not worry about “special” cases like negative powers.

Please do Q.5

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

Answer:

#include <stdio.h>
float power(float num,int powers) //called function
{
float result=1.0; //declaring and initialising variables
for(int i=0;i<powers;i++) //loop to run power times
{
result=result*num; //multiplying the number for power times
}
return result; //returning output
}
int main()   
{
float num; //declaring variables
int powers;
printf("Enter a number >>>"); //taking user input
scanf("%f",&num);
printf("Enter power >>>"); //taking user input
scanf("%d",&powers);
float result =power(num,powers); //calling function
printf("The result is %f",result); //printing output
}

1 //called function 4 //declaring and initialising variables // Loop to run power times { //multiplying the number for power

output:

Enter a number >>>3.5 Enter power >>>4 The result is 150.062500

Add a comment
Know the answer?
Add Answer to:
4. Write a C definition (prototype) for a function called power which takes a float and...
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 a function prototype and a function definition for a function called Numbers that takes three...

    Write a function prototype and a function definition for a function called Numbers that takes three arguments by reference, all of type long, and displays the sum, average, and largest of its three arguments.

  • Write a function that takes two ints and returns the average (float) of those two ints....

    Write a function that takes two ints and returns the average (float) of those two ints. Include a function prototype. In main, call the function with two int inputs of your choice and print the returned value to the output window. The function should not print, just make the calculation an return the average. C language not C++

  • C++ ( Please Read) Write a function prototype and a function definition called getTotal that receives...

    C++ ( Please Read) Write a function prototype and a function definition called getTotal that receives an  array of type double, the number of rows in the array. It returns the total of all elements in the array.

  • I need this in C++ Write a function using the following structure and prototype. struct Stats...

    I need this in C++ Write a function using the following structure and prototype. struct Stats { float avg; //Average value of an integer array float median; //Median value of an integer array int *mode; //array containing the modes int nModes; //number of modes in the array int maxFreq; //max frequency of modes }; Stats *avgMedMode(int *,int); The function takes in an integer array and the size of the array. Then returns a pointer to a structure containing the average,...

  • /*    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 a statement that declares a prototype for a function divide that takes four arguments and...

    Write a statement that declares a prototype for a function divide that takes four arguments and returns no value. The first two arguments are of type int. The last two arguments arguments are pointers to int that are set by the function to the quotient and remainder of dividing the first argument by the second argument. The function does not return a value. (C Program)

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • c++ Write a function that uses recursion to raise a number to a power. The function...

    c++ Write a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program. Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will...

  • C language Write a function to compute the power a^m, where n greaterthanorequalto 0. It should...

    C language Write a function to compute the power a^m, where n greaterthanorequalto 0. It should have the following prototype:/* Sets *p to the n' th power of a and returns 0, except * when n < 0 or p is NULL, in which case it returns -1. */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...

  • Write a function called most_consonants(words) that takes a list of strings called words and returns the...

    Write a function called most_consonants(words) that takes a list of strings called words and returns the string in the list with the most consonants (i.e., the most letters that are not vowels). You may assume that the strings only contain lowercase letters. For example: >>> most_consonants(['python', 'is', 'such', 'fun']) result: 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) result: 'now' The function that you write must use a helper function (either the num_vowels function from lecture, or a similar function that you...

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