*** ***** ******* The second ric ** *** **** ***** ** ** *** ***

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

#include <iostream>
using namespace std;
int main()
{
char ch; //the fill character
int maxRowWidth; //the maximum width of the inverted triangle
cout << "Enter character for pattern ";
ch = cin.get();
cout << "Enter the maximum width of the the triangle \n";
cout << "This must be an odd number from 3 through 79 ";
cin >> maxRowWidth;
//input validation loop
while (maxRowWidth < 3 or maxRowWidth > 79)
{
cout << "Enter an odd number (from 3 up to 79) ";
cin >> maxRowWidth;
}
//make sure number of rows is odd by adding one to any even input value
if (maxRowWidth % 2 == 0)
maxRowWidth++;
cout << endl;

//This nested loop structure outputs an indented solid triangle with its
//longest row at the bottom of the triangle and against the left edge of the output screen

//outer loop controls output of the rows in the figure
//example: If maxRowWidth = 13 then there are 7 rows in triangle
//you can write the outer for loop differently if you wish
for (int i = 1; i <= maxRowWidth; i+=2) // i tracks the current row
{
//nested loop controls indentation by printing spaces



//nested loop controls printing one row of characters



}

//this nested loop structure outputs a right triangle with a vertical leg on its right
//the triangle is maxRowWidth rows high.
//outer loop controls the rows
for ( )
{
//loop does the spaces


//loop does the characters



}

return 0;
}

### 4.54 Triangle Patterns Using Nested For Loops

Modify the program in code editor 4.12.1 to output the first triangle right side up but indented. For example, if the maximum width is 7, the triangle would look like this:

```
     *
    ***
   *****
  *******
```

The second right triangle will have its vertical side on the right.

```
      *
     **
    ***
   ****
  *****
 ******
*******
```

Input will be a character and the maximum row width. Your program may not use `setw()` or tabs.
Transcribed Image Text:### 4.54 Triangle Patterns Using Nested For Loops Modify the program in code editor 4.12.1 to output the first triangle right side up but indented. For example, if the maximum width is 7, the triangle would look like this: ``` * *** ***** ******* ``` The second right triangle will have its vertical side on the right. ``` * ** *** **** ***** ****** ******* ``` Input will be a character and the maximum row width. Your program may not use `setw()` or tabs.
Expert Solution
Source Code

#include<iostream>

using namespace std;

int main(){
    char ch; //the fill character
    int maxRowWidth; //the maximum width of the inverted triangle
    cout << "Enter character for pattern ";
    ch = cin.get();
    cout << "Enter the maximum width of the the triangle \n";
    cout << "This must be an odd number from 3 through 79 ";
    cin >> maxRowWidth;
    //input validation loop
    while (maxRowWidth < 3 or maxRowWidth > 79){
        cout << "Enter an odd number (from 3 up to 79) ";
        cin >> maxRowWidth;
    }
    //make sure number of rows is odd by adding one to any even input value
    if (maxRowWidth % 2 == 0)
        maxRowWidth++;
    cout << endl;

    //This nested loop structure outputs an indented solid triangle with its
    //longest row at the bottom of the triangle and against the left edge of the output screen

    //outer loop controls output of the rows in the figure
    //example: If maxRowWidth = 13 then there are 7 rows in triangle
    //you can write the outer for loop differently if you wish
    for (int i = 1; i <= maxRowWidth; i+=2) // i tracks the current row
    {
        //nested loop controls indentation by printing spaces
        for(int j=1;j<=(maxRowWidth-i)/2;j++){
            cout << " ";
        }
        //nested loop controls printing one row of characters
        for(int j=1;j<=i;j++){
            cout << ch;
        }
        cout << endl;
    }

    //this nested loop structure outputs a right triangle with a vertical leg on its right
    //the triangle is maxRowWidth rows high.
    //outer loop controls the rows
    for (int i=1;i<=maxRowWidth;i++){
        //loop does the spaces
        for(int j=1;j<=maxRowWidth-i;j++){
            cout << " ";
        }
        //loop does the characters
        for(int j=1;j<=i;j++){
            cout << ch ;
        }
        cout << endl;
    }
return 0;
}

 

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education