In: Computer Science
Draw a simplified star-stripe flag using star (*) and equal (=) characters, such as:
****====== ****====== ****====== ****====== ========== ==========
The user can choose how the flag should look like. But there are following restrictions:
Your task:
Design and implement a relatively simple and easy C++ program that performs the following steps:
One of the purpose of this assignment is to practice the modular design. Your program should have at least one function that can be used by the first 3 steps of above tasks.
The C++ program for the above problem is:
#include <iostream>
using namespace std;
int input(int a, int b) {
int l;
while(1) { // loop continues unless valid input is obtained or given by the user
cin>>l;
if(l >= a && l <= b)
return l;
else
cout <<"Enter value between "<<a<<" and "<<b<<" (inclusive): ";
}
}
int main() {
cout <<"Enter width of the flag: ";
int width_flag = input(5,70); // width inclusive of length 5 and 70
printf("Enter height of the flag: ");
int height_flag = input(5,width_flag); // height inclusive of length 5 and width
printf("Enter width of the flag: ");
int width_star = input(1,height_flag - 1); // width inclusive of length 1 and less than height
int i = 0, j = 0;
do { // outer loop takes care of the height
j = 0;
if(i < width_star) {
do { // inner loop takes care of the width
if(j < width_star)
cout <<"*";
else
cout <<"=";
j++;
}
while(j < width_flag);
}
else {
do { // inner loop takes care of width
cout <<"=";
j++;
}
while(j < width_flag);
}
i++;
cout << endl;
}
while(i < height_flag);
return 0;
}
The sample of the program is:
The output sample of the program is:
Comment down if you have any queries regarding the above code. I will help you out as soon as possible.