Starting from:

$25

CS201 - Home Exam 2 - Solved

Introduction
The aim of this take-home exam is to make students comfortable with parametric functions (return types, parameters, code reuse, pass by value, pass by reference etc.) in C++ as well as using computational approaches to solve mathematical questions. You will practice on modularity and code reuse. It is possible to finish this homework without using functions, but it is a must to use them.

In this homework, you will solve a budget problem using a C++ program. Given prices of 3 objects, you will take 3 quantities for these objects and a total budget as inputs and without knowing which quantity refers to which object you need to calculate the most expensive permutation of these objects that does not exceed the budget.

You cannot write all of your code under the main function, i.e. you must write user-defined functions and use them.



Inputs, Flow of the Program and Outputs
The inputs of the program and their order are explained below. It is extremely important to follow this order with the same characters since we automatically process your programs. Thus, your work will be graded as 0 unless the order is entirely correct. Please see the "Sample Runs" section for some examples.

The prompts of the input statements to be used has to be exactly the same as the prompts of the "Sample Runs".

The program starts by asking the user's total budget in TL. This number will indicate how much a user can spend and therefore, it cannot be negative. You should do an input check on the budget such that if the user enters a negative number, the program should display an error message saying, “Budget cannot be negative.”

Then, your program should ask the user quantities of 3 objects. These 3 values must be positive. If one of them is entered as nonpositive, you should display a message “All quantities must be positive.” So, you should perform another input check on quantities.

After inputs pass all checks, you will be able to calculate the budget problem. As a result, you need to display the user's remaining money. If the user cannot afford given quantities in any way, you should prompt a message “You cannot afford any of the permutations with these quantities and budget”.

After finishing the first user, your program needs to do everything one more time. In other words, your program will take inputs of the first user, calculate, and print an appropriate message, then asks for the second user’s inputs and repeat the calculations. When you complete the task twice, prompt a goodbye message and finish your program. Please see the "Sample Runs" section for some examples.

Use of Functions
You are expected to implement a total of seven user-defined functions (other than main).

Function1 (returns bool type): This function is a parametric boolean function that performs input checks for the validity of the user's budget. It will take an integer variable as parameter, check if it is negative, and return true or false accordingly.

Function2 (returns bool type): This function is also a parametric boolean function. It will perform the input check for the validity of given quantities. It will take three integers as parameters, check if they are all positive and return true and return false otherwise.

Function3 (returns int type): You will write a function that takes three integers (quantities) as parameters, calculates the total price with these given quantities and returns it. For this function, please take the prices of objects as 5 TL, 10 TL and 15 TL respectively.

Function4 (returns int type): You will write another function that will perform a conditional maximum operation. It will take 3 integers as parameters: previous_max_price, current_price and budget. It will return previous_max_price if

-      previous_max_price > current_price or

-      current_price > budget and returns current_price otherwise.

Function5 (returns nothing, void): You will write a function that utilizes Function3 and Function4. It takes three quantities, budget and max_price (total of 5) as parameters (all of them are int). All parameters except max_price should be passed by value, and max_price should be passed by reference. It first calculates the current_price using Function3 and 3 quantity parameters. Then, it will call Function4 using max_price, current_price and budget, and update max_price. Since it is a void function, no return is needed. However, you will use a reference parameter (max_price) to apply the same idea.

Function6 (returns int type): This function will utilize Function5. It will take 3 quantities and budget as parameters (all of them are int). Then it will use

Function5 to evaluate all the permutations of quantities. In other words, you will call Function5 six times for each permutation and extract the maximum priced value that does not exceed the budget. After that it will return max_price.

Function7 (returns nothing, void): This function will be your runner function. Since you will do everything twice, you need to modularize your code so that when you call this function in main twice, it will repeat everything you implemented.


More products