Question

You are at the Central Intelligence Agency (CIA). You are assigned the job of developing a program that will analyze possible passwords that are used to encrypt password-protected data. The cIA uses a very specific set of rules for passwords to identify non-CIA operatives who attempt to access password-protected data (these non- CIA operatives will try passwords that do not conform to CIA rules). The rules are complicated enough that the CIA has assigned you the job of developing a program that generates all valid passwords. cIA personnel will then use the program to make sure the passwords they select to protect data conform to the rules. The rules that your program must use to determine if the password is valid are as follows: 1. The password must be an integer greater than or equal to 100,000 and less than 1,000,000 (6 digits). 2. All the digits in the number have to be unique. 3. The sum of the digits must be a prime number. 4. The permutation of the left-most digit (n) and the smallest digit (k) must be odd-for example: in 645923, the left-most is 6 and the smallest digit is 2. Note: the permutation of n things taken k at a time is computed via: (n -k)! Where the denotes a factorial. For example, for the number 645923, the permutation with n 6 and k 2 is: 65*4*3 2 1/(4 3 2 1) 30. You will list all valid passwords. In addition, you will need to list the total number of valid passwords at the end.
Using C++
0 0
Add a comment Improve this question Transcribed image text
Answer #1

This program makes use of just one function apart from main(), called fact() which is a recursive function to calculate the factorial of a number (used in calculating the permutation) All the checks are done within main() itself. If you wish you can separate the different checks into different functions.

The list of valid passwords is not stored anywhere, it is just displayed as and when a valid password is found. If you wish, you may use a vector or a list, etc to store this list of passwords.

Below is code:-

#include <iostream>
#include <cmath>

using namespace std;

//recursive function to calculate and return a factorial
int fact(int num)
{
if(num>1)
return num*fact(num-1);
else
return 1;
}

int main()
{
int validPwdCount=0; //number of valid passwords
for(int i=100000;i<1000000;i++) //this is the range of valid passwords
{
int pwd=i;
bool isValid=true; //make this false if password is invalid
//first check if all digits are unigue
int digits[6]; //save all digits in this array, the password is a 6 digit integer
int index=5;//start filling array from the last position as we'll get the last digit first
while(pwd)
{
digits[index]=pwd%10;//separate each digit and fill in array
pwd/=10;
index--;
}
for(int j=0;j<6;j++)
{
for(int k=0;k<6;k++)
{
if(j!=k) //indices are not equal (not the same number)
if(digits[j]==digits[k])
{
isValid=false;
break;
}
}
if(!isValid) break; //no need to continue looping for duplicate digits if password is invalid
}
if(!isValid) continue; //no need to go ahead for other checks if password is invalid, so continue with outer loop
//and check the next password

//now check if the sum of digits is a prime number
//get sum of digits
long sum=0;
for(int l=0;l<6;l++)
sum+=digits[l];
//check if the sum is a prime number by dividing it by numbers from 2 to square root of the sum
for(int l=2;l<sqrt(sum);l++)
if(sum%l==0) //number is not prime
{
isValid=false;
break; //no need to continue looping for divisors if password is invalid
}
if(!isValid) continue; //no need to go ahead for other checks if password is invalid, so continue with outer loop
//and check the next password

//check if permutation of the left-,ost digit with the smallest digit is odd
int n=digits[0]; //leftmost digit
int k=1000000; //k will hold the smallest digit
for(int l=0;l<6;l++)
if(digits[l]<k)
k=digits[l];
int perm=fact(n)/fact(n-k); //find the required permutation
//check if the permutation is an odd number
if(perm%2==0)
{//the permutation is not an odd number
isValid=false;
continue; //no need to go ahead for other checks if password is invalid, so continue with outer loop
//and check the next password
}
if(isValid)//if password is valid
{
cout<<i<<endl; //print the password
validPwdCount++; //increment the password count
}
}
cout << "Total Number of valid passwords= "<<validPwdCount<< endl;
return 0;
}

Output (last part only):-

Hope this helps!

Add a comment
Know the answer?
Add Answer to:
Using C++ You are an analyst at the Central Intelligence Agency (CIA). You are assigned the...
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
  • Keeping this code written as close to this as possible and only using these headers. How...

    Keeping this code written as close to this as possible and only using these headers. How do I turn this in to a code that can read from 3 files of passwords then sort them into two files. One for valid passwords and the other for invalid passwords and then send a message to the user that says how many are valid and how many are invalid. Only using std::ifstream fin; fin.open("data.txt"); fin.close(); std::ofstream fout; fout.open("data.txt"); fout.close(); #include <iostream> #include...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

  • C++ Loops homework Prompt the user for a desired password, input the password. Your program may...

    C++ Loops homework Prompt the user for a desired password, input the password. Your program may assume without checking that there is no input failure and that the password contains no white space. Let's say the rules for a legal password are: # chars must be in [4, 8] # digs must be >= 2 The password must contain at least one letter of each case The password must contain at least one char that's not a letter or digit...

  • Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...

    Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid To check the validity of the...

  • Using c++ A ten diglt ISBN number uses a checksum as its last diglt to verlfy...

    Using c++ A ten diglt ISBN number uses a checksum as its last diglt to verlfy the first nine digits are valid. Before 2007, all ISBN numbers were composed like this, such as: e-20-5e8005-7 or 1-234-56789-X The first nine digits are assigned by a book's publisher and the last digit is calculated by "weighted sum (described below). The X stands for the checksum value of 10, in order to represent ten as a single digit. You must write a program...

  • Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

    Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...

  • C++ language only. Thank you C++ language (cout <). NO atoi function. And please pay attention...

    C++ language only. Thank you C++ language (cout <). NO atoi function. And please pay attention to the rules at the bottom. Extra good rating to whoever can help me with this. TIA. In certain programming situations, such as getting information from web forms, via text boxes, the data coming in may need to be numerical (we want to perform arithmetic on it), but it is stored in the form of a list of characters (the numerical value stored is...

  • A CERTAIN program to user's password containing rules such as least n length, one uppercase, lowercase,...

    A CERTAIN program to user's password containing rules such as least n length, one uppercase, lowercase, one digit and white space is given as the code down below. So, simiiar to those rules, I need the code for the following questions post in pictures such as no more three consecutive letters of English alphabets, password should not contain User name and so on. //GOAL: To learn how to create that make strong passwords //Purpose: 1)To learn some rules that makes...

  • Please do it by C++ programming! By completing this project, you will demonstrate your understanding of:...

    Please do it by C++ programming! By completing this project, you will demonstrate your understanding of: 1) Arithmetic operations (addition, subtraction, multiplication, division, modulus) 2) Conditional statements (If, If-Else, If-ElseIf-Else, Conditional Operator, Switch) 3) Precondition, postcondition, and indexing loops (Do-While, While, For) 4) Standard text file operations (open, close, read, write) 5) Modularizing code by breaking into functions (including functions with input parameters, and ones that return a specific data type) There is a number guessing game similar to MasterMind...

  • I need it in c++ The U.S. Banking System The code assigned to a bank is...

    I need it in c++ The U.S. Banking System The code assigned to a bank is an eight digit number plus a ninth check digit. To check for validity, the first eight digits are assigned weights (left to right) of 7, 3, and 9 repetitively. Each digit is multiplied by its weight and the products summed. The resulting sum (mod 10) should equal the check digit. The assigning of weights insures that all single digit errors and most transposition errors...

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