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

IN C++ Charge Account Validation: Write a program that lets the user enter a cha

ID: 3831270 • Letter: I

Question

IN C++

Charge Account Validation: Write a program that lets the user enter a charge account number. The program should determine if the number is valid by checking for it in the following list:

5658845 4520125 7895122 8777541 8451277 1302850

8080152 4562555 5552012 5050552 7825877 1250255

1005231 6545231 3852085 7576651 7881200 4581002

Initialize a one-dimensional array with these values. Let user enter a number. Then write a function with header bool linearSearch(int A[ ], int size, int num) to location whether a number is in the list . In your main function, if the user enters a number that is in the array, the program should display a message saying the number is valid. If the user enters a number not in the array, the program should display a message indicating it is invalid.

Explanation / Answer

#include <iostream>

using namespace std;
bool linearSearch(int A[ ], int size, int num) {
for(int i=0;i<size; i++){
if(A[i]==num){
return true;
}
}
return false;
}
int main()
{
int A[] = {5658845, 4520125, 7895122, 8777541 ,8451277, 1302850,
8080152 ,4562555, 5552012, 5050552 ,7825877, 1250255,
1005231 ,6545231 ,3852085 ,7576651, 7881200 ,4581002};
int size = 18;
int num;
cout<<"Enter a charge account : ";
cin >> num;
if(linearSearch(A, size, num)){
cout<<"the number is valid"<<endl;
}
else {
cout<<"the number is invalid"<<endl;
}

return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter a charge account : 3852085                                                                                                                                                                                                                                       

the number is valid                                                                                                                                                                                                                                                    

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter a charge account : 33334444                                                                                                                                                                                                                                      

the number is invalid