Question

C++ Write a program that asks user for a positive integer side length. If they enter...

C++

Write a program that asks user for a positive integer side length. If they enter an illegal value, they must be prompted to enter a good one until they do. It then displays, using asterisks, a filled diamond of the given side length. For example, if the side length is 4, the program should display:

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

Code(Text Format):

#include<iostream>

using namespace std;

int main(){

int side;

cout<<"Enter side length: ";

cin>>side;

/*By using this while loop, prompting the user to

enter valid side length.*/

while(side<=0){

cout<<"Please enter valid side length!"<<endl;

cin>>side;

}

//For upper part

/*space variable is used here to denote the spaces

at the beginning of each line of output.*/

int space = side - 1;

for (int i = 0; i < side; i++)

{

//printing beginnig of line spaces

for (int j = 0;j < space; j++)

cout<<" ";

//printing i+1 stars

for (int j = 0;j <= i; j++)

cout<<"* ";

cout<<endl;

space--;

}

//For lower part

space = 1;

for (int i = side-1; i > 0; i--)

{

//printing beginning of line spaes

for (int j = 0; j < space; j++)

cout<<" ";

// Printing i stars

for (int j = 0;j < i;j++)

cout<<"* ";

cout<<endl;

space++;

}

return 0;

}

Sample Outputs:

Please give feedback.

Add a comment
Know the answer?
Add Answer to:
C++ Write a program that asks user for a positive integer side length. If they enter...
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
  • Write a java program that asks the user to enter an integer. The program should then...

    Write a java program that asks the user to enter an integer. The program should then print all squares less than the number entered by the user. For example, if the user enters 120, the program should display 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

  • Using loops, write a C# program that asks the user to enter repeatedly an integer number,...

    Using loops, write a C# program that asks the user to enter repeatedly an integer number, it stops when the user enters -1, then the program should display how many numbers were entered, their the sum and their average

  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 5. Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. Input Validation: Do not accept a negative starting number.

  • 2. Write a C++ program that asks the user to enter a integer between 1 and...

    2. Write a C++ program that asks the user to enter a integer between 1 and 10. Continue to prompt the user while the value entered does not fall within the range. When the user is successful display a congratulatory message. Save file as OneToTen.

  • In Python, write a program that asks the user for a positive integer number. If the...

    In Python, write a program that asks the user for a positive integer number. If the user enters anything else, the program would ask the user to re-enter a number or enter -1 to quit. If it is a positive number, the program then prints out a multiplication table that goes up to that number. For example, if the user enters 10, the output would look something like this. https://www.vectorstock.com/royalty-free-vector/multiplication-table-on-white-background-vector-2721686

  • Write an assembler program that asks the user (as shown below) for two integers and a...

    Write an assembler program that asks the user (as shown below) for two integers and a single character representing the arithmetic operations: addition, subtraction, multiplication and integer division (displays both quotient and remainder). Perform the requested operation on the operands and display the result as specified below. Assume SIGNED NUMBERS. The program should not divide by 0! If the user attempts to divide by 0, display the error message: "Cannot divide by zero." If this error occurs, the program should...

  • C++ Write a program that asks user to input three positive integers one at a time,...

    C++ Write a program that asks user to input three positive integers one at a time, then compute and output the largest entered number. Use if-else statements for three integer comparison and use for loops for a user validation. Example output: Enter an integer: -7 Invalid! Number must be positive. Enter an integer: -2 Invalid! Number must be positive. Enter an integer: 5 Enter an integer: 7 Enter an integer: 1 Largest number: 7

  • Write a C program that asks the user to enter two real numbers. Then your program...

    Write a C program that asks the user to enter two real numbers. Then your program displays a menu that asks the user to choose what arithmetic operation to be done on those numbers. Depending on the user's entry, the program should display the result to the screen. The sample runs below show what should be done to the numbers entered by the user. Your program should run exactly like shown in the sample runs. make your code run as...

  • Write a program that asks the user to enter a string and then asks the user...

    Write a program that asks the user to enter a string and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. python programming

  • Write a program that generates an array filled up with random positive integer number ranging from...

    Write a program that generates an array filled up with random positive integer number ranging from 60 to 100, and display it on the screen. After the creation and displaying of the array, the program displays the following: [R]everse [A]dd [S]earch E[xit] Please select an option:_ Then, if the user selects: - R (lowercase or uppercase): the program displays the reversed version of the array. - A (lowercase or uppercase): the program displays the sum of the elements of the...

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
ADVERTISEMENT