i need to do this to my project 1 Goal: We will modify Project 1 code to use STL
ID: 3701695 • Letter: I
Question
i need to do this to my project 1
Goal:
We will modify Project 1 code to use STL vectors. The Statistics Class will be modified so that the data is stored in a vector of doubles. This change should not have much impact on BankAccount Class (except for the constructor of it).
Design:
We will modify Project 1 code to use STL vectors. The Statistics Class will be modified so that the data is stored in a vector of doubles. This change should not have much impact on BankAccount Class (except for the constructor of it).
Bankaccount.h file
#include <iostream>
#include "statistics.h"
using namespace std;
class BankAccount
{
public:
statistics deposits;
statistics withdrawals;
BankAccount();
// ~BankAccount();
void deposit(double amount);
void withdrawal(double amount);
double getBalance();
double getTotalDeposits();
double getTotalWithdrawals();
double getAverageDeposit();
double getAveragWithdrawal();
double getMinDeposit();
double getMinWithdrawal();
double getMaxDeposit();
double getMaxWithdrawal();
double getVarOfDeposits();
double getVarOfWithdrawal();
double getStdevOfdeposits();
double getStdevOfWithdrawal();
double printAllStats();
void print();
void test();
};
Bankaccount.cpp
#include <iostream>
//#include "statistics.h"
#include "BankAccount.h"
//
using namespace std;
BankAccount::BankAccount() {
}
//~BankAccount();
void BankAccount::deposit(double amount) {
deposits.add(amount);
}
void BankAccount::withdrawal(double amount) {
withdrawals.add(amount);
}
double BankAccount::getTotalDeposits() {
return deposits.sum();
}
double BankAccount::getTotalWithdrawals() {
return withdrawals.sum();
}
double BankAccount::getBalance() {
double totaldeposits = BankAccount::getTotalDeposits();
double totalwithdrawals = BankAccount::getTotalWithdrawals();
return totaldeposits - totalwithdrawals;
}
double BankAccount::getAverageDeposit() {
return deposits.average();
}
double BankAccount::getAveragWithdrawal() {
return withdrawals.average();
}
double BankAccount::getMinDeposit() {
return deposits.min();
}
double BankAccount::getMinWithdrawal() {
return withdrawals.min();
}
double BankAccount::getMaxDeposit() {
return deposits.max();
}
double BankAccount::getMaxWithdrawal() {
return deposits.max();
}
double BankAccount::getVarOfDeposits() {
return deposits.var();
}
double BankAccount::getVarOfWithdrawal() {
return withdrawals.var();
}
double BankAccount::getStdevOfdeposits() {
return deposits.stdev();
}
double BankAccount::getStdevOfWithdrawal() {
return withdrawals.stdev();
}
double BankAccount::printAllStats() {
cout << "Current balance is: " << BankAccount::getBalance() << endl;
cout << "Total deposits made: " << BankAccount::getTotalDeposits() << endl;
cout << "Total withdrawals made: " << BankAccount::getTotalWithdrawals() << endl;
cout << "Average deposit amount: " << BankAccount::getAverageDeposit() << endl;
cout << "Average withdrawal amount: " << BankAccount::getTotalWithdrawals() << endl;
cout << "Minimum deposit amount: " << BankAccount::getMinDeposit() << endl;
cout << "Minimum withdrawal amount: " << BankAccount::getMaxWithdrawal() << endl;
cout << "Maximum deposit amount: " << BankAccount::getMaxDeposit() << endl;
cout << "Maximum withdrawal amount: " << BankAccount::getMaxWithdrawal() << endl;
cout << "Variance of all the deposits: " << BankAccount::getVarOfDeposits() << endl;
cout << "Variance of all the withdrawal: " << BankAccount::getVarOfWithdrawal() << endl;
cout << "Standard deviation of all the deposits: " << BankAccount::getStdevOfdeposits() << endl;
cout << "Standard deviation of all the withdrawal: " << BankAccount::getStdevOfWithdrawal() << endl;
return 0;
}
void test() {
BankAccount ba;
ba.deposit(500.0);
ba.deposit(600.0);
ba.deposit(900.0);
ba.withdrawal(912.0);
ba.withdrawal(120.0);
ba.deposit(9000.0);
double balance = ba.getBalance();
if (!balance) {
cout << "Could not print balance " << endl;
}
ret = ba.deposit(10.0);
if (!ret) {
cout << "Could not deposit the amount ---> Failed Transaction";
}
ret = ba.withdrawal(100.0);
if (!ret) {
cout << "Could not make the withdrawal ---> Failed Transaction";
}
ret = ba.printAllStats();
if (!ret) {
cout << "Could not print statistics";
}
}
void print() {
BankAccount ba;
ba.deposit(500.0);
ba.deposit(600.0);
ba.deposit(900.0);
ba.withdrawal(912.0);
ba.withdrawal(120.0);
ba.deposit(9000.0);
ba.printAllStats();
}
Statistics.h
class statistics {
private:
double* arr;
int arr_size;
public:
statistics();
statistics(double* array, int size);
void add(double value);
// add(value: double): void
double get(int i);
// get(i: int): double
void set(int i, double data);
// set(i: int, data: double): void
double sum();
// sum(): double
double average();
// average(): double
double min();
// min(): double
double max();
double var();
// var(): double
double stdev();
// stdev(): double
void printStats();
// printStats(): void
};
Statistic.cpp
#include <iostream>
#include<cmath>
#include "statistics.h"
using namespace std;
statistics::statistics() {
arr_size = 0;
}
statistics::statistics(double* array, int size) {
for (int i = 0; i < size; i++) {
arr[i] = array[i];
}
arr_size = size;
}
void statistics::add(double value) {
double *temp = new double[arr_size + 1];
for (int i = 0; i < arr_size; i++) {
temp[i] = arr[i];
}
temp[arr_size] = value;
arr_size += 1;
arr = temp;
}
double statistics::get(int i) {
return arr[i];
}
void statistics::set(int i, double data) {
arr[i] = data;
}
double statistics::sum() {
double sum = 0.0;
for (int i = 0; i < arr_size; i++) {
sum += arr[i];
}
return sum;
}
double statistics::average() {
return statistics::sum() / arr_size;
}
double statistics::min() {
double min_ = arr[0];
for (int i = 1; i < arr_size; i++) {
if (arr[i] < min_) {
min_ = arr[i];
}
}
return min_;
}
double statistics::max() {
double max_ = arr[0];
for (int i = 1; i < arr_size; i++) {
if (arr[i] > max_) {
max_ = arr[i];
}
}
return max_;
}
double statistics::var() {
double mean = statistics::average();
double variance = 0.0;
for (int i = 0; i < arr_size; ++i) {
variance += pow(arr[i] - mean, 2);
}
return variance;
}
double statistics::stdev() {
return sqrt(statistics::var());
}
void statistics::printStats() {
cout << "Sum of elements of the array: " << statistics::sum() << endl;
cout << "Average of elements of the array: " << statistics::average() << endl;
cout << "Minimum of all elements of the array: " << statistics::min() << endl;
cout << "Maximum of all elements of the array: " << statistics::max() << endl;
cout << "variance of all elements of the array: " << statistics::var() << endl;
cout << "Standard Deviation of all the elements of the array: " << statistics::stdev() << endl;
}
Explanation / Answer
BankAccount.h
#include <iostream>
#include "statistics.h"
using namespace std;
class BankAccount
{
public:
statistics deposits;
statistics withdrawals;
BankAccount();
// ~BankAccount();
void deposit(double amount);
void withdrawal(double amount);
double getBalance();
double getTotalDeposits();
double getTotalWithdrawals();
double getAverageDeposit();
double getAveragWithdrawal();
double getMinDeposit();
double getMinWithdrawal();
double getMaxDeposit();
double getMaxWithdrawal();
double getVarOfDeposits();
double getVarOfWithdrawal();
double getStdevOfdeposits();
double getStdevOfWithdrawal();
double printAllStats();
void print();
void test();
};
Bankaccount.cpp
#include <iostream>
//#include "statistics.h"
#include "BankAccount.h"
//
using namespace std;
BankAccount::BankAccount() {
}
//~BankAccount();
void BankAccount::deposit(double amount) {
deposits.add(amount);
}
void BankAccount::withdrawal(double amount) {
withdrawals.add(amount);
}
double BankAccount::getTotalDeposits() {
return deposits.sum();
}
double BankAccount::getTotalWithdrawals() {
return withdrawals.sum();
}
double BankAccount::getBalance() {
double totaldeposits = BankAccount::getTotalDeposits();
double totalwithdrawals = BankAccount::getTotalWithdrawals();
return totaldeposits - totalwithdrawals;
}
double BankAccount::getAverageDeposit() {
return deposits.average();
}
double BankAccount::getAveragWithdrawal() {
return withdrawals.average();
}
double BankAccount::getMinDeposit() {
return deposits.min();
}
double BankAccount::getMinWithdrawal() {
return withdrawals.min();
}
double BankAccount::getMaxDeposit() {
return deposits.max();
}
double BankAccount::getMaxWithdrawal() {
return deposits.max();
}
double BankAccount::getVarOfDeposits() {
return deposits.var();
}
double BankAccount::getVarOfWithdrawal() {
return withdrawals.var();
}
double BankAccount::getStdevOfdeposits() {
return deposits.stdev();
}
double BankAccount::getStdevOfWithdrawal() {
return withdrawals.stdev();
}
double BankAccount::printAllStats() {
cout << "Current balance is: " << BankAccount::getBalance() << endl;
cout << "Total deposits made: " << BankAccount::getTotalDeposits() << endl;
cout << "Total withdrawals made: " << BankAccount::getTotalWithdrawals() << endl;
cout << "Average deposit amount: " << BankAccount::getAverageDeposit() << endl;
cout << "Average withdrawal amount: " << BankAccount::getTotalWithdrawals() << endl;
cout << "Minimum deposit amount: " << BankAccount::getMinDeposit() << endl;
cout << "Minimum withdrawal amount: " << BankAccount::getMaxWithdrawal() << endl;
cout << "Maximum deposit amount: " << BankAccount::getMaxDeposit() << endl;
cout << "Maximum withdrawal amount: " << BankAccount::getMaxWithdrawal() << endl;
cout << "Variance of all the deposits: " << BankAccount::getVarOfDeposits() << endl;
cout << "Variance of all the withdrawal: " << BankAccount::getVarOfWithdrawal() << endl;
cout << "Standard deviation of all the deposits: " << BankAccount::getStdevOfdeposits() << endl;
cout << "Standard deviation of all the withdrawal: " << BankAccount::getStdevOfWithdrawal() << endl;
return 0;
}
void test() {
BankAccount ba;
ba.deposit(500.0);
ba.deposit(600.0);
ba.deposit(900.0);
ba.withdrawal(912.0);
ba.withdrawal(120.0);
ba.deposit(9000.0);
double balance = ba.getBalance();
if (!balance) {
cout << "Could not print balance " << endl;
}
int ret;
ret = ba.deposit(10.0);
if (!ret) {
cout << "Could not deposit the amount ---> Failed Transaction";
}
ret = ba.withdrawal(100.0);
if (!ret) {
cout << "Could not make the withdrawal ---> Failed Transaction";
}
ret = ba.printAllStats();
if (!ret) {
cout << "Could not print statistics";
}
}
void print() {
BankAccount ba;
ba.deposit(500.0);
ba.deposit(600.0);
ba.deposit(900.0);
ba.withdrawal(912.0);
ba.withdrawal(120.0);
ba.deposit(9000.0);
ba.printAllStats();
}
statistics.h
class statistics {
private:
vector<double> arr;
int arr_size;
public:
statistics();
statistics(vector<double> array, int size);
void add(double value);
// add(value: double): void
double get(int i);
// get(i: int): double
void set(int i, double data);
// set(i: int, data: double): void
double sum();
// sum(): double
double average();
// average(): double
double min();
// min(): double
double max();
double var();
// var(): double
double stdev();
// stdev(): double
void printStats();
// printStats(): void
};
Statistics.cpp
#include <iostream>
#include<cmath>
#include "statistics.h"
using namespace std;
statistics::statistics() {
arr_size = 0;
}
statistics::statistics(vector<double> array, int size) {
for (int i = 0; i < size; i++) {
arr[i] = array[i];
}
arr_size = size;
}
void statistics::add(double value) {
double *temp = new double[arr_size + 1];
for (int i = 0; i < arr_size; i++) {
temp[i] = arr[i];
}
temp[arr_size] = value;
arr_size += 1;
arr = temp;
}
double statistics::get(int i) {
return arr[i];
}
void statistics::set(int i, double data) {
arr[i] = data;
}
double statistics::sum() {
double sum = 0.0;
for (int i = 0; i < arr_size; i++) {
sum += arr[i];
}
return sum;
}
double statistics::average() {
return statistics::sum() / arr_size;
}
double statistics::min() {
double min_ = arr[0];
for (int i = 1; i < arr_size; i++) {
if (arr[i] < min_) {
min_ = arr[i];
}
}
return min_;
}
double statistics::max() {
double max_ = arr[0];
for (int i = 1; i < arr_size; i++) {
if (arr[i] > max_) {
max_ = arr[i];
}
}
return max_;
}
double statistics::var() {
double mean = statistics::average();
double variance = 0.0;
for (int i = 0; i < arr_size; ++i) {
variance += pow(arr[i] - mean, 2);
}
return variance;
}
double statistics::stdev() {
return sqrt(statistics::var());
}
void statistics::printStats() {
cout << "Sum of elements of the array: " << statistics::sum() << endl;
cout << "Average of elements of the array: " << statistics::average() << endl;
cout << "Minimum of all elements of the array: " << statistics::min() << endl;
cout << "Maximum of all elements of the array: " << statistics::max() << endl;
cout << "variance of all elements of the array: " << statistics::var() << endl;
cout << "Standard Deviation of all the elements of the array: " << statistics::stdev() << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.