C++ can you please answer using e-text(not hand written) thanks The Riddler is p
ID: 3824041 • Letter: C
Question
C++
can you please answer using e-text(not hand written)
thanks
Explanation / Answer
#include <string>
#include <iostream>
#include <random>
#include <vector>
int random(int first, int last){ //method to generate random numbers
std::random_device now;
std::mt19937 engine(now());
std::uniform_int_distribution<int> r(first,last);
return r(engine);
}
std::vector<int> address_generator(){
std::vector<int> address(4);
address[3] = (random(0, 4) * 2) + 1; //last digit: odd number
address[2] = random(1, 3); //second digit: 1 or 2 or 3
address[1] = random(0, 4) * 2; //third digit: even number
address[0] = address[2] * 3; //last digit: second digit * 3
return address;
}
bool checkSum27(std::vector<int> address){ // method to check if sum is 27
int sum = address[0] + address[1] + address[2] + address[3];
return sum == 27;
}
bool unique_digits(std::vector<int> address){
for (int i = 0; i < address.size(); ++i){
for (int j = 0; j < i; ++j){
if (address[j] == address[i]) return false;
}
}
return true;
}
bool valid_address(std::vector<int> address){
return checkSum27(address) && unique_digits(address);
}
int main(){
int count = 0;
bool done = false;
while (!done){
++count;
std::vector<int> address = address_generator(); //generate te address
std::cout << "Address #" << count << ": ";
for (int i : address) std::cout << i; //print out address
if (valid_address(address)){ //validate the address
std::cout << " is the correct address!!! " << std::endl;
done = true;
}
else{
std::cout << " is not valid ";
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.