I'm attempting to create a computer program that prints a triangle of digits, as well as a "ruler" that measures the user specified width of the screen, showing the width in digits. The ruler must have a tens row and a ones row.
The program reads three integers:
a) the screen width (maximum 80)
b) the width of the triangle.
c) the position of the triangle relative to the left margin.
The '1' column of the triangle must be printed at the given position on the screen. If the screen width is not wide enough to fit the entire triangle, the triangle must be clipped. In the extreme case, if the position is greater than the screen width, nothing is drawn (the entire triangle is clipped).
Here's a couple examples:
(Sorry about the code tags, it's the only way I could show the correct spacing)
Example one:
Enter screen width: 20
Enter triangle width: 4
Enter position: 7
1 2
12345678901234567890
1234
123
12
1
Example two:
Enter screen width: 10
Enter triangle width: 4
Enter position: 8
1
1234567890
123
123
12
1
Example three:
Enter screen width: 10
Enter triangle width: 4
Enter position: 9
1
1234567890
12
12
12
1
So, here's my problem. I've figured out how to properly print the "rulers" and the triangle. But, I can't figure out how to position the triangle, or clip the triangle if it is too wide. My teacher mentioned that it would require a 'For' loop nested within another for loop. How could I do this? I put a note in the code where I think the for loop should go, am I right?
Here's the code I have so far:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int screenWidth, triWidth, position;
cout << "Enter Screen Width: ";
cin >> screenWidth;
cout << "Enter Triangle Width: ";
cin >> triWidth;
cout << "Enter Position: ";
cin >> position;
int ruler = (screenWidth / 10);
for (int topRuler = 1; topRuler <= ruler; topRuler++)
{
cout << " " << topRuler; // Prints the 1st ruler
}
cout << endl;
int showNextRuler;
for (int nextRuler = 1; nextRuler <= screenWidth; nextRuler++)
{
if (nextRuler % 10 == 0)
{
showNextRuler = 0;
}
else if ((nextRuler > 10) && (nextRuler < 20))
{
showNextRuler = (nextRuler - 10);
}
else if ((nextRuler > 20) && (nextRuler < 30))
{
showNextRuler = (nextRuler - 20);
}
else if ((nextRuler > 30) && (nextRuler < 40))
{
showNextRuler = (nextRuler - 30);
}
else if ((nextRuler > 40) && (nextRuler < 50))
{
showNextRuler = (nextRuler - 40);
}
else if ((nextRuler > 50) && (nextRuler < 60))
{
showNextRuler = (nextRuler - 50);
}
else if ((nextRuler > 60) && (nextRuler < 70))
{
showNextRuler = (nextRuler - 60);
}
else if ((nextRuler > 70) && (nextRuler < 80))
{
showNextRuler = (nextRuler - 70);
}
else if (nextRuler >= 80)
{
showNextRuler = 0;
}
else if (nextRuler < 10)
{
showNextRuler = nextRuler;
}
cout << showNextRuler; // Prints the 2nd ruler
}
cout << endl;
cout << endl;
//
// GOOD UP TO HERE
//
for (int i = triWidth; i >= 1; i--)
{
for (int triangle = 1; triangle <= i; triangle++)
// THIS is where I think I need to put the loop to Position and Clip the triangle.
// Am I right? If so, how would it work?
{
int showTriangle;
if (triangle % 10 == 0)
{
showTriangle = 0;
}
else if ((triangle > 10) && (triangle < 20))
{
showTriangle = (triangle - 10);
}
else if ((triangle > 20) && (triangle < 30))
{
showTriangle = (triangle - 20);
}
else if ((triangle > 30) && (triangle < 40))
{
showTriangle = (triangle - 30);
}
else if ((triangle > 40) && (triangle < 50))
{
showTriangle = (triangle - 40);
}
else if ((triangle > 50) && (triangle < 60))
{
showTriangle = (triangle - 50);
}
else if ((triangle > 60) && (triangle < 70))
{
showTriangle = (triangle - 60);
}
else if ((triangle > 70) && (triangle < 80))
{
showTriangle = (triangle - 70);
}
else if (triangle >= 80)
{
showTriangle = 0;
}
else if (triangle < 10)
{
showTriangle = triangle;
}
cout << showTriangle; // Prints the Triangle
}
cout << endl;
}
}