Write a C++ program that processes charge account numbers. Input will come from
ID: 3622431 • Letter: W
Question
Write a C++ program that processes charge account numbers. Input will come from a file and some of the output will go to a file. Both file names should be entered from the keyboard at run time in the main program.
1. Refer to Programming Problems #1 and # 4 on page 503. Use the list of numbers from #1 for the input file for the test case. Insert a -1 at the end of this list as the sentinel. These numbers should be read by your program and stored in a single-dimensional array of type long. Assume there will be no more than 20 numbers, but your code should check that the user doesn't attempt to enter more. If an attempt is made to enter 21, print an error message on the screen and process only the first 20.
2. Print an appropriate program purpose and labels on both the screen output and the file output. Print the unsorted numbers on the output file, four numbers per line.
3. Use the selection sort algorithm given in your text to sort your array. Print the the sorted array on the output file, four numbers per line.
4. The user should enter a number from the keyboard. Your program should print a message on the screen saying whether or not the number is in the list. The binary search should be used to determine if the number is valid.
5. Your design should include at least four functions in addition to the main function. The selection sort code and the code to perform a binary search should be two of the functions.
6. Your name as programmer should appear on the output file and on the screen.
This is the Input File need to be in txt format
Explanation / Answer
Dear,
Here is the code
//Header file section
#include < iostrem >
using namespace std;
// Function prototype
void sortArray(int array[], int size);
int binarySearch(int [], int, int,int);
int main()
{
Ifstream inFile;
const int arrSize = 18;
int tests[arrSize] ,i=0;
int results;
int element;
String fname;
cout<<”Enter file name:”;
cin>>fname;
inFile.open(fname);
cout<<"Enter Account number to be searched:";
cin>>element;
into num;
infile>>num;
do
{
tests[i]=num;
infile>>num;
i++;
}
while(num!=-1);
//Function to sort list of Account numbers
sortArray(tests,arrSize);
//Function to search
results = binarySearch(tests,0, arrSize,element);
if (results == -1)
cout <<"You entered a Invalid Account Number. ";
else
{
cout << "You entered a valid Account number ";
cout << (results + 1) << ". ";
}
//To pause system for a while
system("pause");
return 0;
} //End main
//Function definition
//It sorts list of Account numbers
void sortArray(int array[], int size)
{
//Local variables
bool swap;
int temp;
//Loop repeats untill all elements sorted
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}//end if
}//End for
} while (swap);
}//End sortArray
//function to search Account number
int binarySearch(int array[], int first, int last, int value)
{
int middle; // Mid point of search
if (first > last)
return -1;
middle = (first + last)/2;
if (array[middle]==value)
return middle;
if (array[middle]return binarySearch(array, middle+1,last,value);
else
return binarySearch(array, first,middle-1,value);
}
//End binarySearch
Hope this will help you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.