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

Sample run: Result should look exactly like the sample run Write a C++ program t

ID: 3787423 • Letter: S

Question

Sample run: Result should look exactly like the sample run
Write a C++ program to print a variable number of rows of characters arranged in the form of one to four side-by-side triangles. The user chooses the number of triangles and the triangle size Here is a sample run in which the user asks for 4 triangles of size 5 -bash-4.3$ /triangles enter number and size of triangles 4 5 k -bash-4.3$ o The source code file must be named triangles .cpp and we suggest you create a directory named -/cs 16/p3 to store it In o Your program must make certain the user enters a number of triangles between 1 and 4, and a size greater than 0 in response to the prompt. If a faulty value is entered, the program does not print triangles and does not exit, but instead it calmly informs the user about the mistake(s) and prompts for the number of triangles and size again. (See "sample runs" link below.)

Explanation / Answer

#include <iostream>
using namespace std;

int main() {
   // your code goes here
   cout << "enter the number and size of triangles ";
   int n, s;
   cin >>n>>s;
   if (n > 4) {
cout<<"number must be between 1 and 4";
return 0;
}
if (s <= 0) {
cout<<"size must be greater than 0";
return 0;
}
for (int i = 1; i <= s; i++) {
if (n >= 1) {
for (int j = 1; j <= i; j++) {
cout<<"*";
}
for (int j = i + 1; j <= s; j++) {
cout<<" ";
}
}
if (n >= 2) {
cout<<" ";
for (int j = 1; j <= s - i + 1; j++) {
cout<<"*";
}
for (int j = s - i + 2; j <= s; j++) {
cout<<" ";
}
}
if (n >= 3) {
cout<<" ";
for (int j = 1; j <= i - 1; j++) {
cout<<" ";
}
for (int j = i; j <= s; j++) {
cout<<"*";
}
}
if (n >= 4) {
cout<<" ";
for (int j = 1; j <= s - i + 1; j++) {
cout<<" ";
}
for (int j = s - i + 1; j <= s; j++) {
cout<<"*";
}
}
cout<<" ";
}
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote