The attached account info file consists of a triple of information. A valid acco
ID: 3864544 • Letter: T
Question
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++ 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;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.