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

code: //************************************************************************

ID: 3876088 • Letter: C

Question

code:  

//**************************************************************************

// Program: Charge Account Numbers

//

// Purpose: This program checks whether a number entered by the user is a

// valid charge account number.

//

// Input: The user is asked to enter a three-digit number to be checked.

//

// Processing: The main function uses an initialization list to initialize

// an array with charge account numbers. Then it calls a function

// to display the list. The user is asked to enter a charge

// number. The main function calls another function to check the

// validity of this number and then it displays a message

// reporting the results.

//

// Output: The program displays the list of account numbers and a message

// indicating whether the number entered by the user is valid

// or not.

//

// Author: Your name

// Class: CS 2020

// Semester: Spring 2018

//**************************************************************************

#include <iostream>

#include <iomanip>

using namespace std;

// Function prototypes

void displayAcctNos(int[], int);

// Parameters: array, value2find, num_of_elements

int searchList(int[], int, int);

int main()

{

const int ARY_SIZE = 9;

// Array of account numbers

int accounts[ARY_SIZE] =

{ 565, 452, 789,

877, 845, 130,

808, 456, 555 };

  

int accountNumber;   

int position;

//********************************************************

// Display account numbers

//********************************************************

displayAcctNos(accounts, ARY_SIZE);

//********************************************************

// Get an account number from the user.

//********************************************************

cout << endl << "Please enter a 3-digit account number: ";

cin >> accountNumber;

//***********************************************************

// Locate given account number in the array accounts.

// Display appropriate message to the user about the account.

// Allow multiple account lookups until -1 if entered by the user.

//***********************************************************

cout << endl;

return 0;

}

//*********************************************************************

// Function: displayAcctNos

// Description: Displays the list of charge numbers.

// Parameters: array of account numbers

// howmany - number of elements to process

// Returns: void

//*********************************************************************

void displayAcctNos(int acctNos[], int howmany)

{

cout << endl << "Charge Account Numbers" << endl;

cout << "Element Charge Number " << endl;

for (int k = 0; k < howmany; k++)

cout << setw(4) << k << setw(14) << acctNos[k] << endl;

}

//*********************************************************************

// Function: searchList

// Description: This function searches the array of charge numbers for the

// number passed into value using the linear search algorithm.

// If the number is found the function returns the position of

// the value in the array.

// Parameters: array of account numbers

// value - locate this account number in the array

// howmany - number of elements to process

// Return: If the number is not found it returns -1.

//*********************************************************************

int searchList(int accounts[], int value, int howmany)

{

return -1;

}

Mandatory Instructions Part A. Edit, debug and compile a C++ program. 1. 2. 3. 4. 5. 6. Log into your BGUnix account using PuTTY Switch to cs2020 directory Copy labl.cpp from the lib directory into your cs2020 directory. That's your starting point. Open the labl.cpp file in the pico editor by typing the command: pico labl.cpp. Add a linear search function to find given account number. Prototype and a stub for this function is given in the seed file. In the main function, add a sentinel-control While the account number entered is not equal to the sentinel value (1), report whether the account number is valid or not and let the user enter another number. When -1 is entered, no message about the account number should appear. Compile, debug, and run your program until you are sure that it is working correctly Test your program to allow the user to check as many account numbers as he/she wishes 7. 8. with values that are found in the arrays and values that are not found in the arrays. What to túrn in? Once your program displays the output correctly, create a new photo log file called labI.log by typing in these commands at the $ S photo labl.log $Is Starts the photo utility and captures all screen output in the file labl.log Use a lower-case L, not the number one to see a list of the files in your account in the "long" format (showing size, date modified, etc.) Displays your program on the screen Compiles your program Enter these account numbers: 845,222,555 followed by -1 without re-running the Program each time Press the Ctrl and d keys S cat labl.cpp S gtt labl.cpp $ a.out S [Ctrl]-d at the same time to end the photo session You are done! Now complete the grade sheet and upload to Canvas

Explanation / Answer

code written is explained through comments, please refer to following code, if you are still unable to understand anything in this code please comment on this answer

Linear Search: Given value is checked with every value in an array, if both given value and value in array are same then we found it else it is not present in array

//*************************code*****************************************

//**************************************************************************

// Program:     Charge Account Numbers

//

// Purpose: This program checks whether a number entered by the user is a

//    valid charge account number.

//

// Input: The user is asked to enter a three-digit number to be checked.

//

// Processing: The main function uses an initialization list to initialize

//    an array with charge account numbers. Then it calls a function

// to display the list. The user is asked to enter a charge

// number. The main function calls another function to check the

// validity of this number and then it displays a message

// reporting the results.

//

// Output: The program displays the list of account numbers and a message

//    indicating whether the number entered by the user is valid

// or not.

//

// Author: Your name

// Class: CS 2020

// Semester: Spring 2018

//**************************************************************************

#include <iostream>

#include <iomanip>

using namespace std;

// Function prototypes

void displayAcctNos(int[], int);

// Parameters: array, value2find, num_of_elements

int searchList(int[], int, int);

int main()

{

    const int ARY_SIZE = 9;

    // Array of account numbers

    int accounts[ARY_SIZE] =

      { 565, 452, 789,

        877, 845, 130,

        808, 456, 555 };

      

    int accountNumber;      

    int position;

    //********************************************************

    // Display account numbers

    //********************************************************

    displayAcctNos(accounts, ARY_SIZE);

    //********************************************************

    // Get an account number from the user.

    //********************************************************
    //do while loop for multiple lookups, until position returned is not -1
    do{
    cout << endl << "Please enter a 3-digit account number: ";

    cin >> accountNumber;

    //***********************************************************

    // Locate given account number in the array accounts.

    // Display appropriate message to the user about the account.

    // Allow multiple account lookups until -1 if entered by the user.

    //***********************************************************
    position=searchList(accounts, accountNumber, ARY_SIZE);
    if(position==-1)
    cout<<"accountNumber not found - Invalid Number"<<endl;
    else
    cout<<"Valid Number - accountNumber found at postion: "<<position<<endl;
    }while(position!=-1);       // loop runs until position returned is not equal to -1

    cout << endl;

    return 0;

}

//*********************************************************************

// Function: displayAcctNos

// Description: Displays the list of charge numbers.

// Parameters: array of account numbers

//              howmany - number of elements to process

// Returns: void

//*********************************************************************

void displayAcctNos(int acctNos[], int howmany)

{

    cout << endl << "Charge Account Numbers" << endl;

    cout << "Element Charge Number " << endl;

    for (int k = 0; k < howmany; k++)

        cout << setw(4) << k << setw(14) << acctNos[k] << endl;

}

//*********************************************************************

// Function: searchList                           

// Description: This function searches the array of charge numbers for the

// number passed into value using the linear search algorithm.

// If the number is found the function returns the position of

// the value in the array.

// Parameters: array of account numbers

//              value - locate this account number in the array

//              howmany - number of elements to process

// Return: If the number is not found it returns -1.

//*********************************************************************

int searchList(int accounts[], int value, int howmany)

{
    for(int i=0;i<howmany;i++)
    {
        if(accounts[i]==value)    //checks if value matches with number in accounts array
        {
            return i;                //if matched return position i in array
        }
    }
  
    return -1;         //if not matched with any number return -1

}