61.0k views
3 votes
Write a program trapezoid.cpp that prints an upside-down trapezoid of given width and height. However, if the input height is impossibly large for the given width, then the program should report,

2 Answers

4 votes

Final answer:

The question pertains to writing a C++ program to print an upside-down trapezoid or report an error if the input height is impossible for the given width. An example code snippet demonstrates basic logic using loops to print the trapezoid shape. It includes error handling for when the input height is too large.

Step-by-step explanation:

You asked how to write a program trapezoid.cpp that prints an upside-down trapezoid of given width and height, or reports an error if the height is impossibly large for the given width. The subject of this question falls under Computers and Technology, particularly involving programming skills. Let's assume you are going to use C++ for this task since the file extension is .cpp.

Here is an example of how you could approach this problem:

\#include <iostream>
using namespace std;

int main() {
int width, height;
cout << "Enter width and height of the trapezoid: ";
cin >> width >> height;

if (height > (width+1)/2) {
cout << "Error: The height is too large for the given width." << endl;
} else {
for (int row = 0; row < height; row++) {
for (int i = 0; i < row; i++)
cout << " ";
for (int i = 0; i < width - 2*row; i++)
cout << "*";
cout << endl;
}
}
return 0;
}

This example C++ program checks if the height is too large and if it's not, it proceeds to print the upside-down trapezoid.

User Beatrice Zmau
by
4.7k points
6 votes

Answer:

#include <iostream>

using namespace std;

int main()

{

int rows, width, height, spaces, stars; // declare values

cout << "enter width" << endl;

cin >> width;

cout << "enter height" << endl;

cin >> height;

for (int row = 0; row < height; ++row) {

for (int col = height + row; col > 0; --col) {

if (height % 6 == 1) {

cout << "Impossible shape!" << endl;

return 0;

}

cout << " ";

}

for (int col = 0; col < (width - 2 * row); ++col) {

cout << "*";

spaces += 1;

stars -= 2;

}

cout << endl;

User Yogesh Malpani
by
4.9k points