Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q1. Write a single program that uses loops to display Patterns A and B (see samp

ID: 3702247 • Letter: Q

Question

Q1. Write a single program that uses loops to display Patterns A and B (see sample run below). Your first loop will display Pattern A, and then write a second loop to display Pattern B. Your program should prompt the user to enter the Height of the pattern. Your program should also include a loop that lets the user repeat the program until the user says he or she is done Input Validation for Height: Do not accept zero or a negative value for height. That is if the user; enters 0 or a negative number, your program should prompt the user to re-enter a positive number for the height. Sample Run. Text in red is user entered values Enter Height:!5 Printing Pattern A Printing Pattern B Do you wish to run the program again Enter y/Y to run or n/N to quit: y Enter Height: -8 Height has to be positive Enter Height: 8 Printing Pattern A Printing Pattern B Do you wish to run the program again Enter y/Y to run or n/N to quit: n Done

Explanation / Answer

Executable code:

#include <iostream>
using namespace std;
int main()
{
char ch='y';
//prompts the user to select yes or no continue
while(ch=='y'||ch=='Y'){
//h is the height of the pattern
int h;
cout<<"Enter height: "<<endl;
cin>>h;
//if height is negative or zero its asked to enter again
if(h<=0)
{
cout<<"Height has to be positive"<<endl;
cin>>h;
}
//prints the pattern A using loops
cout<<"Printing Pattern A: "<<endl;
for(int i=1;i<=h;i++){
for(int j=i;j>0;j--)
cout<<"#";
cout<<endl;
}
//prints the pattern B just as opposite to pattern A
cout<<"Printing Pattern B:"<<endl;
for(int i=h;i>=0;i--){
for(int j=0;j<i;j++)
cout<<"#";
cout<<endl;
}
//Asks whether the user want to continue
//y->continue n->stop
cout<<"Do you wish to run the program again"<<endl;
cout<<"Enter y/Y to run or n/N to quit"<<endl;
cin>>ch;
}
cout<<"Done";
return 0;
}

sample input:

5 y -4 6 y 8 n

sample output: