Question

This program is in C++ Write a program that validates charge account numbers read in from...

This program is in C++

Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is not found, tell the user to enter a valid number. Place this in a loop so multiple values can be checked without restarting the program. Ask the user when to quit.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<bits/stdc++.h>

using namespace std;

void swap(long long int *xp, long long int *yp)  

{  

    int temp = *xp;  

    *xp = *yp;  

    *yp = temp;  

}  

/* Function to sort an acc numbers */

void selectionSort(long long int  arr[], long long int  n)  

{  

    long long int i, j, min_idx;  

  

    for (i = 0; i < n-1; i++)  

    {  

        

        min_idx = i;  

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

        if (arr[j] < arr[min_idx])  

            min_idx = j;   

        swap(&arr[min_idx], &arr[i]);  

    }  

}  

  

/* Function to print an array */

void printArray(long long int arr[], long long int size)  

{  

    long long int i;  

    for (i=0; i < size; i++)  

        cout << arr[i] << " ";  

    cout << endl;  

}  

/* Function to search an acc number */

long long int binarySearch(long long int arr[], long long int l, long long int r, long long int x)

{

    if (r >= l) {

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

  

        if (arr[mid] == x)

            return mid;

  

        if (arr[mid] > x)

            return binarySearch(arr, l, mid - 1, x);

  

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

    }

  

    return -1;

}

int main(int argc, char const *argv[])

{

   long long int arr[130];

   ifstream myfile("data.txt");

    long long int  n=0;

   int x;

   while (n < arr[30] && myfile >> x)

   // and read integer from file

   arr[n++] = x;            

    long long int a;

    long long int i=0;

    long long int number;

    int result;

    int choice;

    

    while(1){

          cout<<"Enter the choice\n1.Check Acc Number.\n2.Quit.\n";

          cin>>choice;

             switch(choice){

                 case 1:cout<<"Enter an account number\n";

                        cin>>number;

                        while (myfile >> a)

                         {

                          arr[i++]=a;

                          }

                        

                         selectionSort(arr, n);  

                         cout << "sorted list of account numbers: \n";  

                         printArray(arr, n);  

                         result = binarySearch(arr, 0, n - 1, number);

                         (result == -1) ? cout << "account number entered is not found\n"

                           : cout << "entered account number is found\n";

                          break;

                 case 2: exit(0);

                 default:cout<<"invalid choice\n";

       }         

      }

    getchar();

  return 0;

}

Enter the choice 1. Check Acc Number. 2.Quit. Enter an account number 8965494 sorted list of account numbers: 1235553 4456667

Add a comment
Know the answer?
Add Answer to:
This program is in C++ Write a program that validates charge account numbers read in from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

  • Using c++ 1 of 2 Assignment 5 Lab Section 3 write a program create a vector...

    Using c++ 1 of 2 Assignment 5 Lab Section 3 write a program create a vector with random numbers. Use merge sort to reorder the vector Prompt user to enter a number to search in the vector. If there are more than one number in the vector equal to the search value, display the indices remove the repeated numbers. If found just one matching number, display the index. If no matching number, prompt user for 2 options: add to the...

  • In python 3, 1. Write a program that takes a number of credit hours and returns...

    In python 3, 1. Write a program that takes a number of credit hours and returns a classification as follows: 29 credits or less: freshman; 30-59 credits: sophomore; 60-89 credits: junior; 90+ credits: senior. 2. Use a loop to valid input such that users can only enter positive numbers. 3. Use a loop to allow the user to keep entering credit hours and receiving classifications until they quit. All code must be in a function. I suggest the following structure:...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT