Lab 8 – Pair
Programming Loops
The following is a program that prints an n line triangle. Expand this program such that it will print
an n line triangle and an n line
square. For example if you wish to print
a 5 line triangle and square the output of the program would be as follows:
How many lines? 5
*
* *
* *
* *
* * * * *
* * * * *
* *
* *
* *
* * * * *
Press any key to continue .
. .
A ten line output would look like this:
How many lines? 10
*
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * *
* * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * *
Press any key to continue .
. .
#include <iostream>
#include <iomanip>
using namespace std;
int
main()
{
int i, j;
int space
= 2; //number
of spaces to print
int lines; //number of lines in
the triangle
cout
<< "How many lines? ";
cin >> lines;
//Print triangle
// Print first
line
cout << '*'
<< endl;
// Print middle
for (i =
1; i < lines -1; ++i)
{
cout << '*' << setw(space) << '*' << endl;
space +=2;
}
//Print last
line.
cout << '*';
for (i =
0; i < lines-1; i++)
cout << " *";
cout << endl << endl;
//Printing
square
//Place your
square code here
return 0;
}
Extra Credit: Prompt the user enter a ‘t’ if they wish to print a triangle and an ‘s’ to print a square. Print the geometric figure of the user’s choice. Make sure you do error checking to guarantee the user enters the requested characters.