Starting from:

$34.99

CS150 Homework #2 Solution

Write a program that asks the user to enter the size of a triangle to write to a text file (an integer from 1 to 50), then print the triangle into a file by printing a series of lines consisting of asterisks. Print “Triangle saved to file” to the screen, but do NOT print the triangle itself to the screen, only to the file. Name the output file triangle.txt
Input Validation: If the user enters an integer less than 1 or more than 50 tell them it is out of range and have them re-enter it (do not print the triangle for the invalid input, only for the final value in the range of 1-50 inclusive: 1 and 50 are both ok).
The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user. On the next line print one less asterisk and continue by decreasing the number of asterisks by 1 for each successive line until only one asterisk is printed. Hint: Use nested for loops; the outside loop controls the number of lines to print, and the inside loop controls the number of asterisks to print on a line.
For example, if the user enters 5, the output to the file would be:
*
**
***
****
*****
****
***
**
*
Please provide screenshots of your program running (console window AND file contents) for the following test cases (upload screenshots to your repl):
Test Case A: 1
Test Case B: 2
Test Case C: 5
Test Case D: -1 (re-enter), 0 (re-enter), 51 (re-enter), 10
(Triangles should NOT be created for -1, 0 or 51)
You must include your algorithm (list of steps taken by the program) as a comment at the top of your code or a flowchart showing the design of your program in your repl. Hint: Creating a file with the extension .draw in your repl will provide drawing tools.

More products