413 Program Drawing a half arrow C This program outputs a do
4.13 Program: Drawing a half arrow (C++) This program outputs a downwards facing arrow composed of a rectangle and a right triangle. The arrow dimensions are defined by user specified arrow base height, arrow base width, and arrow head width. (1) Modify the given program to use a loop to output an arrow base of height arrowBaseHeight. (1 pt)
(2) Modify the given program to use a loop to output an arrow base of width arrowBaseWidth. Use a nested loop in which the inner loop draws the *’s, and the outer loop iterates a number of times equal to the height of the arrow base. (1 pt)
(3) Modify the given program to use a loop to output an arrow head of width arrowHeadWidth. Use a nested loop in which the inner loop draws the *’s, and the outer loop iterates a number of times equal to the height of the arrow head. (2 pts)
(4) Modify the given program to only accept an arrow head width that is larger than the arrow base width. Use a loop to continue prompting the user for an arrow head width until the value is larger than the arrow base width. (1 pt)
while (arrowHeadWidth <= arrowBaseWidth) { cout << \"Enter arrow head width: \" << endl; cin >> arrowHeadWidth; }
Example output for arrowBaseHeight = 5, arrowBaseWidth = 2, and arrowHeadWidth = 4:
#include <iostream>
using namespace std;
int main() {
int arrowBaseHeight = 0;
int arrowBaseWidth = 0;
int arrowHeadWidth = 0;
cout << \"Enter arrow base height: \" << endl;
cin >> arrowBaseHeight;
cout << \"Enter arrow base width: \" << endl;
cin >> arrowBaseWidth;
cout << \"Enter arrow head width: \" << endl;
cin >> arrowHeadWidth;
// Draw arrow base (height = 3, width = 2)
cout << \"**\" << endl;
cout << \"**\" << endl;
cout << \"**\" << endl;
// Draw arrow head (width = 4)
cout << \"****\" << endl;
cout << \"***\" << endl;
cout << \"**\" << endl;
cout << \"*\" << endl;
return 0;
}
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
#include <string>
using namespace std;
int main() // friver method
{
int arrowBaseHeight = 0; // required initialisations
int arrowBaseWidth = 0;
int arrowHeadWidth = 0;
int i = 0;
cout << \"Please Enter the desired arrow base height : \"; // prompt to enter the data
cin >> arrowBaseHeight; // getting the data
cout << \"Please Enter the desired arrow base width : \"; // prompt to enter the data
cin >> arrowBaseWidth; // getting the data
while (arrowHeadWidth <= arrowBaseWidth) { // checking for the condition
cout << \"Please Enter the desired arrow head width : \"; // prompt to enter the data
cin >> arrowHeadWidth; // getting the data
}
string ast = \"\"; // ast will contain how many asterisk we want for the base width;
int tempBaseHeight = arrowBaseWidth; // temporary variable
for (int x = 1; x <= arrowBaseHeight; x++) //iterating to form the base width of the arrow
{
for(int y = 1; y <= tempBaseHeight; y++)
{
cout << \"*\"; // printing the asterisks
}
cout << endl;
}
for (i = 1; i <= arrowBaseHeight; i++)
{
cout << ast; //Printing the base width
}
int tempHeadWidth = arrowHeadWidth;
for (int y = 1; y <= arrowHeadWidth; y++)
{
for(int z = tempHeadWidth; z > 0; z--) // iterating to print the number of asterisks we need per line in the arrowHead
{
cout << \"*\";
}
tempHeadWidth -= 1; // decrementing the value to run the code till the head width
cout << endl; // it makes a new line to keep adding more asterisks for the next row
}
}
OUTPUT :
CASE 1 :
Enter arrow base height: 5
Enter arrow base width: 2
Enter arrow head width: 4
**
**
**
**
**
****
***
**
*
CASE 2 :
Enter arrow base height: 5
Enter arrow base width: 3
Enter arrow head width: 1
Enter arrow head width: 2
Enter arrow head width: 3
Enter arrow head width: 4
***
***
***
***
***
****
***
**
*
Hope this is helpful.


