I need to create an inverted pyramid, please help me

R

rh75691

Hey guys,

I just need some help here, I kept on redoing this but still no luck.
I need to code an inverted pyramid:

******
****
**
*

Ok so you must be thinking that this is from my class. YES IT IS! And
you must be thinking that teaching me would defeat the purpose of
learning! YUP!

However, I want to let you know that this pyramid is killing me and I
need to pass it tomorrow. I have coded it already, but it doesn't
work. I just kept redoing it over and over.

I just need some guidance please. Please share me what you learned and
guide me.

#include<iostream>
#include<conio.h>

using namespace std;

main() {
for(int x = 18; x <= 1; x-=5) {
for(int y = 0; y <= x/3; y++) {
cout<<" ";
for(int z = 1; z<= x/3; z++) {
cout<<"*";
}
}
}

getch();
}

Thanks!
 
J

Juha Nieminen

I just need some help here, I kept on redoing this but still no luck.
I need to code an inverted pyramid:

******
****
**
*

That's easy:

#include <iostream>

int main()
{
std::cout << "******\n"
<< " ****\n"
<< " **\n"
<< " *\n";
}
 
A

anoop.fan

//The solution is

main()
{
int i,j,k;
for(i=0;i<5;i++) //loop for number of lines in pyramid
{
for(j=0;j<=i-1;j++) //loop for spaces in each line
cout<<" ";

for(k=1;k<=5-2*i;k++) // number of stars per line
cout<<"*";

cout<<"\n";
}
}
 
J

joecook

Hey guys,

I just need some help here, I kept on redoing this but still no luck.
I need to code an inverted pyramid:

Your problem is that you are trying to code this before you have
figured out how to explain it and write down (in English) how you
perform that task. After that, it should be _easy_.

For example:
Problem: Draw an inverted pyramid which start at X big at the top.

Step 1: Write * X times.
Step 2: Goto Next Line
Step 3: Indent 1 space
Step 4: Write * X-1 times.
Step 5: Indent 2 spaces
Step 6: Write * X-2 times.
....

Now, there are two things missing. An exit condition, and a loop
variable. Adding those:

Step 1: Set IndentAmount to 0;
Step 2: If X-IndexAmount is 0, we're done, return
Step 3: Write * X-IndentAmount times.
Step 4: Goto Next Line (newline)
Step 5: Increment IndexAmount by 1
Step 6: Indent 'IndexAmount' spaces


Then converting this to code is easy, right???
int main()
{
int size of pyramid = PYRAMID_SIZE;
int indentAmount = 0; //Step 1
while (PYRAMID_SIZE - indentAmount != 0) // Step 2
{
writeAsterix(PYRAMID_SIZE - indentAmount); // Step 3
std::cout<<std::endl; // Step 4
++indentAmount; // Step 5
writeSpaces(indentAmount); // Step 6
}

This is a just a raw dump from English to C++. Always think about the
steps in the language you know well so you understand what has to be
done before you try writing code.

Joe
 
M

Michael DOUBEZ

//The solution is

A not so good writing of a buggy program is
main()
{
int i,j,k;

No need for so much variable and no need to declare them outside the loop:
for(i=0;i<5;i++) //loop for number of lines in pyramid

OP doesn't want a 5 line pyramide but 4 lines
{
for(j=0;j<=i-1;j++) //loop for spaces in each line
cout<<" ";

This indentation is calling for trouble
for(k=1;k<=5-2*i;k++) // number of stars per line

With this writing you get 4* and 1* and then no * at all.
'****'
' **
' '
' '
' '

Not what the OP wants.
cout<<"*";

cout<<"\n";
}
}

It could be rewritten as:

int main()
{
//height of pyramid
const int height=4;

for(int j=height;j>0;--j)
{
//pyramid's width decreases by step of 2
//when number of star is 0 print one star
const int nbstar=j?(2*(j-1)):1;
//indenting with height-j spaces
const int nbspace=height-j;

//print line
for(int i=0;i<nbspace;++i)cout.put(' ');
for(int i=0;i<nbstar ;++i)cout.put('*');
cout.put('\n');
}
}
 
J

Juha Nieminen

Michael said:
const int nbstar=j?(2*(j-1)):1;
const int nbspace=height-j;
for(int i=0;i<nbspace;++i)cout.put(' ');
for(int i=0;i<nbstar ;++i)cout.put('*');
cout.put('\n');

How about:

std::cout << std::string(height-j, ' ')
<< std::string(j?(2*(j-1)):1, '*') << "\n";

Maybe not as efficient, but efficiency was probably not the goal.
 
Z

ZikO

Juha said:
That's easy:

#include <iostream>

int main()
{
std::cout << "******\n"
<< " ****\n"
<< " **\n"
<< " *\n";
}

It's easy if u want to draw 4 raws. How about 50? Is it still easy :p
 
Z

ZikO

That's easy:
#include <iostream>

int main()
{
std::cout << "******\n"
<< " ****\n"
<< " **\n"
<< " *\n";
}

It's only appears to be easy this way because you have focused only on 4
rows. I believe OP wants to work out an algorithm which is more general
:p. let's 50 rows. That's not easy by your way.




















ok. Im fussy :p
 
M

Michael DOUBEZ

Juha said:
How about:

std::cout << std::string(height-j, ' ')
<< std::string(j?(2*(j-1)):1, '*') << "\n";

Maybe not as efficient, but efficiency was probably not the goal.


Yes.

I even considered the verbose:
fill_n(ostream_iterator<char>(cout),nbspace,' ');
fill_n(ostream_iterator<char>(cout),nbstar ,'*');

or, for the output of stars, transforming:

const int nbstar=j>1?(2*(j-1)):1;
for(int i=0;i<nbstar ;++i)cout.put('*');

into

int nbstar=2*(j-1);
do{cout.put('*');}while(nbstar-->0);

Which would account for the case nbstar==0->1 star.
 
R

rh75691

  That's easy:

#include <iostream>

int main()
{
    std::cout << "******\n"
              << " ****\n"
              << "  **\n"
              << "   *\n";

}

Hi, our discussion is on for loops.. Thanks
 
J

James Kanze

On Feb 18, 11:48 am, (e-mail address removed) wrote:
Your problem is that you are trying to code this before you
have figured out how to explain it and write down (in English)
how you perform that task.

His problem might even be further up stream. I'm not sure
exactly what he wants: his example hat 6, 4, 2, and then 1 star,
decrementing the number of stars by two each time but the last.
Is that really what is wanted; that for n lines, line i
consists of i spaces, followed by 2*(n-1) stars, except for the
last?

At any rate, I'd start by defining the mathematical formula: for
a display of n lines, what is the equation for the number of
spaces and the number of stars, as a function of n and the line
number i. Given that:

for ( int i = 0 ; i < n ; ++ i ) {
std::cout << spaces( i, n ) << stars( i, n ) << std::endl ;
}

seems an obvious solution, with

std::string
spaces(
int lineNumber,
int lineCount )
{
// calculate spaces, here simply lineNumber
int spaceCount = lineNumber ;
return std::string( spaceCount, ' ' ) ;
}

and

std::string
stars(
int lineNumber,
int lineCount )
{
// calculate stars -- we need to special case the
// last line (lineNumber == lineCount - 1)
int starCount
= 2 * (lineCount - lineNumber - 1) ;
if ( starCount == 0 ) {
starCount = 1 ;
}
return std::string( starCount, '*' ) ;
}

(Obviously, a large part of his problem is that he's not
decomposing it enough. Also, I think he'd get a prettier
pyramid with
starCount = 2 * (lineNumber - lineCount) - 1 ;
and no special case.)

If he doesn't want to use the strings, of course, just pass an
std::eek:stream& to spaces and stars, and loop in them, outputting
the desired character.
 
G

Guest

Hey guys,

I just need some help here, I kept on redoing this but still no luck.
I need to code an inverted pyramid:

******
 ****
  **
   *

Ok so you must be thinking that this is from my class. YES IT IS! And
you must be thinking that teaching me would defeat the purpose of
learning! YUP!

However, I want to let you know that this pyramid is killing me and I
need to pass it tomorrow. I have coded it already, but it doesn't
work. I just kept redoing it over and over.

I just need some guidance please. Please share me what you learned and
guide me.

in pseudo code:

(define (-- n) (- n 1))

(define (n-times fun n)
(if (not (zero? n))
(begin
(fun n)
(n-times fun (-- n)))))

(define (display-row biggest-row n port)
(n-times (lambda (n) (display " " port)) (ceiling (/ (- biggest-
row n) 2)))
(n-times (lambda (n) (display "*" port)) n)
(newline))

(define (display-pyramid rows port)
(define (rec-disp-py rows n port)
(cond
((zero? n) (display-row rows 1 port))
((> n 0)
(display-row rows n port)
(rec-disp-py rows (- n 2) port)
)))

(rec-disp-py rows rows port))

(define (pyramid)
(display-pyramid 6 (current-output-port)))
 
Z

Zeppe

Hey guys,

I just need some help here, I kept on redoing this but still no luck.

my help is: do not rely on luck.
I need to code an inverted pyramid:

******
****
**
*

Ok so you must be thinking that this is from my class. YES IT IS! And
you must be thinking that teaching me would defeat the purpose of
learning! YUP!

However, I want to let you know that this pyramid is killing me and I
need to pass it tomorrow. I have coded it already, but it doesn't
work. I just kept redoing it over and over.

You know that you are doing this in order to learn. The pyramid itself
is of no interest whatsoever. So, either you learn it, it's pointless.
I just need some guidance please. Please share me what you learned and
guide me.

Sit in front of a piece of paper and work out the algorithm. That's what
I learnt.

Best wishes,

Zeppe
 
J

Juha Nieminen

Hi, our discussion is on for loops.. Thanks

My reply was sarcastic.

Your original question didn't mention anything about loops or how the
pyramid should be generated. You gave exactly one example output and
absolutely no other explanation of what exactly is it that you want.
Thus my reply, albeit sarcastic, is a correct answer to your question.
 
P

Pascal J. Bourguignon

Hey guys,

I just need some help here, I kept on redoing this but still no luck.
I need to code an inverted pyramid:

******
****
**
*

Do you notice that your pyramid has something wrong with it?

What about displaying rather:

*******
*****
***
*
I just need some guidance please. Please share me what you learned and
guide me.

Count the stars and the spaces... It's easier to count the spaces if
you replace them with something else (not an underline!):


.....******* 4 7
......***** 5 5
.......*** 6 3
........* 7 1

What do you notice with these two series?

When does it stop?


#include<iostream>
#include<conio.h>
using namespace std;
int main() {
int spaces;
int stars;
for(spaces=??,stars=??; ??? ; spaces+= ?? , stars-= ??){
// display spaces spaces, and stars stars, and a newline.
??
}
return(0);
}
 
R

rh75691

in pseudo code:

(define (-- n) (- n 1))

(define (n-times fun n)
    (if (not (zero? n))
        (begin
            (fun n)
            (n-times fun (-- n)))))

(define (display-row biggest-row n port)
    (n-times (lambda (n) (display " " port)) (ceiling (/ (- biggest-
row n) 2)))
    (n-times (lambda (n) (display "*" port)) n)
    (newline))

(define (display-pyramid rows port)
    (define (rec-disp-py rows n port)
        (cond
              ((zero? n) (display-row rows 1 port))
              ((> n 0)
                  (display-row rows n port)
                  (rec-disp-py rows (- n 2) port)
              )))

     (rec-disp-py rows rows port))

(define (pyramid)
    (display-pyramid 6 (current-output-port)))

Hi nick, I don't understand that code yet.. Sorry. I'm pretty newbie
 
D

Daniel T.

It's only appears to be easy this way because you have focused only on 4
rows. I believe OP wants to work out an algorithm which is more general
:p. let's 50 rows. That's not easy by your way.

ok. Im fussy :p

He only focused on 4 rows because that was an explicit requirement of
the original problem. The problem statement didn't as for a general
algorithm to draw pyramids.

When the requirement is to draw a 50 row pyramid, or a pyramid of a
variable number of rows, the solution will of course be different.
 
R

rh75691

A not so good writing of a buggy program is




No need for so much variable and no need to declare them outside the loop:


OP doesn't want a 5 line pyramide but 4 lines


This indentation is calling for trouble




With this writing you get 4* and 1* and then no * at all.
'****'
' **
'  '
'   '
'    '

Not what the OP wants.



It could be rewritten as:

int main()
{
   //height of pyramid
   const int height=4;

   for(int j=height;j>0;--j)
   {
     //pyramid's width decreases by step of 2
     //when number of star is 0 print one star
     const int nbstar=j?(2*(j-1)):1;
     //indenting with height-j spaces
     const int nbspace=height-j;

     //print line
     for(int i=0;i<nbspace;++i)cout.put(' ');
     for(int i=0;i<nbstar ;++i)cout.put('*');
     cout.put('\n');
   }

}

Hello Michael,

Thanks for explaining the code for me, actually anoop's code result to
this:
*****
***
*
\n
\n
\n

There was a 3 blank lines. I don't really get how his code works
because:

main()
{
int i,j,k;
for(i=0;i<5;i++) //loop for number of lines in pyramid
{
for(j=0;j<=i-1;j++) //loop for spaces in each line
cout<<" ";

for(k=1;k<=5-2*i;k++) // number of stars per line
cout<<"*";

cout<<"\n";
}

getch();
}


Look at 5-2*1; At our first iteration this wud be 3*. But the first
line resulted to 5 stars.

Then on our second iteration the stars should be 5-2*2 = 1*.
But it resulted to 3 stars.

I dont get it!
 
J

Jerry Coffin

in pseudo code:

(define (-- n) (- n 1))

(define (n-times fun n)
(if (not (zero? n))
(begin
(fun n)
(n-times fun (-- n)))))

(define (display-row biggest-row n port)
(n-times (lambda (n) (display " " port)) (ceiling (/ (- biggest-
row n) 2)))
(n-times (lambda (n) (display "*" port)) n)
(newline))

(define (display-pyramid rows port)
(define (rec-disp-py rows n port)
(cond
((zero? n) (display-row rows 1 port))
((> n 0)
(display-row rows n port)
(rec-disp-py rows (- n 2) port)
)))

(rec-disp-py rows rows port))

(define (pyramid)
(display-pyramid 6 (current-output-port)))

If you're going to use tail recursion instead of a real loop, why not
something simple and straightforward like this:

bool pyramid(int lineCount, int current=0) {
int i=2*(lineCount-current);
std::cout << std::setw(i+current) << std::string(i, '*') << '\n';
return (current < lineCount) && pyramid(lineCount, current+1);
}

Of coure, I've cheated and ignored the special case for the last row,
but if you're ignoring the really basic requirement of using a for loop
why worry about something as trivial as working incorrectly? :)
 
Z

Zeppe

Hey guys,

I just need some help here, I kept on redoing this but still no luck.
I need to code an inverted pyramid:

******
****
**
*

// pyramid
#include <iostream>
int main() {
unsigned n = 60;
for(unsigned i = 0; i < n*(n+1)/2; ++i)
std::cout << (i%(n+1) == n ? '\n' : i%(n+1)-i/(n+1) < n-1-2*i/(n+1)
? '*' : ' ');
}

// nuke!!
#include <iostream>
int main() {
unsigned n = 60;
for(unsigned i = 0; i < n*(n+1); ++i)
std::cout << (i%(n+1) == n ? '\n' : i%(n+1)-i/(n+1) < n-1-2*i/(n+1)
? '*' : ' ');
}

:p

Zeppe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top