Final Exam Part 1 – Programming Assignments
INSTRUCTIONS FOR PART-1
1) For the programming portion of the midterm, you will be required to submit one or more files containing your program source to the following email address: Cpp.Prep.Course@gmail.com
2) Source consists of .cpp and .h files only. Please do not submit binaries, such as executables (.exe) and libraries (.dll) or anything else. It is likely that Gmail will not even accept such files. They will not be considered when grading your submissions.
3) Do not modify the public interfaces and function signatures of any source code given to you. Any source file given to you will be used to grade your code, so any modification will most likely make your code fail to compile.
4) All source must compile as submitted and without any alterations in order to be graded. Any submissions not meeting this criteria will be summarily awarded zero points without exception.
5) Source files must be well commented. For incorrect or incomplete submissions, comments with algorithms/pseudo code/intentions will net you partial credit.
6) Make sure to try your submissions with multiple test cases focusing especially on corner cases. Make sure to validate all user input (i.e. negative values, zero etc.)
7) Good coding practices and use of object oriented and well organized code might net you brownie points.
8) Turn in one zip file that contains your solutions.
a. The zip file should be named LastName_FirstName.zip where LastName and FirstName are your last and first names.
b. There should be two folders called Solution1 and Solution2 inside the zip file; they contain the corresponding source codes for the problem solutions.
Be sure to follow all instructions to receive full credit!
PROBLEMS FOR PART-1
PROBLEM 1:
For this problem, you will use the random library (C++11) to generate a sequence S of k real values of several distributions and then you will sort them using the MergeSort algorithm.
You will need to prompt the user to enter values for k and the name of the distribution that you will use in your main function. Depending on the distribution selected (uniform, poisson, or normal), you will prompt for the appropriate input parameters. For example, for uniform distribution, you need the min and max values, for poisson, you need lambda, and for normal you will need mean and stdev.
Generate the sequence S using the following functions:
1) Make a function called get_uniform that returns a uniformly distributed real value in the range [min, max]:
double get_uniform(double min, double max);
2) Make a function called get_poisson that returns an integer (casted as double) that follows a poisson distribution with lambda value:
double get_poisson(double lambda);
3) Make a function called get_normal that returns real value that follows a normal distribution with mean value and stdev value:
double get_normal(double mean, double stdev);
4) Make a function called get_sequence that generates the sequence S of k values and returns k:
int get_sequence(double S[], int k, const Args &args);
where args is defined as follows:
struct Args
{
string name;
vector
};
name
args[0]
args[1]
uniform
min
Max
poisson
lambda
normal
mean
stdev
Use the structure Args to pass the arguments collected in the main function to generate the desired sequence. The structure Args is valid ONLY in the following conditions:
The name field has any of the strings under the name column in the table above.
If name is uniform, then the vector args has exactly two elements, the min and the max.
If name is poisson, then the vector args has exactly one element, the lambda value.
If name is normal, then the vector args has exactly two elements, the mean and the standard deviation.
Any other name or argument count makes the structure Args invalid.
Sort the sequence using the MergeSort algorithm as follows:
1) Make a function called sort that sorts the sequence S in order (1 for ascending, 2 for descending, otherwise terminate without sorting) using the MergeSort algorithm. It returns 0 if it sorted, -1 if it did not.
int sort(double S[], int k, int order);
2) Make a function called minimum that returns the minimum value in S.
double min(double S[], int k);
3) Make a function called maximum that finds the maximum value in S.
double max(double S[], int k);
4) Make a function called mean that finds the average of the sequence S.
double mean(double S[], int k);
5) Make a function called stdev that finds the standard deviation of the sequence S.
double stdev(double S[], int k);
6) Make a function called median that finds the median value in S.
double median(double S[], int k);
For the quartile functions, use Method 3 rules from Wikipedia.
1) Make a function called first_quartile that finds the first quartile value in S.
double first_quartile(double S[], int k);
2) Make a function called second_quartile that finds the second quartile value in S. Note: this is the median.
double second_quartile(double S[], int k);
3) Make a function called thrid_quartile that finds the third quartile value in S.
double third_quartile(double S[], int k);
4) Make a function called iqr that finds the interquartile range value in S.
double iqr(double S[], int k);
Output the following in main.cpp:
Unsorted sequence of k values.
Sorted sequence of k values, ascending first, then descending.
The statistics: min, max, mean, stdev, median, q1, q2, q3, and IQR of the sequence.
IMPORTANT:
You must adhere to the naming conventions in boldface throughout this text.
You must use the provided skeleton code for this problem.
You are not allowed to modify anything from the header files provided to you.
Do not modify the public interfaces; including the function signatures given in the skeleton codes.
You may add, however, any additional helper functions and classes in your cpp files.
You must submit two files named EXACTLY as they appear below:
o mergesort.cpp – contains the implementation of the functions and the MergeSort algorithm
o main.cpp – contains the main function
PROBLEMS FOR PART-1
PROBLEM 2:
An Asset Management Firm –XYZ– is planning to allow its customers to open various types of accounts and use them for depositing funds into and withdrawing funds from those accounts. XYZ hired you to help them implement their ideas by writing C++ code. You are asked to create an inheritance hierarchy that XYZ might use to represent customers’ new accounts. All customers can deposit money into their accounts (i.e., credit funds to their accounts), and withdraw money from their accounts (i.e., debit funds from their accounts). The IT manager asked you to implement their ideas as follows:
S-accounts will earn interest on the money they hold.
C-accounts charge a fee per every transaction (i.e., for credit or for debit).
O-accounts earn interest on the money they hold, charge a fee per every transaction, and require the customer to maintain a minimum balance.
Create an inheritance hierarchy containing a base class Acc and derived classes SAcc, CAcc, and OAcc that inherit from class Acc for the accounts described above.
The base class Acc should include one data member called m_balance of type double to represent the account balance. This class should provide a constructor that receives an initial balance and uses it to initialize the data member as follows:
Acc(double init_balance);
You should validate the initial balance in your program to ensure that it is greater than or equal to 0.0. If it is not, the balance should be set to 0.0 and your program should display an error message.
For example, for an invalid initial amount of -$50, display the following error message:
Invalid amount for initial balance: -50.00, the balance is set to 0.0.
to indicate that the initial balance was invalid (use the getNet member function described below to perform this test).
Note: Do not use cin/cout/cerr inside any of the classes described here; if you need to output an error message or any message in particular, do it in your program (the main.cpp file).
The class Acc should provide three member functions:
1) A deposit function that takes a double parameter as the deposit amount and returns a Boolean indicating if the transaction was performed successfully; i.e., the deposit amount is larger than or equal to zero.
2) A withdraw function that takes a double parameter as the withdraw amount and returns a Boolean indicating if the transaction was performed successfully; i.e., the withdraw amount is less than or equal to the account balance.
3) A constant getNet function that takes no parameters and returns a double representing the current account balance.
Display any error messages in your program in case the functions above return false. For example, an invalid deposit amount of -$20 should display the following message:
Could not perform deposit: -20.00.
or an invalid withdraw amount of $500 for an account with $200 balance:
Could not perform withdraw: 500.00.
Note: A valid withdraw amount is one that is bigger or equal to the current balance of the account.
The derived class SAcc should inherit the functionality of an Acc, but also include a data member of type double indicating the interest rate (m_rate, as a percentage) assigned to the Account; you should also verify that the interest rate is a positive value in your program, if not, display an error message. SAcc’s constuctor should receive the initial balance, as well as an initial value for the SAcc’s interest rate as follows:
SAcc(double init_balance, double rate);
SAcc should provide the following public member function:
1) A constant computeInterest function that takes no parameter and returns a double indicating the amount of interest earned by an account. The interest earned is the multiplication of the interest rate and the account balance.
2) A function addInterest that takes no parameter and returns void. This function should add the interest computed by the function computeInterest above to the account balance.
Note: SAcc should inherit member functions deposit and withdraw as is without redefining them.
Derived class CAcc should inherit from base class Acc and include an additional data member of type double that represents the fee (m_fee) charged per transaction; you should verify that the fee is a positive value, if not, display an error message. CAcc’s constructor should receive the initial balance, as well as a parameter indicating a fee amount as follows:
CAcc(double init_balance, double fee);
Class CAcc should redefine member functions deposit and withdraw so that they subtract the fee from the account balance whenever either transaction is performed successfully. CAcc’s versions of these functions should invoke the base-class Acc version to perform the updates to an account balance. Please note the following rules:
a) If the deposit amount generates a fee that makes the account balance below zero in CAcc’s deposit function, return false and display an error message; otherwise return true.
b) For a valid withdraw amount that generates a fee that makes the account balance below zero in CAcc’s withdraw function, return false and display an error message; otherwise return true.
For example, if a deposit of $1 with a transaction fee of $5 is attempted on an account with a current balance of $3, display:
Could not perform deposit: 5.00.
For a withdraw of $40 with a transaction fee of $5 and an account balance of $40, display:
Could not perform withdraw: 40.00.
Derived class OAcc should inherit from base class Acc and include an additional data member of type double representing the fee charged per transaction (m_fee), an additional data member of type double representing the minimum balance (m_min_balance) that may be held in the account, and an additional data member of type double indicating the interest rate (m_rate, as a percentage) assigned to the account. You should verify that the fee, the minimum balance, and the interest rate are positive values, if not, display an error message per offence. OAcc’s constructor should receive the initial balance, the fee, the minimum balance, and the interest rate as follows:
OAcc(double init_balance, double fee, double min_balance, double rate);
OAcc should have a computeInterest function that operates exactly like the one from SAcc. The addInterest function should only add interest to the account if this current balance is above or at the minimum balance; the interest is computed by multiplying the current balance with the interest rate. OAcc’s deposit and withdraw functions should act similarly to those of CAcc. Please note the following rules:
a) For both deposit and withdraw, if the current balance is above or at the minimum balance before the transaction is performed, do not charge a fee and perform the transaction; otherwise, charge the fee and use the next rules.
b) If the deposit amount generates a fee that makes the account balance below zero in OAcc’s deposit function, return false and display an error message like the one in SAcc; otherwise return true.
c) For a valid withdraw amount that generates a fee that makes the account balance below zero in OAcc’s withdraw function, return false and display an error message like the one in SAcc; otherwise return true.
For example, if addInterest is called when the current balance is below the minimum balance, display the following error message:
Could not add interest: current balance is below minimum balance.
IMPORTANT:
You must adhere to the naming conventions in boldface throughout this text.
You must use the provided skeleton code for this problem.
You are not allowed to modify anything from the header files provided to you.
Do not modify the public interfaces; including the function signatures given in the skeleton codes.
You may add, however, any additional helper functions and classes in your cpp files.
You must submit eight files named EXACTLY as they appear below:
o Note: Acc.h is given to you, do not submit it.
o Acc.cpp – contains implementation for Acc class
o SAcc.h – contains class definition for SAcc
o SAcc.cpp – contains implementation for SAcc class
o CAcc.h – contains class definition for CAcc
o CAcc.cpp – contains implementation for CAcc class
o OAcc.h – contains class definition for OAcc
o OAcc.cpp – contains implementation for OAcc class
o main.cpp: contains the main function
/docProps/thumbnail.jpeg