Write a program that lets the user enter a charge account number. Be sure to include comments throughout your code where appropriate. The program should determine if the number is valid by checking for it in the following list of valid account number
5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 8080152,4562555, 5552012, 5050552, 7825877, 1250255, 1005231, 6545231,3852085, 7576651, 7881200, 4581002
The list of numbers above should be initialized in a single– dimensional array. A simple linear search should be used tolocate the number entered by the user. If the user enters a numberthat is in the array, the program should display a message sayingthat the number is valid. If the user enters a number that is notin the array, the program should display a message indicating thatthe number is invalid.

Respuesta :

Answer:

#include <iostream>

using namespace std;

int main(){

   int accountnum;

[tex]int\ accountnumbers[18] = \{5658845, 4520125, 7895122, 8777541,[/tex] [tex]8451277, 1302850, 8080152,4562555, 5552012, 5050552, 7825877,[/tex] [tex]1250255, 1005231, 6545231,3852085, 7576651, 7881200, 4581002\};[/tex]

   cout<<"Account Number: ";

   cin>>accountnum;

   int arrlength =*(&accountnumbers + 1) - accountnumbers;

   int exist = 0;

   for(int i =0;i<arrlength;i++){

       if(accountnum == accountnumbers[i]){

        cout<<"Valid account number";

        exist = 1;

        break;

       }

   }

   if(exist == 0)

       cout<<"Invalid account number";

     

   return 0;

}

Explanation:

The solution is implemented in C++ (See attachment)

This declares user account number as integer

  int accountnum;

This initializes the list of account numbers to an array

[tex]int\ accountnumbers[18] = \{5658845, 4520125, 7895122, 8777541,[/tex] [tex]8451277, 1302850, 8080152,4562555, 5552012, 5050552, 7825877,[/tex] [tex]1250255, 1005231, 6545231,3852085, 7576651, 7881200, 4581002\};[/tex]

This prompt user for account number

   cout<<"Account Number: ";

Here, user inputs the account number

   cin>>accountnum;

This calculates the length of the array

   int arrlength =*(&accountnumbers + 1) - accountnumbers;

This initializes a check variable to 0

   int exist = 0;

This iterates through the array

   for(int i =0;i<arrlength;i++){

This checks for valid account number.

       if(accountnum == accountnumbers[i]){

If true, the following message is printed

        cout<<"Valid account number";

The check variable is updated to 1, which implies that the account number is valid

        exist = 1;

And the loop is terminated

        break;

       }

   }

If the check variable is not updated, then the account number is invalid

   if(exist == 0)

       cout<<"Invalid account number";

     

   return 0;

Ver imagen MrRoyal
ACCESS MORE