Question

In: Computer Science

Draw a simplified star-stripe flag using star (*) and equal (=) characters, such as: ****====== ****======...

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:

  • The width of the flag is between 5 and 70 (inclusive);
  • The height of the flag is between 5 and the width of the flag (inclusive);
  • The star area is placed at the upper left corner of the flag;
  • The star area is a square and its width must be smaller than the height of the flag;
  • There must be at least one star.
  • The program should use do-while, and nested-loop statements

Your task:

Design and implement a relatively simple and easy C++ program that performs the following steps:

  • keep asking user to enter the width of a flag until a valid width is entered;
  • keep asking user to enter the height of a flag until a valid height is entered;
  • keep asking user to enter the width of the star area until a valid number is entered;
  • draw (display) the flag based on the user input.

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.

Solutions

Expert Solution

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.


Related Solutions

Draw a simplified star-stripe flag using star (*) and equal (=) characters, such as: ****====== ****======...
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: The width of the flag is between 5 and 70 (inclusive); The height of the flag is between 5 and the width of the flag (inclusive); The star area is placed at the upper left corner of the flag; The star area is a square...
Draw an American Flag in Python turtle Height A=1 Width B=1.9 Blue C=0.5385 D=0.76 Star Pattern...
Draw an American Flag in Python turtle Height A=1 Width B=1.9 Blue C=0.5385 D=0.76 Star Pattern E=F=0.0538 G=H=0.0633 Strips K=0.0633 L=0.0769
Why was Eio eager to promote the subsidiary initiative of licensing Star Wars characters for LEGO...
Why was Eio eager to promote the subsidiary initiative of licensing Star Wars characters for LEGO toys? Why was there so much resistance from the headquarters initially? If you were a subsidiary manager at a multinational, what are the lessons you can draw from this case that can help you successfully convince the headquarters that your ideas deserve to be supported?
Question 2. a) What are the differences between element, crystal and mineral? Draw the simplified crystal...
Question 2. a) What are the differences between element, crystal and mineral? Draw the simplified crystal system shapes. b) Write the physical properties of minerals and mohs hardness scale.
Q. Given a document with 32 distinct characters, with equal number of occurrences of each, what...
Q. Given a document with 32 distinct characters, with equal number of occurrences of each, what would be the ratio of the length of the Huffman coded document to the original, if each character in the input was coded in 8 bits? Show your analysis.
What is the gravitational lensing signature of a planet orbiting a star? Draw a picture of...
What is the gravitational lensing signature of a planet orbiting a star? Draw a picture of the spatial relationship between the star being lensed, the planet, and the star around which the planet is orbiting. Also, draw a typical light curve showing how the planet manifests itself.
Which, if any, of the following is an advantage of using the simplified method for determining...
Which, if any, of the following is an advantage of using the simplified method for determining the office use of a home deduction compared to using the regular method? a. No depreciation on computer equipment used in the office has to be computed using the simplified method. b. The exclusive use requirement does not have to be met if the simplified method is used. c. The simplified method allows the taxpayer to avoid issues that arise in allocating expenses of...
You are to construct a star schema for Simplified Automobile Insurance Company. The relevant dimensions, dimension attributes, and dimension sizes are as follows:
Modern Database Management, 13th EditionJeff Hoffer9-39. You are to construct a star schema for Simplified Automobile Insurance Company. The relevant dimensions, dimension attributes, and dimension sizes are as follows:InsuredPartyAttributes: InsuredPartyID and Name. There is an average of two insured parties for each policy and covered item.CoverageItemAttributes: CoverageKey and Description. There is an average of 10 covered items per policy.AgentAttributes: AgentID and AgentName. There is one agent for each policy and covered item.PolicyAttributes: PolicyID and Type. The company has approximately 1...
1-a)List the defining characteristics of the Chondrichthyes. b) Draw a simplified phylogenetic tree for cartilaginous fishes...
1-a)List the defining characteristics of the Chondrichthyes. b) Draw a simplified phylogenetic tree for cartilaginous fishes and outline the characteristics of the three main types. 2- Describe the main evolutionary steps involved in the conquest of landmasses by vertebrate organisms during the Devonian period.
In our class example, I simplified the “annuity” prize option by assuming level, equal annual payments....
In our class example, I simplified the “annuity” prize option by assuming level, equal annual payments. Actually, this annuity prize option us now on an annuitized prize payment schedule with 26 beginning of year payments that start at a lower amount with each successive payment being 5% higher than the previous annual payment. The sum of these 26 annuitized payments equal the announced estimated jackpot amount with a lower one-time lump-sum payment also being available as the Cash Option. A...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT