Jump to content

for function (making various objects)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Simplistic

Simplistic

    Newbie

  • Members
  • Pip
  • 7 posts
hello.
i have an assignment to do for school presentation on pascal ''for'' function.
my teacher gave me the task to make various objects with ''for''.
for example, a pyramide
*
***
****
(a user has to enter number of rows on pyramide, and i have to devide an algoritam which will make a pyramide based on a number that user entered)

Now this task was fairly easy, like most of the objects he gave me to do, but i simply cannot figure out how to do the last one on the list (hardest).
It has to go somthing like this
*
**
***
**
*
i tried everything i know and i cant make it like this.
i easily do the part when the number of "*" in a row is growing but i cannot devide a sollution for the rows where number of stars is going down. (in this case last 2 rows)
Thank you for help!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Use 2 for loops for the last one. If you show us what code you have so far, we can give you more help.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Simplistic

Simplistic

    Newbie

  • Members
  • Pip
  • 7 posts
ok so it goes like this:

program a;
var n,j,i:integer;
begin
writeln ('how many rows of * you want?');
readln (n);
for i:=1 to n do
begin 
for j:=1 to n-i do
write (' ');
for i:=1 to i do
write ('*');
writeln;
end;
readln;
end.
with this code i get (if the n=3)
*
**
***
and i lack 2 last rows which will narrow it down to 1 '*' in last row.

Edited by Jaan, 16 November 2008 - 03:48 AM.
Please use code tags when you're posting your codes!


#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The shrinking rows is where a second for block is required (using downto instead of to).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Simplistic

Simplistic

    Newbie

  • Members
  • Pip
  • 7 posts
ok got that, could you code the answer, please. He didnt mention any downto function, therfor he didnt explained it to me.
thank you!

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
There are a couple ways to do it:
1) decrement instead of increment your for loop
2) on the internal loop: loop to n-i instead of to i.

Create the second loop and I'll give you pointers on how to follow up with it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Simplistic

Simplistic

    Newbie

  • Members
  • Pip
  • 7 posts
i know i seem like a complete noob in pascal, which i am, because i didnt have much time to study it that far. but i dont understand how to create second for block that will help me and i dont know whats an increment (lol). could you recomand me a manual or something cause i cant find it by googling either.

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
for i:=1 to 10
i starts at 1, then increments to 2, then increments to 3, etc.

if i is 1 and you want to print 4 stars, you can print 5-i stars
if i is 2 and you want to print 3 stars, you can print 5-i stars
if i is 3 and you want to print 2 stars, you can print 5-i stars
if i is 4 and you want to print 1 stars, you can print 5-i stars
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
jakefrog

jakefrog

    Newbie

  • Members
  • PipPip
  • 11 posts
And the down to funcion is almost the same of regular for

for i:=10 DOWNTO 0 DO


#10
Simplistic

Simplistic

    Newbie

  • Members
  • Pip
  • 7 posts
ok now after a while i got it. it just needed 1 more block, something like 2 separated parts that create 1 object. thank you for advices!