C++ ques:

i want a program dat displays d following pattern:

15,14,13,12,11
10,9,8,7
6,5,4
3,2
1
So what's the problem?
That could theoretically be done with one std::cout line, though I suppose your teacher/professor (you are in a class, right?) would expect something that has one or maybe two decreasing for loop(s).

EDIT: The two-loop solution would be easier.

-Albatross
Last edited on
I want a pony.
Last edited on
Folowing program does that:
1
2
3
4
int main()
   cout << " 15,14,13,12,11\n 10,9,8,7\n 6,5,4\n 3,2\n 1 ";
   return 0;
}
> i want a program dat displays d following pattern:

Here's one more. Take your pick.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

template< int N, int V = N * (N+1) / 2 > struct print_line
{
    print_line()
    {
        for( int i = 0 ; i < (N-1) ; ++i ) std::cout << V-i << ',' ;
        std::cout << V-N+1 << '\n' ;
    }
};

template< int N = 5 > struct triangle : print_line<N>, triangle<N-1> {};

template<> struct triangle<1> : print_line<1> {};

int main() { triangle<>() ; }

Topic archived. No new replies allowed.