Re: I need to create an inverted pyramid, please help me
In article <bb10862b-d523-4377-8ab6-a21bfc1056a7@
17g2000vbf.googlegroups.com>, nick_keighley_nospam@hotmail.com says...
On 18 Feb, 16:48, rh75...@gmail.com wrote:
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)))
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) {
=09int i=2*(lineCount-current);
=09std::cout << std::setw(i+current) << std::string(i, '*') << '\n';
=09return (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? :-)
--
Later,
Jerry.
The universe is a figment of its own imagination.