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...
a splitting function, split_by Write a splitting function named split_by that takes three arguments an equality...
a splitting function, split_by Write a splitting function named split_by that takes three arguments an equality checking function that takes two values and returns a value of type bool, a list of values that are to be separated, and a list of separators values. This function will split the second list into a list of lists. If the checking function indicates that an element of the first list (the second argument) is an element of the second list (the third...
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...
(a) Write an R function rnormmax that has three arguments, n, mu, and sigm, # and...
(a) Write an R function rnormmax that has three arguments, n, mu, and sigm, # and returns the maximum of a vector of n random numbers from the normal distribution # with mean mu and standard deviation sigm. Make the arguments mu and sigm optional # with default values of 0 and 1, respectively. # (b) Write an R code that replicates rnormmax(n=1000) hundred thousand times and creates # a histogram of the resulting vector via standard hist function.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT