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

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

The Riddler is planning his next eper somewhere an Pennsylvania Avenue. In his usual sporting fashion, he has left the address in the form af a puzzle. The address on vana is a four-digit namber where All four digits are The digit in the thousands place is three times the digit in the tens ploce The number is odd The sun of the digits is 27. write eCH program the uses oleop or loops to find the address where the Riddler plans to strike. Nane the source file for Your progran as progran4. compile Your progromu g Well progrsn4 cpp -D program4 start writing the think aboat these questions How mory wonisbles vilyou seed to use? Do the variables need to be What data Type should each varioble have

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote