# Triangle pattern

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 11-09-2007
SRS
Hints:
You will need another for loop nested prior to the for loop printing the letters, and a little bit of math to figure how many spaces determined by which letter you are printing...
• 11-09-2007
CornedBee
REALLY simple math at that. Every row is the same width x, of which y spaces are covered with letters. How many spaces?
• 11-09-2007
Tesctassa
Quote:

Originally Posted by SRS
You will need another for loop nested prior to the for loop printing the letters, and a little bit of math to figure how many spaces determined by which letter you are printing...

Nope! No need of a third loop. Just two as before plus one more (constant ;)) variable and some math.

:)
• 11-09-2007
SRS
ahh, yes
Quote:

Originally Posted by Tesctassa
Nope! No need of a third loop. Just two as before plus one more (constant ;)) variable and some math.

:)

dynamethod :: WARNING: do not read! may be more than a hint! :p
I don't want to ruin your post or education, but I am more curious than allowing this to slip by...

I didn't consider that possibility, but i know how... Now I want to know which way is the better way to go?
A:
Code:

```for()  //  cycle lines {     for() {}  //  print leading spaces     for() {}  //  print letters }```
B:
Code:

```for()  //  cycle lines {     for()  //  cycle prints per line     {         if() {}  //  print leading spaces         else {}  //  print letters     } }```
Is running a loop not better than comparing each cycle?
Is there another way better than both of these?
• 11-10-2007
In the first line, you printed out 3 spaces, and then 1 letter. Second line, 2 spaces, and two letters. Third line, 1 space, and 3 letters. Last line, 0 spaces and 4 letters.

Every line adds up to 4 char's, and the spaces are always printed (in this configuration), FIRST. Could you use a loop for the spaces, and then a nested loop inside that, for the printing of the letter?

This can also be done in one loop, but the nesting of the loops is more educational.
• 11-10-2007
CornedBee
Adak: what? There's no third nesting of loops. That would be incredibly hard to get right.

SRS: Method A is better. So yes, there's a third loop. There's a way of getting rid of both inner loops if you know the interface of std::string well enough, though.
• 11-10-2007
dynamethod
Code:

```#include<iostream> #include<string> using namespace std; int main() {         int x;         int y;         char cLetter;                 for(y = 1; y <= 4; y++)         {                cout << string(abs(y-5),' ');                                 for(x=1; x<=y; x++)                 {                 cLetter=64+y; cout << cLetter;                 }                         cout << "\n";         }         cin.get();         return 0; }```
i feel rotten about this though because ive borrowed the code from another post, which i found here:
Code:

`http://cboard.cprogramming.com/showpost.php?p=673514&postcount=3`
after i done a search, i dont quite understand the string function fully, particularly the 'abs', but it works, still probably shouldnt have done this lol. im now doing a bit of research into the string funcion now though
• 11-10-2007
CornedBee
The abs(y - 5) is just stupid. Write 5 - y and it works the same, not to mention that it's easier to understand.

Also, the loops are weird (0 to n-1 is more idiomatic in C++ than 1 to n), and the code makes a greater assumption about character values than usual. (Most code assumes that characters are sequential. This code assumes that they have ASCII codes.)

Then there's the redundant computation of cLetter in the inner loop instead of outside the loop, where it belongs, and the string trick could be used there, too, so why isn't it?
• 11-10-2007
dynamethod
if i try the same code without the 'abs':

Code:

```#include<iostream> #include<string> using namespace std; int main() {         int x;         int y;         char cLetter;                 for(y = 1; y <= 4; y++)         {                cout << string(y-5,' ');                                 for(x=1; x<=y; x++)                 {                 cLetter=64+y; cout << cLetter;                                 }         cout << "\n";         }                 cin.get();         return 0; }```
the code compiles, but when i try execute the app i get:

Code:

```terminate called after throwing an instance of 'std::length_error'   what():  basic_string::_S_create Aborted (core dumped)```
• 11-10-2007
CornedBee
Quote:

Write 5 - y
Look closely.
• 11-10-2007
anon
Code:

```for(y = 1; y <= 4; y++)         {                cout << string(y-5,' ');```
y-5 is going to end up being a negative value.

And it would be a good thing to get into the habit of representing ranges as [0, n) - from 0 inclusive to n exclusive. This is the way the standard libraries do it and it can reduce adding and substracting a magic 1 in many places. The main reason is probably that it matches how arrays are indexed in C++.

So a loop that would loop 4 times would be:
Code:

`for (int i = 0; i < 4; ++i)`
• 11-10-2007
dynamethod
Quote:

Originally Posted by CornedBee
Look closely.

Sorry :S, i should pay more attention

heres what i have now:

Code:

```#include<iostream> #include<string> using namespace std; int main() {         int y;         char cLetter;         cLetter = 64;                 for(y = 1; y <= 4; y++)         {                        cout << string((5-y),' ');                 cout << string((0+y), cLetter+y);                 cout << "\n";         }                 cin.get();         return 0; }```
i still think im not using the string function properly, string function is new territory for me, ive still alot to learn :S
• 11-10-2007
dynamethod
Quote:

Originally Posted by anon
Code:

`for (int i = 0; i < 4; ++i)`

when i set y in my case to 0, i seem to have a gap at the top line of the output like this:

Code:

```      A   BB  CCC DDDD```
where as i set y to 1, i get:

Code:

```  A   BB  CCC DDDD```
• 11-10-2007
CornedBee
It's not a function, it's a class. You're constructing a temporary object.
• 11-10-2007
dynamethod
ok, just edited my code a bit more, i feel even more stupid for that last piece of code i posted now lol

ive done without the char cLetter -

Code:

```#include<iostream> #include<string> using namespace std; int main() {         for(int y = 0; y <= 4; y++)         {                        cout << string((4-y),' ');                 cout << string((0+y), 64+y);                 cout << "\n";         }                 cin.get();         return 0; }```
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last