noam hezrony - 1 year ago 164
C++ Question

# i need help making this little c++ program work

Holy digits Batman! The Riddler is planning his next caper somewhere on Pennsylvania Avenue. In his usual sporting fashion, he has left the address in the form of a puzzle. The address on Pennsylvania is a four-digit number where:
• All four digits are different
• The digits in the thousandths place is three times the digit in the tens place
• The number is odd
• The sum of the digits is 27
Write a program that uses a loop (or loops) to find the address where the Riddler plans to strike.

I'm not so sure where the program is going wrong. any help will be appreciated.

``````#include <iostream>
using namespace std;

void splitAddress(int address, int thou, int hund, int tens, int ones) {
thou++;
}
hund++;
}
tens++;
}
ones++;
}
}
void areIntsTheSame(int address, int thou, int hund, int tens, int ones) {
if (thou == hund || thou == tens || thou == ones ||
hund == tens || hund == ones || tens != ones) {
}
}
void thou3tens(int address, int thou, int tens) {
if (thou != 3 * tens) {
}
}
if (address % 2 == 0) {
}
}
void Sum27(int address, int thou, int hund, int tens, int ones) {
if ((thou + hund + tens + ones) != 27) {
}
}
int main() {
int thou = 0;
int hund = 0;
int tens = 0;
int ones = 0;

system("pause");
return 0;
}
``````

I think, it's quite easy to generate an int and then check if it matches the rules. But the address can be found faster if you consider some logical consequences of the rules:

``````Digit 4: is odd
Digit 3: is either 1 or 2 or 3!
Digit 2: is even!
Digit 1: is Digit 3 multiplied by 3.
``````

Of course, if you now generate an int by these rules, it's really hard to check its digits, so you could consider using a container like `std::string` instead. But this will get quite ugly, because you always need to add (and subtract) `'0'`, in order to work with the digit as a digit. So I decided to go with `std::vector<int>` for your problem.

Before starting, I made a function that returns a random integer in a given range:

``````#include <random>

int random(int first, int last){
std::random_device now;
std::mt19937 engine(now());
std::uniform_int_distribution<int> r(first,last);
return r(engine);
}
``````

With this, I implemented the "generator"

``````std::vector<int> generate_adress(){
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

}
``````

With this, the only 2 things missing, are:

• The digits must be 27 added together.

• The digits need to be unique.

and that's done quite easily:

``````bool sum27(std::vector<int> address){
return sum == 27;
}

for (int i = 0; i < address.size(); ++i){
for (int j = 0; j < i; ++j){
}
}
return true;
}
``````

I wrapped these 2 up into one convenient function:

``````bool valid_address(std::vector<int> address){
}
``````

Now let's go to `main()` function and use the functions:

``````int main(){

int ctr = 0;

bool done = false;
while (!done){
++ctr;
std::cout << "Adress #" << ctr << ": ";

std::cout << " That's the address!" << std::endl;
done = true;
}
else{
std::cout << " not correct...\n";
}
}
return 0;
}
``````

Here is a run of the code:

``````Adress #72: 3617 not correct...
``````

only 93 tries is quite good. If you would generate the address completely random, it takes way more time (obviously):

``````Adress #10054: 2333 not correct...
``````

10000 tries, though. Anyway, here's the full code:

``````#include <string>
#include <iostream>
#include <random>
#include <vector>

int random(int first, int last){
std::random_device now;
std::mt19937 engine(now());
std::uniform_int_distribution<int> r(first,last);
return r(engine);
}

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

}

return sum == 27;
}

for (int i = 0; i < address.size(); ++i){
for (int j = 0; j < i; ++j){
}
}
return true;
}
}

int main(){

int ctr = 0;

bool done = false;
while (!done){
++ctr;
std::cout << "Adress #" << ctr << ": ";