162k views
1 vote
1. Write a try-catch block. In try block, do the following thing: if the balance is less than 100, throw an InsufficientFund exception; else print out the message saying that there are enough money in the account. In catch block, simply print out the message saying that there is not enough money in the account.

2. Write a template function template
T max(T& variable1, T& variable2)
That returns the larger value of variable1 and variable 2.
Assume that class T has overloaded the operator >.

1 Answer

4 votes

Answer:

(Assuming C++):

#1

try{

if (balance < 100)

{

throw "InsufficientFund";

}

else

{

printf("Sufficient funds in account.\\");

}

}

catch{

printf("Insufficient funds in account.\\");

}

---------------------------------------------------------------------------------------

#2

template <typename T>

T max (T& variable1, T& variable2)

{

return variable1 >= variable2 ? variable1 : variable2;

}

Step-by-step explanation:

For this, the answer assumes the use of the C++ language since not specified in the problem.

For #1, the use of the try-catch statement is simply to check the value of the funds within an account. The question ask to check if the balance is under 100, to throw an "InsufficientFund" exception to be caught by the catch loop. Otherwise, print there is enough funds. If the exception is caught, the catch loop should simply print insufficient funds to the console. The above code accomplishes each of these things by assuming a variable called "balance" holds the value of the account and has already been declared. Additionally, the code uses the printf function to display the messages to the screen (Assuming stdio.h header has been included).

For #2, a template function called max was created to determine if variable1 or variable 2 had a greater value, and return the greatest variable. The code uses the ternary operator to test the expression greater than or equal to and returns variable1 if greater or equal, otherwise, it returns variable2.

Side note, I haven't coded in C++ in years so some syntax may be wrong, but either way this is the concept of the two questions asked (I think I'm syntactically correct though).

Cheers.

User Jason George
by
5.4k points