how to cout symbols in specific shapes

I have to solve a problem which will help me with a more difficult one. I must write an algorithm that displays a triangle made of numbers. More details below:

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
#include <iostream>
using namespace std;
main()
{
    int n, i, j;                        
    cin>>n; // n is the variable which determines the size of the triangle
    for(i=1;i<=n;i++)  //i represents the number of elements from one line
    {for(j=1; j<=i;j++) //j represents the number of lines which is equal to i

        cout<<2*j-1<<" "<<endl;
    }
}

/* the program should return:
1
1 3
1 3 5
...
1 3 5...2n-1
 
but instead returns:
1
1
3
1
3
5
...
1
3
5
...
2n-1 */  


After doing this I'll have to write an algorithm that displays a rhombus made of stars with specific dimensions. But this one will come only after the one I showed you now.

I realized that I really don't know how to give shape to the data returned by the program. I even tried with a square matrix ( v[i][j]) but the numbers were still displayed one under another like in the example above.
That's because you cout endl after each number. You will need to cout endl after each line not each number. Move the cout << endl; part to the outter for loop.
Last edited on
{
int n, i, j;
cin>>n; // n is the variable which determines the size of the triangle
for(i=1;i<=n;i++) //i represents the number of elements from one line
{for(j=1; j<=i;j++) //j represents the number of lines which is equal to i

cout<<2*j-1<<" ";
}
cout<<\n;
}

try this one...i am a beginner but i think it should work as you want
And yea i also want to solve the rhombus of asterisks problem. Please tell me if you get it work!
Thanks. This is how it must be written:
1
2
3
4
5
6
7
8
9
  for(i=1;i<=n;i++)  //i represents the number of elements from one line
    {for(j=1; j<=i;j++) //j represents the number of lines which is equal to i
{


cout<<2*j-1<<" ";
    }
    cout<<endl;
} 


About that rhombus problem, I'll work on it tomorrow, or the day after tomorrow and I'll post my trial.
Last edited on
COOL
It is not nice to mix "\n" and endl. Also you shouldn't have just main () { ... } but for example int main (int argc, char ** argv) { ... return 0; }. Maybe you should do some proper reading and not just hopping into programming hoping all the problems will eventually solve themselves.
Last edited on
There is no reason not to use '\n' "\n" or std::endl as you wish. std::endl is functionally the same as a newline followed by a flush of the output stream. "\n" is a c-string, of course, so it requires a (slight) bit more than '\n' for std::cout to process, but it's very unlikely you'd ever notice a difference.

It is true that main always returns int, and the prototype you give is accurate for programs that wish to access parameters the program was invoked with, but the return 0; is the same as letting execution fall off the end of main and isn't necessary. main is the only function for which this is true.

Last edited on
Topic archived. No new replies allowed.