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!
for function (making various objects)
Started by Simplistic, Nov 15 2008 12:05 PM
9 replies to this topic
#1
Posted 15 November 2008 - 12:05 PM
|
|
|
#2
Posted 15 November 2008 - 05:45 PM
Use 2 for loops for the last one. If you show us what code you have so far, we can give you more help.
#3
Posted 16 November 2008 - 03:41 AM
ok so it goes like this:
*
**
***
and i lack 2 last rows which will narrow it down to 1 '*' in last row.
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
Posted 16 November 2008 - 07:15 AM
The shrinking rows is where a second for block is required (using downto instead of to).
#5
Posted 16 November 2008 - 07:33 AM
ok got that, could you code the answer, please. He didnt mention any downto function, therfor he didnt explained it to me.
thank you!
thank you!
#6
Posted 16 November 2008 - 08:32 AM
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.
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.
#7
Posted 16 November 2008 - 12:51 PM
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
Posted 16 November 2008 - 08:06 PM
for i:=1 to 10i 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
#9
Posted 03 December 2008 - 08:32 AM
And the down to funcion is almost the same of regular for
for i:=10 DOWNTO 0 DO
#10
Posted 21 December 2008 - 06:08 AM
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!


Sign In
Create Account

Back to top









