Question

Having trouble coding this for C++. Please add comments for better understanding!

Write a program that draws two triangles by using a character X as examples below. The program must prompts a user for an i

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I WROTE THE CODE ALONG WITH COMMENTS.

CODE:

#include <iostream>

using namespace std;

int main()
{
int n,i,j,k,l=1;
cout<<"Input: ";
cin>>n;https://www.onlinegdb.com/online_c++_compiler#tab-stdin
while(3) //for loop used to repeat if n is 1 or 2.
{

if(n>2) //n is greather then 2 enter into if condition.
{
if(n%2==0) //check n is even or not. if even enter into if condition.
{
  
n=n+1; //n is incremented by one.
  
for(i=n;i>0;i--) //for loop used to printe x's on different lines.
{
for(j=i;j>0;j--) //for loop used to print x's on left side.
{
cout<<"x";
}
for(k=1;k<=l;k++) //for loop used to print spaces between them.
cout<<" ";
for(j=i;j>0;j--) //for loop used to print x's on right side.
{
cout<<"x";
}
cout<<endl; //next line.
l++; //increment i value.
}
}
else
{
  
for(i=n;i>0;i--) //for loop used to printe x's on different lines.
{
for(j=i;j>0;j--) //for loop used to print x's on left side.
{
cout<<"x";
}
for(k=1;k<=l;k++) //for loop used to print spaces between them.
cout<<" ";
for(j=i;j>0;j--) ////for loop used to print x's on right side.
{
cout<<"x";
}
cout<<endl; //next line.
l++; //increment i value.
}
}
break;
}
else
{
cout<<"Please try again!"<<endl; //if n is 1 or 2 enter into else loop.
cout<<"Input: ";
cin>>n;
}
  
}

return 0;
}

OUTPUT:

Input: 3 xxx XX ... Program finished with exit code 0 Press ENTER to exit console. I

Input: 4 XXXXX XXXX XXXX xxx xx х Program finished with exit code 0 Press ENTER to exit console.

Input: 1 Please try again! Input: 2 Please try again! Input: 7 XXX xxx xxx xx X ...Program finished with exit code 0 Press EN

SCREENSHOT OF THE CODE:

main.cpp 1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { int n,i,j,k,1=1; cout<<Input: ; cin>>n;https://wfor(k=1;k<=1;k++) //for Loop used to print spaces between them. cout<< ; for(j=i;j>0;j--) //for Loop used to print xs on rcout<<endl; //next line. 1++; //increment i value. } } break; } 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 } 68 else

Add a comment
Know the answer?
Add Answer to:
Having trouble coding this for C++. Please add comments for better understanding! Write a program that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please upload.cpp file only before the time up. The code must contain your name and has...

    Please upload.cpp file only before the time up. The code must contain your name and has proper format. No library function is allowed. Write a program that draws two triangles by using a character 'x as examples below. The program must prompts a user for an integer larger than 2. The user input will then be used to set the size of the output. If a user input an even value, the program will increase it by one before drawing...

  • C++: Write a C++ program with user input, output and using inheritance. Please include comments in...

    C++: Write a C++ program with user input, output and using inheritance. Please include comments in your code.

  • PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning...

    PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning of the file: Your name. The name of the class(es) used in the program. The core concept (found below) for this lesson. The date the program was written. Include a recursive method separate from the main method that will add together all of the even numbers between and including 1 and the value the user supplies. For instance, if the user enters 10 then...

  • C Program Assignment: 2-Add comments to your program to full document it by describing the most...

    C Program Assignment: 2-Add comments to your program to full document it by describing the most important processes. 3-Your variables names should be very friendly. This means that the names should have meaning related to what they are storing. Example: Instead of naming the variable that stores the hours worked for an employee in a variable called ‘x’ you should name it HoursWorked. 5-Submit your .c program (note that I only need your source code and not all files that...

  • Using Python Please Question 1: Pine Tree Write a program that, prints a 'pine tree' consisting...

    Using Python Please Question 1: Pine Tree Write a program that, prints a 'pine tree' consisting of triangles of increasing sizes, filled with a character (' oror '$' etc). Your program should consist of three functions: 1. A function print_shifted_triangle (n, m, symbol). It prints an n-line triangle, filled with symbol characters, shifted m spaces from the left margin. For example, if we call print_shifted_triangle (3, 4, +"), the expected output is: +++ Left margin +++++ 4 spaces 2. A...

  • C++ only Implement a program that prompts a user to enter a number N where N...

    C++ only Implement a program that prompts a user to enter a number N where N is a positive number. The program must validate a user input and ask a user to re-enter until an input is valid. Implement a function that pass N as an argument and return a result of calculates N! (N-factorial): The function must use loop for the implementation. Hint: Factorial: N! = N * (N-1) * (N-2) * …. * 1 ( This is only...

  • MATLAB Questions. I have a basic understanding of the tools for MATLAB, but do not know...

    MATLAB Questions. I have a basic understanding of the tools for MATLAB, but do not know how to create the code for these questions. Please show the entire code. 2. A paper cup shaped as a frustum of cone with R-14R1 is designed to have a volume of 25 cm Determine R1, R2 and the surface area, S, of the paper for cups with a height, h, of 1O cm. The volume of the cup, V, and the surface area...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • Activity 16-02 With proper indentation and comments, With proper interface/menu on the monitor Write C program...

    Activity 16-02 With proper indentation and comments, With proper interface/menu on the monitor Write C program of the following problem. This question involve repetition: while, and for statements This question involve selection: Don't use switch, use IF Marks is based on comments, indentation and interface/menu. You must try Choose any one value of j to print screen the following i Three input: num less than j, 21 and any valid number three input: A, B and X. Question: 1.0 From...

  • *Please try to use C++ specific coding for my understanding because I never learned C. A...

    *Please try to use C++ specific coding for my understanding because I never learned C. A polynomial P is a function defined by an array of coefficients CI. Example : C-(4, 2, 1), then In the general case, given the array CI of size N, we can define Write a program - with the function main0 that gets N, the coefficients C] and a value x from the user. - with a function poly( that calculates and returns the value...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT