PDA

View Full Version : Joining loops?



stevengreen22
07-03-2011, 05:31 PM
Hi guys,

I need to write a prog that will display 4 different shapes when a menu is selected, I haven't as yet written the menu (please don't help with this - :))

need to draw a normal triangle, an upside down one, a pyramid and a diamond.

the issue I'm having is with the diamond.

I figured I'd have a loop for 1 to 10 do and then follow that up with a 10 downto 1 to create the reverse effect but I'm getting some odd results and all sorts of errors.

Could/would you mind pointing me in the right direction?
thanks.

code so far -


program STARS2TEST(output);
uses crt;
const Title:string = ' STARS ';
By:string = ' By Steve Green ';
var i,j,k,l: integer;


begin {main program}
ClrScr;
textcolor(red);
writeln (Title);
writeln;
writeln (by);
writeln;
textcolor(green);


for i := 1 to 10 do
begin

for j:= 1 to 10-i do //this is normal pyramid
write(' ');

for j:= 1 to i do
write('*');

for j:=1 to i-1 do
write('*');
writeln;
end;


{ for i := 10 downto 1 do
begin

for j:= 1 to 10-i do //this is upside down pyramid
write(' ');

for j:= 1 to i do
write('*');

for j:=1 to i-1 do //these don't work together...
write('*');
writeln;
end; }


writeln;

writeln;
writeln;
textcolor(red);
writeln ('Press <Return> to continue');

readln ();

end.


Thanks in advance.

'Heres to hoping the otehr guys in class don't check this and thieve my code (as weak and poor as it is)' :D Jay....Leave code alone! :)

WILL
07-03-2011, 08:12 PM
Hi Steven, I'm not sure what you are asking help for. Do you want help to draw the diamond or how to nest for loops? In plain language, what is your end goal after it's all written?

Assuming that you are asking for help drawing a diamond shape, I can tell you that I would not be using a for loop at all. That is to say that you are able to draw lines. If drawing only pixels is your problem's limitation then, yes a for loop would be needed, but not if you have other basic graphical shape functions.

I won't get into how I'd draw the diamond just yet, as I don't know what limitations and context you're working under, but if you come back and explain your problem and what rules you are working under then I'd be happy to assist you where I can. :)

stevengreen22
07-03-2011, 10:42 PM
Hi WIll,

Thanks for you time and apologies for not making it clear.

The task is to create program that has a menu. when a user presses 1-4 a different shape is displayed.

*
**
***
****
in this format.

I've used a loop to drap the normal trianlge, reversed it to draw one upside down and used the above code to draw a pyramid. the final task is to draw a diamond.

*
**
***
****
***
**
*
or something to that effect.
I though I could write the code and then write the reverse to make the bottom part of thre triangle but the loops don't work together? One way or the other works but not when combined.

Does that make any more sense?

stevengreen22
07-03-2011, 10:45 PM
The diamond didn't format.

---*---
--***--
-****-
******
-****-
--***--
---*---

or soemthing..

Out of curisoity though, what did you mean when you said you coudl "draw" the shape? Its out of the scope of the assignment but it'd still be great to know and how would you go about it without using loops?

WILL
07-03-2011, 11:57 PM
Oh... you mean you're using ASCII in a console to print out different shapes. I was under the assumption that you were using actual graphics instead. Guess I should have actually looked at your code. :)

In that case, yeah you could use a nested for loop to do what you propose. Nesting is just placing one inside of the other. For example...


var i, j: Integer;

for i := 0 to 10 do
for j := 0 to 10 do
writeln('(' + inttostr(i) + ', ' + inttostr(j) + ')');...would give you a print-out of all the combinations of values the i and j variables go though for the entire run of this program's nested for loops. Try it and see how it works.

You can see that the inside loop completes before the outer one as it runs through it's iterations before the encasing for loop moves onto it's next iteration. So in plain language, each i value will go through all the j values as the for loop using i moves along.

You can have multiple nesting of for and other types of loops, if conditionals and all of the other constructs that make up the Pascal language. You can even get really crazy with this too and code yourself into a messif you aren't keeping track of what you're doing logically. Just be sure that you know what each does and that it is in fact something that you need it to do. It takes practice and time to get to know what you need for each programming problem. Comes with experience so just try different things to see what works best for each case scenario. Often what is the right way and what is the wrong way to program something all depends on what the problem is and ultimately what the program is trying to accomplish.

User137
08-03-2011, 06:55 AM
'Heres to hoping the otehr guys in class don't check this and thieve my code (as weak and poor as it is)' :D Jay....Leave code alone! :)

Sorry but i really feel like using forums for homework is big time cheating. All of your classroom could soon come here to look for right answers and actually learn nothing. You could aswell email the teacher for these tips as it is the exact same thing. Homeworks aren't usually anyway such important that you need to get them right, its enough that you have tried and then learn the rest at the lesson when right answers are gone through. It's different if you'd be asking for guides after the homework have been sorted out and still cannot figure them out.

So.. good luck :)

stevengreen22
08-03-2011, 08:53 AM
Let me run this scenario past you then.
I, like the other guys in class have not done any programming before.
We've so far had 2 presentations, each 15 mintes and neither of which was very enlightening.

Don't think for one minute that I'm simply copying the text and help given cos thats crap. What I'm askign for is explanations and/or ideas on improvements.
for ex, the readln,ch) thing. THAT seems fairly obvious yet not pointed out to me by tutor?! Simple things and simple mistakes like that annoy me.

I've spent a great deal of time poring over websites reading how tos and generally learning more. BUT, it's one thing to read a guide and another entirley to see a workign example.

From what I wrote, you can see that I've done the majority of the task, I simply do not know how to combine the loops, if I did I wouldn't need to ask for assistance / guidance from here.

To clarify a point - each of the homeworks we're set coulnts towards the general result that we get so it's important for me to get it right.

stevengreen22
08-03-2011, 02:15 PM
Hi guys,

All sorted!!!

I was making the mistake of using the same variable for the 2nd loop that was in the first so it was having a fit. :)
I changed the variable and voila! One Diamond!!!!

Thanks for the help!!!!