Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++ FORMAT The attached account info file consists of a triple of information

ID: 3859998 • Letter: I

Question

IN C++ FORMAT

The attached account info file consists of a triple of information.

A valid account number

The current account balance

The maximum account balance

Using the account info file write a program using appropriate functions that lets a user:

Enter a given account number

Enter a charge amount (positive values only)

This program then validates the account number and the amount. Then it determines if the current requested charge will exceed the account balance.

if a valid account, print a validation message

If a valid amount then print if the charge is accepted

if anything is invalid print a message indicating what is wrong.

Store the values in the file in appropriate vectors. once stored in the vectors, use the selection sort method to sort the array and use a binary search method to find the account number.

Do not use the algorithms library for either the sort or search, write the functions yourself.

txt file

Explanation / Answer

c++ code:

#include <bits/stdc++.h>

#include <iostream>   

using namespace std;

int bin_search(vector<float> arr, int l, int r, float x)

{

if (r >= l)

{

int mid = l + (r - l)/2;

if (arr[mid] == x) return mid;

if (arr[mid] > x) return bin_search(arr, l, mid-1, x);

return bin_search(arr, mid+1, r, x);

}

return -1;

}

vector<float> selectionSort(vector<float> arr)

{

int n = arr.size();

for (int j = 0; j < n - 1; ++j)

{

int min = j;

for (int i = j+1; i < n; ++i) {

if (arr.at(min) > arr.at(i)) {

min = i;

}

}

if (min != j)

swap(arr.at(j), arr.at(min));

}

return arr;

}

int main()

{

string line,filename;

filename = "input.txt";

ifstream myfile (filename.c_str());

vector<float> account_nos;

vector<float> account_nos_copy;

vector<float> cur_balances;

vector<float> max_balances;

if (myfile.is_open())

{

while ( getline (myfile,line) )

{

string buf; // Have a buffer string

stringstream ss(line); // Insert the string into a stream

vector<string> tokens; // Create vector to hold our words

while (ss >> buf)

tokens.push_back(buf);

account_nos.push_back(atof(tokens[0].c_str()));

cur_balances.push_back(atof(tokens[1].c_str()));

max_balances.push_back(atof(tokens[2].c_str()));

}

myfile.close();

}

else

{

cout << "Unable to open file" << endl;

exit(1);

}

account_nos_copy = selectionSort(account_nos);

// sort (account_nos_copy.begin(), account_nos_copy.end());

float acc_no;

float charge;

cout << "Enter Account Number!" << endl;

cin >> acc_no;

if( bin_search(account_nos_copy,0,account_nos_copy.size() - 1, acc_no) == -1 )

{

cout << "No account found in with account number = " << (int)acc_no << endl;

}

else

{

cout << (int)acc_no << " is a valid account number!" << endl;

int index = 0;

for (int i = 0; i < account_nos.size(); ++i)

{

if(account_nos[i] == acc_no)

{

index = i;

break;

}

}

cout << "Enter the charge!" << endl;

cin >> charge;

if(charge < 0)

{

cout << "Invalid charge! charge must be > 0" << endl;

}

else

{

if(cur_balances[index] >= charge)

{

cout << "Cur_balance = " << cur_balances[index] << " for account_no " << (int)acc_no << " so we can deduct charge = " << charge << " from this account! ";

}

else

{

cout << "We can not deduct charge from this account since current balance is lower than charge!";

}

}

}

return 0;

}

Sample Output 1:

Enter Account Number!
7825877
7825877 is a valid account number!
Enter the charge!
500
Cur_balance = 501.83 for account_no 7825877 so we can deduct charge = 500 from this account!

Sample Output 2:

Enter Account Number!
1245124
No account found in with account number = 1245124