54.5k views
2 votes
Your smartphone or tablet may require you to enter a short personal identification number, or PIN. This may be any number in a range of lengths. For our purposes, we will consider PINs to be of length 4, 5, or 6. We use digits 0–9 inclusive. Since PINs can be of different lengths, you need to consider 1111, 01111, and 001111 as separate examples. We provide a testing function check_pin( pin ) which accepts a four-digit guess for pin. check_pin returns True if the PIN number matches the internal PIN. The pin should be provided as a string, that is, '1234', rather than [ '1','2','3','4' ] or [ 1,2,3,4 ]. Compose a function crack_pin() which scans all possible PIN numbers in the valid ranges and returns the correct one. Your submission should include a function crack_pin(). check_pin is already defined for your use.

User MarkHu
by
5.2k points

1 Answer

1 vote

Answer:

#include <iostream>

using namespace std;

void crack_pin(string a, int low, int res)

{

if (low == res)

cout<<a<<endl;

else

{

for (int i = low; i <= res; i++)

{

swap(a[low], a[i]);

crack_pin(a, low+1, res);

swap(a[low], a[i]);

}

}

}

int main()

{

string str = "ABC";

cout <<"Enter pin guess"<<endl;

getline(cin,str);

int n = str.size();

crack_pin(str, 0, n-1);

return 0;

}

Step-by-step explanation:

Take pin guess from user using getline method in str variable of type string.

Find size of pin guess.

Declare a function crack_pin and send string, starting (0) and ending (n-1) to function.Call the function recursively to find all combinations.If start=end print string else call function again.

User BertLi
by
5.2k points