Pyramid Help

Pages: 12
*
***
*****
*******


Hello Experts I need this output.Give me the Code for this.
Last edited on
1
2
3
4
5
cout << 
"   *"
"  ***"
" *****"
"*******";


*audience "oooo" ... "aaaahhhhh"
And the worst part is... that code probably doesn't compile. :(

A hint @the OP: Count the spaces before the pyramid, and count the stars on each line. Notice any relationships? ;)

-Albatross

Last edited on
I need this output by using loops.
A policy we have around here is to never (except perhaps under a few very special circumstances) give out solutions to problems. I'll give you a template for how your project with loops might look like along with a few MAJOR hints, but until you try the problem on your own and post the results, you'll get no further help from me. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
for (/*each row of the pyramid*/)
{
    for (/*some conditions based off the iteration that the loop is on*/)
    {
        //Print spaces.
        //Hint: max_spaces = rows - 1;
    }
    for (/*some other conditions also based off that iteration*/)
    {
        //Print asterisks.
        //Hint: max_asterisks = 2 * rows - 1
    }
}


-Albatross
Here you go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <algorithm>

struct Less{
   int n;

   Less(int n) : n(n) {}

   int f(int x){
      if( x >= 2*n ) return 3*x-6*n+2;
      if( x & 1 ) return (3*x-1)/2;
      return (6*n-3*x-6)/2;
   }

   bool operator() (int a, int b){
      return f(a) < f(b);
   }
};

struct Iota{
   int i;

   Iota(int i) : i(i) {}

   int operator() (){
      return i++;
   }
};

struct Printer{
   int n;

   Printer(int n) : n(n) {}

   void operator() ( int i ){
      if( i >= n*2 ) std::cout.put('\n');
      else if( i & 1 ) for( int j = 0; j < i; j++) std::cout.put('*');
      else for( int j = 0; j*2 < i; j++) std::cout.put(' ');
   }
};

int main(){
   int n = 4;
   int* arr = new int[n*3];
   std::generate( arr, arr+n*3, Iota(0) );
   std::sort( arr, arr+n*3, Less(n) );
   std::for_each( arr, arr+n*3, Printer(n) );
   std::cin.get();
   delete[] arr;
   return 0;
}

If there is anything that needs to be explained here, feel free to ask.
note: the loops are hidden in the STL functions generate, sort and for_each
Last edited on
line 49 should be delete [] arr;.
You may want to make it const correct.
line 49
whoops. I'll fix that..

const correct
I could, but what would I gain from that?
my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
int main()
{
       int n;
       cout<<" enter n = ";
       cin>>n;
       int i,j;
       for(i=1;i<n;i++)
       {
              for(j=i;j<n;j++)
               cout<<" ";
             for(j=1;j<2*i+1;j++)
                cout<<"*";
            }
     system("pause");
     return 0;
}
       
{
Last edited on
@tianversion: you seem to have a stray {

And wtf. btw, i<n isn't inclusive, if you want to include n, use <= or make i = 0

system("pause");...classy, All in all, I give you a C. (for complete failure)

;) jkjk however you should let 'em try their homework, as it is obvious not an
attempt was made.
here it is in C# MUAHAHAHAHAHAHAH

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pyrmaid_print
{
    class Program
    {
        static void Main(string[] args)
        {
            string spaces = "   ";
            int perLine = 1;
            int space = 0;

            for (int i = 4; i > 0; i--)
            {
                int howMany = perLine;
                Console.Write(spaces.Substring(space));
                while (howMany > 0)
                {
                    Console.Write("*");
                    howMany--;
                }
                Console.WriteLine("");
                perLine += 2;
                space++;
            }
            Console.ReadKey();
        }
    }
}
@tianversion
Keep console open http://www.cplusplus.com/forum/articles/7312/
Why system is evil http://www.cplusplus.com/forum/articles/11153/

@hamsterman
Nothing.
I was thinking about std::bind. But it needs a constant object derived from binary_function. And AFAIK you can't bind an unary function.
It seems that boost::bind fix those issues.
My turn.

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
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/arithmetic/dec.hpp>
#include <boost/preprocessor/arithmetic/sub.hpp>
#include <boost/preprocessor/arithmetic/mul.hpp>
#include <iostream>

#define SPACE(z, cur, param) " "
#define STAR(z, cur, param) "*"

#define LINE(z, cur, max) \
    BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_SUB(max,cur)), SPACE, ~) \
    BOOST_PP_REPEAT(BOOST_PP_INC(BOOST_PP_MUL(cur,2)), STAR, ~) "\n"

#define DO_IT(N) BOOST_PP_REPEAT(N,LINE,N)

int main()
{
    std::cout << DO_IT(4) << std::endl;

    std::cout << "(hit enter to quit...)" << std::endl;
    std::cin.get();

    return 0;
}
Posting full code solutions to mock the OP stopped being funny a few days after the idea was conceived. I mean, come on... really? :/

-Albatross
We don't do it to mock the OP. It's a way to express our creativity. You should try it too.

I believe the OP was given enough help by your second post here.
It should be enough to get him started, at least.
If he posts his code asking for more help, we won't deny it.
Excuses, excuses. :)

-Albatross
What's up with Albatross?
As Return 0 once said, Albatross is the forum police.
You do know I can read that, right? >_>

I... really don't want to police people around. I guess I'm just passionate about seeing that this forum is a nice place for someone to come and discuss C++ or ask for help. Sorry if I come across as being "policey". It's not at all what I intended. O_o

-Albatross
Last edited on
I bet she can't read this-> Albatross == forum police
Dang, I can't make it smaller. :(
Last edited on
Pages: 12