Welcome to CS141 Lab 4 - Learning to implement Loops
Today’s lab is structured as follows:
5 Mins - Quiz
35 Mins - Lab exercise on Loops
10 Mins - Grading
Lab Exercise
In today’s lab, you will need to use any loop statement (while, for, do while) of your choice to make certain geometric figures.
Line:
* * * * * * * * * *
Square:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Triangle of the following shape:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
The trick is to print out asterisk symbols in a way that you’re able to form the figures shown above. The input has to be taken from the user and the user gets to define the measurement of each geometric figure.
Grading Scale is as follows:
0 - Did not show up or did not code with a partner
1 point - Write a program to prompt for the length of the line. Run the program with the help of a single loop. Your output should look something like this:
Enter the length of the line:
7
* * * * * * *
2 (1 more point) - Write a program to prompt for the length of the side of the square. Run the program with the help of nested loops. Your output should look something like this:
Enter the length of the side of the square:
4
* * * *
* * * *
* * * *
* * * *
3 (1 more point, extra credit) - Write a program to prompt for the height of the triangle. Note that the height here refers to the perpendicular distance from the largest side to the opposite vertex. In other words, it is the number of asterisks in your longest line in the centre. Run the program with the help of a combination of nested loops. Your output should look something like this:
Enter the height of the triangle:
5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Note here that the centre line contains 5 asterisks.
In order to gain all 3 points, you need to display your output one after the other. In other words, if you have completed the triangle extra credit, you also need to display the line and square before it. An ideal completed program should look something like this:
Enter the length of the line:
6
* * * * * *
Enter the length of the side of the square:
7
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
Enter the height of the triangle
5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
All the best!