In: Computer Science
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
5658845 6559.14 7500.00 4520125 1352.83 5000.00 7895122 142.41 5000.00 8777541 4563.07 5000.00 8451277 6160.57 10000.00 1302850 3741.25 10000.00 8080125 2374.01 10000.00 1562555 6549.02 7500.00 5552012 1089.54 10000.00 5050552 491.00 7500.00 7825877 501.83 10000.00 1250255 1427.56 7500.00 1005231 4725.82 5000.00 6545231 9478.78 10000.00 3852085 2211.99 10000.00 7576651 246.55 7500.00 7881200 1335.15 5000.00 4581002 930.58 5000.00
C++ program:
#include <iostream>
#include <fstream>
using namespace std;
int binarySearch(int accNumber,int accNum[],int n)
{
int first = 0;
int last = n-1;
int middle = (first+last)/2;
while (first <= last)
{
if(accNum[middle] <
accNumber)
{
first = middle +
1;
}
else if(accNum[middle] ==
accNumber)
{
return
middle;
break;
}
else
{
last = middle -
1;
}
middle = (first + last)/2;
}
if(first > last)
{
return -1;
}
}
int main()
{
//declare vectors to store data
int accNum[20];
double currBalance[20];
double maxAccBalance[20];
int n=0;
//opening the file , if it exists
ifstream infile("info.txt");
if(!infile)
{
cout<<"Please provide input
file info.txt";
exit(0);
}
//reading the file info.txt
while(!infile.eof())
{
cin>>accNum[n]>>currBalance[n]>>maxAccBalance[n];
n++;
}
//selection sort
int minAccLoc;
for (int i = 0; i < n; i++)
{
minAccLoc = i;
for (int j = i + 1; j < n; j++)
{
if (accNum[j] < accNum[minAccLoc])
{
minAccLoc = j;
}
}
//Swap the values
int temp=accNum[i];
accNum[i]=accNum[minAccLoc];
accNum[minAccLoc]=temp;
double t=currBalance[i];
currBalance[i]=currBalance[minAccLoc];
currBalance[minAccLoc]=t;
t=maxAccBalance[i];
maxAccBalance[i]=maxAccBalance[minAccLoc];
maxAccBalance[minAccLoc]=t;
}
int accNumber;
double charge;
while(1)
{
cout<<endl<<"Enter the user account
number: ";
cin>>accNumber;
int loc=binarySearch(accNumber,accNum,n);
if(loc==-1)
cout<<endl<<"Invalid account
number";
else
{
cout<<endl<<"Valid
account number";
cout<<"Enter the current
requested charge : ";
cin>>charge;
if(charge>currBalance[loc])
cout<<endl<<"Requested
charge is not accepted since it exceeds account balance.";
else
{
cout<<endl<<"Requested charge is accepted.";
currBalance[loc]-=charge;
}
}
}
}