New course! Every coder should learn Generative AI!
Try a free lesson+ 18
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
int main() {
srand(static_cast<size_t>(time(0)));
int n = 11 + rand() % (42 - 11); // range generator [11, 41]
n = n % 2 == 0 ? n - 1 : n; // set odd distances
for (int i = 3; i <= n; ++i) {
// determines the distance between two arrow guys
int dist = n - i * 2;
// checking contact condition
if (dist > 1)
std::cout << std::setw(i) << "->" << std::setw(dist) << "<-\n";
else {
std::cout << std::setw(i + dist) << "BOOOM!\n"; break;
}
}
}
Possible output:
-> <-
-> <-
-> <-
-> <-
-> <-
-> <-
-> <-
-> <-
-> <-
-> <-
-> <-
-><-
BOOOM!
Live Link : [http://cpp.sh/2o23]