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 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.
Answer.
Step 1
Program Code:
#include<iostream>
#include<math.h>
using namespace std;
class Flag
{
int flagWidth, flagHeight;
int starArea;
public:
void read() // function to read flag height, width and star
area
{
bool valid = false;
do {
// prompt user to enter an int between 5 and 70
cout<<"Enter the flag width (between 5-70): ";
cin>>flagWidth;
// if the number entered is valid, set done to exit the loop
if (flagWidth >=5 && flagWidth <= 70) {
valid = true;
}
} while (!valid);
valid = false;
do {
// prompt user to enter an int between 5 and flagWidth
cout<<"Enter the flag height (between 5-flagwidth): ";
cin>>flagHeight;
// if the number entered is valid, set done to exit the loop
if (flagHeight >=5 && flagHeight <= flagWidth)
{
valid = true;
}
} while (!valid);
valid = false;
do {
// prompt user to enter star area
cout<<"Enter the star area (must be square and smaller than
flagHeight): ";
cin>>starArea;
// if the number entered is valid, set done to exit the loop
if (starArea < flagHeight) {
valid = true;
}
} while (!valid);
}
void display() // display the output
{
for(int i=1; i<=flagHeight;i++)
{
for(int j=1; j<=flagWidth;j++)
{
if(i<=starArea&&j<=starArea)
cout<<"*";
else
cout<<"=";
}
cout<<endl;
}
}
};
int main()
{
Flag d;
d.read();
d.display();
}
Step 2
Screenshot of code:
Output:
Thank you.