Question

Write a program that contains a main function, a “double custom_operator_function(int op_type, double x, double y)”,...

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) The custom operator function calls the “min”, “max”, or “pow” function based on the value of the parameter op_type (1=min), (2=max), (3=pow) and returns the result of the called function. The arguments passed to the min/max/pow function are the parameters x and y. ii) Do not use the min, max, and pow functions from the math library.

Please use C programming.

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

Program:

#include<stdio.h>

//function min()
double min(double x,double y)
{
   if(x<y)
   {
       return x;
   }
   return y;
}

//function max()
double max(double x,double y)
{
   if(x>y)
   {
       return x;
   }
   return y;
}

//function power() as there is built-in function pow(), so to avoid confusion
double power(double x,int y)
{
   double v=1;
   int i;
   for(i=1;i<=y;i++)
   {
       v=v*x; //x is multiplied y times
   }
   return v;
}

//function custom_operator_function
double custom_opreator_function(int op_type,double x,double y)
{
   double m,n,o;
   if(op_type==1)
   {
       m= min(x,y);
   }
   else if(op_type==2)
   {
       n= max(x,y);
   }
   else if(op_type==3)
   {
       o=power(x,y); //there is built-in function pow()
  
   }
}


int main()
{
   int p;
   double i,j,k;
  
   //value for op_type is confined here
   printf("Enter the value for p: 1/2/3-");
   scanf("%d", &p);
  
   printf("Enter the value for i: ");
   scanf("%lf", &i);
  
   printf("Enter the value for j: ");
   scanf("%lf", &j);
      
   k= custom_opreator_function(p,i,j);
   printf("Result: %lf", k);
  
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a program that contains a main function, a “double custom_operator_function(int op_type, double x, double y)”,...
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 base, power; printf("enter base and power:...

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

  • Consider the following functions: int min(int x, int y) { return x < y ? x...

    Consider the following functions: int min(int x, int y) { return x < y ? x : y; } int max(int x, int y) { return x < y ? y : x; } void incr(int *xp, int v) { *xp + = v; } int square(int x) { return x*x; } Assume x equals 10 and y equals 100. The following code fragment calls these functions int low = min (x,y); int high = max (x,y); for (i low;...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • 61. The following program is accepted by the compiler:         int sum( int x, int y...

    61. The following program is accepted by the compiler:         int sum( int x, int y )         {             int result;             result = x + y;            }                            T__   F__ 62. The following implementation is accepted by the compiler:         void product()         {             int a; int b; int c; int result;             cout << "Enter three integers: ";             cin >> a >> b >> c;             result = a * b * c;            ...

  • Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a...

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

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

  • C++ Code Pass By Reference Practice Write the following functions for basic array operations based on...

    C++ Code Pass By Reference Practice Write the following functions for basic array operations based on the function descriptions given below. Write the following functions for basic array operations based on the function descriptions given below. FindMinArray: This function will search an array of integers (passed to the function as a parameter) for its minimum value, then return that value. SumArray: This function will sum an array of integers (passed to the function through a parameter) then return that sum....

  • #include <iostream> #include <cmath> using namespace std; int main() { int x; int y; int z;...

    #include <iostream> #include <cmath> using namespace std; int main() { int x; int y; int z; int r1; int r2; cin >> x; cin >> y; cin >> z; r1 = 4* pow(x,3)-5*pow(y,2)+3*z; r2 = r1+7*pow(x,3)-2*pow(y,2)+11*z; cout << "r1="; cout << 4* pow(x,3)-5*pow(y,2)+3*z; cout << endl; cout << "r2="; cout << r1+7*pow(x,3)-2*pow(y,2)+11*z; cout << endl; cout << "r3="; cout << r2-9*pow(x,3)+22*pow(y,2)-6*z; cout << endl; return 0; } 1-115 Your output r2-395 13-186 5:Compare output Output differs. See highlights below. -163...

  • Look at the following function definition: def my_function(x, y): return x[y] a. Write a statement that...

    Look at the following function definition: def my_function(x, y): return x[y] a. Write a statement that calls this function and uses keyword arguments to pass ‘testing’ into x and 2 into y. b. What will be printed when the function call executes? 6. Write a statement that generates a random number in the range I'm using python to solve this this is what i did def main():      result= my_function(x='testing', y=2)      print(result) def my_function(x,y):      return x[y] main()

  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

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