Using #include <stdio.h>
6) Write a program that computes X^Y (X to the power of Y). You are
required to use a while loop to solve the problem. Do not use the
pow function from the “math.h” library. You may hard code the
variables X and Y in the beginning of your program.
Hint: 5^4 =5*5*5*5
SOURCE CODE IN C:
#include <stdio.h>
//function to calculate x to the power y and return it
double power(int x, int y)
{
//res stores x to power of absolute value of y
int res=1,copy=y;
if(y<0)
copy=-copy; //storing absolute value of y in copy
//calculating x to power of absolute value of y
while(copy!=0)
{
res*=x;
copy-=1;
}
if(y<0) //if y is negative, we return 1/(x to power of absolute value of y)
return 1.0/res;
else //else we return x to power of absolute value of y
return res;
}
int main(void)
{
//testing function
printf("2 to the power 4: %.2f\n",power(2,4));
printf("3 to the power -2: %.2f\n",power(3,-2));
printf("4 to the power 0: %.2f\n",power(4,0));
return 0;
}
OUTPUT:
Using #include <stdio.h> 6) Write a program that computes X^Y (X to the power of Y)....
#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: 2.1 Specification Write an ANSI-C program that reads input from the...
Two positive integers are entered through the keyboard. Write a program to find the value of one number raised to the power of another. You are NOT allowed to use the pow function in math.h library file
Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a print(text, number) function and the main() function. The power(x,y) function returns an integer result that is calculated by raising a number x (integer) to a power y (integer). The second argument y (exponent) of the function can not exceed 100. If the second argument exceeds 100, the function should return -1. Your power(x,y) function must be able to take either 1 or 2 integer...
What to do Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of...
Write a program that contains a main function, a “double custom_operator_function(int op_type, double x, double y)”, a “double min(double,double)”, a double max(double x,double y), and a double pow(double x, int y). The main function is used to call the custom operator function. Next, the custom operator function calls the min/max/pow functions based on the op_type. From the main, call the custom operator function with the three inputs 1/-3/5, 2/4/5, and 3/4/5. Print the result of each input to screen. i)...
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ /* */ /* */ /* This program computes average power, average magnitude */ /* and zero crossings from a speech signal. */ #include <stdio.h> #include <math.h> #define MAXIMUM 50 int main(void) { /* Declare variables */ int k=0, npts=30; double speech[MAXIMUM]={0.000000,-0.023438,-0.031250,-0.031250,-0.039063,-0.039063,-0.023438,0.000000,0.023438,0.070313,-0.039063,-0.039063,0.046875,0.101563,0.117188,0.101563,0.070313,0.054688,0.023438,0.000000,-0.031250,-0.039063,-0.070313,-0.070313,-0.070313,-0.070313,-0.062500,-0.046875,-0.039063,-0.031250}; /* Declare the function prototypes */ /* Compute and print statistics. */ printf("\n SPEECH DATA "); print(,,);//complete the statement printf("\n"); printf(" SPEECH STATISTICS : \n"); printf(" average power: %f \n",);//complete the statement printf(" average magnitude: %f...
Write a MIPSzy subroutine, power, that accepts 2 arguments x and y and computes x^y . You can assume that x and y are both larger than 0. The main program passes the two parameters and receives the return value through the program stack. addi $t0, $zero, 5100 top: lw $t1, 0($t0) beq $t1, $zero, done addi $sp, $sp, -4 sw $t1, 0($sp) addi $t2, $zero,4 addi $sp, $sp, -4 sw $t2, 0($sp) addi $sp, $sp, -4 jal power lw...
i need this program in DEV C and starts with include stdio.h only Write a program that receives two input values: a float number x=0, and an integer N>3. The program should output Y, which is the product of all the N numbers divided by x.
Consider the problem of computing the power function pow(n,x) = n^x using only multiplications. The first approach is to perform x multiplications ($n \cdot n \cdot n \cdot \ldots \cdot n$, x times). Find a better, recursive algorithm to solve this problem (by better, we mean one that uses fewer than $x$ multiplications). Write down the pseudocode for this new function, and then analyze the runtime of that recursive program by first writing out the recurrence relation $T(n, x)$ that...
[Arithmetic Expression] Write a program that computes y from the following arithmetic expression: y = mx + c. Use all values as signed words which are stored as variables in the memory. Show your results for different values of m, x and c. please answer in assembly