Question

In: Computer Science

Write code to define a function named mymath. The function has three arguments in the following...

Write code to define a function named mymath. The function has three arguments in the following order: Boolean, Integer, and Integer. It returns an Integer.
The function will return a value as follows:

1. If the Boolean variable is True, the function returns the sum of the two integers.
2. If the Boolean is False, then the function returns the value of the first integer - the value of the second Integer.

Solutions

Expert Solution

The answer to this question is as follows:

The code is as follows:

#include <iostream>

using namespace std;
//Decalring the function mymath that takes three parameters
int mymath(bool value,int a,int b)
{
if(value==true)
{
//if the boolean value is true return sum
return a+b;
}
//otherwise return first value - second value
else
{
return a-b;
}
}

int main() {
int x=10;
int y=25;
bool val=true;
//calling the function and printg the output
cout<<"The ouput is "<<mymath(val,x,y);
}

The input and output is provided in the screenshot below:


Related Solutions

Write python 3 code to define a function that uses three arguments, and returns a result....
Write python 3 code to define a function that uses three arguments, and returns a result. The function returns True if the first argument is more than or equal to the second argument and less than or equal to the third argument, otherwise it returns False. Write a python 3 code for function main, that does the following: creates a variable and assign it the value True. uses a while loop which runs as long as the variable of the...
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the...
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the two arrays contain the same values (but not necessarily in the same order), otherwise it returns 0. Your solution must not sort either array or a copy of either array! Also you must not modify either array, i.e., the values in the arrays upon return from the function must be the same as when the function was called. Note that the arrays do not...
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
write a python program that include a function named activity_selection() and take in two arguments, first...
write a python program that include a function named activity_selection() and take in two arguments, first one would be the number of tasks and the second argument would be a list of activities. Each activity would have an activity number, start time and finish time. Example activity_selection input and output: activity_selection (11, [[1, 1, 4 ], [2, 3, 5], [3, 0, 6], [4, 5, 7], [5, 3, 9], [6, 5, 9],[7, 6, 10], [ 8, 8, 11], [ 9, 8,...
1. define a class that has three different constructors. 2. write code that overrides the following...
1. define a class that has three different constructors. 2. write code that overrides the following method public double sqr(double num1) { return num1*num1; }
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The name of the file, a pointer to an array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to file, and then close the file. Write another function named fileToArray. This function should accept 3 arguments: the name of the file, a pointer, to an int array, and the size of...
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If case is equal to “lower” the function...
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
Write a MATLAB function named myaccel that accepts three inputs and has a single output. Inputs:...
Write a MATLAB function named myaccel that accepts three inputs and has a single output. Inputs: a list of time values a list of position values a single time value. Output: a single number which is obtained by numerically differentiating position with respect to time twice (forward difference method) and then interpolating the results based on the third input. Example: time=0:10; position=time.^3; myaccel(time,position,2.8) % should return 22.8
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT