Building a Triangle

Nov 13, 2010 at 4:18am
I'm trying to build a triangle for a program but I can't seem to get it built the way I want. I feel that I have part of my code right, but something definitely needs to be added. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
using namespace std;

int main(void)
{
    auto    int    rows;
    auto    int    index;
    auto    char   stick = '|';
   
    cout << "Enter rows: ";
    cin >> rows;

    for (index = 0; rows > index; ++index)
        {
        cout << index + 1 << setw((rows - 1) * 2)
             << stick << endl;
        }
    return 0;
}


Here is a link for output.

http://img.photobucket.com/albums/v641/Brianzor/testing.jpg?t=1289621766

Any help would be appreciated.

Thanks,
Brian

Edit: Not looking for answers, just a hint or push.
Last edited on Nov 13, 2010 at 4:32am
Nov 13, 2010 at 5:11am
one loop won't suffice,it must be nested,the counter of the first loop("index") will be use in your second loop.

1
2
3
4
5
6
7
8
9
10
11
12
13

for(index=0;index<(rows*2);index+=2)
     {
         for()
               {
               }
          for(x=0;x<index;x++)
               {
               }
           std::cout<<std::endl;
     }




i didn't tried this code but i know this is how it supposed to be when i coded it before,
Last edited on Nov 13, 2010 at 5:15am
Nov 13, 2010 at 10:30am
I'm having quite a difficult time trying to interpret the code you've just given, especially since you said you haven't tried it yourself. I've looked into nested for loops but can't seem to figure them out.
Nov 13, 2010 at 7:47pm
Bump.
Nov 13, 2010 at 8:55pm
Look for different types of loops, I am not very familiar with them, but I don't think For is the correct loop to use. Look at Do-Whiles, Whiles, and such... but that's just my inexperienced suggestion.
Nov 13, 2010 at 11:47pm
HInt : look up setw and setfill in the search box above.

This is a great site not only for it's forums, but also for it's reference section. They have great example code that you can try.
Nov 14, 2010 at 6:30am
Thank you for all the help guys, but I still can't seem to figure it out. I either output nothing or garbage. I think I'm ready for a solid answer on how to do this.
Nov 14, 2010 at 12:45pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <iomanip>

using namespace std;

//Declare your variables.
int i;          //Loop iterator.
char stick='|'; //Stick character
int rows;       //Number of rows.

int main()
{

    cout << "Enter Rows: ";
    cin >> rows;

    for (i=1; i<=rows; i++) //Start rows.
    {
        cout << i;  //Print row number.
        //Print a field of spaces equal to the number of rows minus half the width of the triangle, plus 2 spaces
        //between the row number and the triangle,
        cout << setfill (' ') << setw(rows-i+2) << " ";
        //Print a field of pipes equal to the number of row number*2 then subtract 1 to keep the width odd and centered.
        cout << setfill ('|') << setw((i*2-1)) <<"|";
        cout << "\n\n";  //Print carriage return.
    }


return 0;
}
Last edited on Nov 14, 2010 at 12:46pm
Nov 17, 2010 at 5:22am
Thanks for your help there HooklessFastener but I found out today that using only for loops would make my project way harder, so a friend suggested arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>
using namespace std;

void SetArray(int Array[], const int numRows);
void DisplayArray(int Array[], int numCols, const int numRows);
int main(void)
{
    auto    int     numRows;
    auto    int     numCols;

    cout << "Enter # of rows: ";
    cin >> numRows;

    auto    int     Array[numRows];

    SetArray(Array, numRows);
    numCols = numRows * 2 - 1;
    DisplayArray(Array, numCols, numRows);

    return 0;
}
void SetArray(int Array[], const int numRows)
{
    auto    int     index;

    for(index = numRows - 1; index >= 0; --index)
        {
        Array[index] = ((index + 1) * 2) - 1;
        }
}
void DisplayArray(int Array[], int numCols, const int numRows)
{
    auto    int     row;
    auto    int     col;
    auto    int     lower = numRows;
    auto    int     upper;


I've gotten this so far but I've hit a wall and have been trying to get it to display. (Arrays were never really my strong point) Can anyone shed a little light onto the situation?
Last edited on Nov 17, 2010 at 5:27am
Topic archived. No new replies allowed.