इस ट्यूटोरियल में, हम एक हट पैटर्न को प्रिंट करने के कार्यक्रम पर चर्चा करेंगे।
इसके लिए हमें छपी जाने वाली झोंपड़ी की चौड़ाई (मान लीजिए N) प्रदान की जाएगी। हमारा काम तारों का उपयोग करके दी गई चौड़ाई की एक झोपड़ी संरचना और लाइन वर्णों का उपयोग करके झोपड़ी के अंदर एक गेट को प्रिंट करना है।
उदाहरण
#include <iostream>
using namespace std;
//printing the given hut structure
int print_hut(int n){
int i, j, t;
if (n % 2 == 0) {
n++;
}
for (i = 0; i <= n - n / 3; i++) {
for (j = 0; j < n; j++) {
t = 2 * n / 5;
if (t % 2 != 0) {
t--;
}
//calculating the distance from the initial
//character
//and printing the outer boundary of the hut
if (i == n / 5
|| i == n - n / 3
|| (j == n - 1 && i >= n / 5)
|| (j >= n / 5 && j < n - n / 5 && i == 0)
|| (j == 0 && i >= n / 5)
|| (j == t && i > n / 5)
|| (i <= n / 5 && (i + j == n / 5 || j - i == n / 5))
|| (j - i == n - n / 5)) {
cout << "*";
}
//printing the structure of the door
else if (i == n / 5 + n / 7 && (j >= n / 7 && j <= t - n / 7)) {
cout << "_";
}
else if (i >= n / 5 + n / 7 && (j == n / 7 || j == t - n / 7)) {
cout << "|";
}
else {
cout << " ";
}
}
cout << "\n";
}
}
int main(){
int n = 12;
print_hut(n);
return 0;
} आउटपुट
********** * * * ************* *___* * *| |* * *| |* * *| |* * *| |* * *| |* * *************