ค้นหาบล็อกนี้

2/05/2554

The For Loop – Extra Exercise

The For Loop - Extra Exercise
--
*The For Loop - Extra Exercise

If you manage to finish the exercises early, you may wish to try your
hand at the following:-

Fill the screen with Xs in a spiral

The object of this exercise is to start with a blank screen, and draw
Xs across the top line of the screen, and then from the top-right hand
corner, down to the bottom right, and from the bottom right leftwards
to the bottom-left, and from the bottom-left, up to the square JUST
BELOW the top left hand corner. Then repeat this until the screen
fills up with Xs. A scaled-down illustration of this process is given
below:-


The screen width is 80 columns (1 for the left-most column, 80 for the
right-most), screen height is 24 lines (1 for the top line, 24 for the
bottom).
To draw Xs from column 1 to column 80 on line 1, we would use the
following code:-

for colNum = 1 to 80 do begin gotoxy(colNum,1); write('X') end;

To draw Xs from row 2 to row 24 in column 80:-

for rowNum = 2 to 24 do begin gotoxy(80,rowNum); write('X') end;

To draw Xs from column 79 to column 1 on line 24:-

for colNum = 79 downto 1 do begin gotoxy(colNum,24); write('X') end;

and to draw Xs from row 23 to row 2 in column 1:-

for rowNum = 23 downto 2 do begin gotoxy(1,rowNum); write('X') end;

So next time, we will be drawing from column 2 to column 79 on row 2,
then from row 3 to row 23 in column 79, from column 78 to column 2 on
row 23, and finally from row 22 to row 3 in column 2

Thus, we will need four loops (one after another) to go across, down,
left, and up again, and a loop enclosing all of these loops, which
controls where to start and stop in each row/column each time.

For example, if the outer loop is called loops and starts at 1, then
the first of the four loops might now read:-

for colNum = loops to 81-loops do begin gotoxy(colNum,1); write('X') end;

Logically, loops will go from 1 to half the screen size (12).

See if you can work out the rest...

If the drawing seems to go too fast, then put a delay after each
write('X') - e.g. delay(20) to pause 1/50th of a second, so that 50
Xs get written per second.

... and if you have time, see if you can work out how to work backwards
afterwards, and write a space character from the centre of the screen,
spiralling out to the top of the screen until the screen is cleared.

... Still got time on your hands? Put another loop around this entire
process, so that the whole drawing/undrawing of the spiral is repeated
so that the resulting rectangular box of Xs gets progressively
smaller!

Good luck!

Solution 1 - Draw Spirals from outside edge of screen to centre

program DrawSpiral;

const maxCols = 70;
maxRows = 24;
delayValue = 5;

var
colNum,rowNum,loops:integer;

{ Ignore this if using DOS Pascal }
procedure delay(i:integer);
var j:integer;
k:real;
begin
for j:=1 to i*20 do k:=sqrt(j);
end;

begin

{ increase offset so that draws progressively smaller
rectangles }
for loops := 1 to 12 do
begin

{ draw horizontally from left to right }
for colNum := loops to maxCols-loops+1 do
begin
gotoxy(colNum,loops); write('X'); delay(delayValue);
end;

{ draw vertically from top to bottom }
for rowNum := loops+1 to maxRows-loops+1 do
begin
gotoxy(maxCols-loops+1,rowNum); write('X');
delay(delayValue);
end;

{ draw horizontally from right to left }
for colNum := maxCols-loops downto loops do
begin
gotoxy(colNum,maxRows-loops+1); write('X');
delay(delayValue);
end;

{ draw vertically from bottom to top }
for rowNum := maxRows-loops downto loops+1 do
begin
gotoxy(loops,rowNum); write('X'); delay(delayValue);
end;

end;

end.
Solution 2 - Draw Spirals and then remove them again from centre outwards

program DrawSpiral;

uses WinCrt;

const maxCols = 70; maxRows = 24; delayValue = 5;

var
colNum,rowNum,loops:integer;

{ Ignore this if using DOS Pascal }
procedure delay(i:integer);
var j:integer; k:real;
begin for j:=1 to i*20 do k:=sqrt(j) end;

begin

{ increase offset so that draws progressively smaller rectangles }
for loops := 1 to 12 do
begin

{ draw horizontally from left to right }
for colNum := loops to maxCols-loops+1 do
begin
gotoxy(colNum,loops); write('X'); delay(delayValue);
end;

{ draw vertically from top to bottom }
for rowNum := loops+1 to maxRows-loops+1 do
begin
gotoxy(maxCols-loops+1,rowNum); write('X'); delay(delayValue);
end;

{ draw horizontally from right to left }
for colNum := maxCols-loops downto loops do
begin
gotoxy(colNum,maxRows-loops+1); write('X'); delay(delayValue);
end;

{ draw vertically from bottom to top }
for rowNum := maxRows-loops downto loops+1 do
begin
gotoxy(loops,rowNum); write('X'); delay(delayValue);
end;

end;

{ decrease offset so that draws progressively larger rectangles }
for loops := 12 downto 1 do
begin

{ draw vertically from top to bottom }
for rowNum := loops+1 to maxRows-loops do
begin
gotoxy(loops,rowNum); write(' '); delay(delayValue);
end;

{ draw horizontally from left to right }
for colNum := loops to maxCols-loops do
begin
gotoxy(colNum,maxRows-loops+1); write(' '); delay(delayValue);
end;

{ draw vertically from bottom to top }
for rowNum := maxRows-loops+1 downto loops+1 do
begin
gotoxy(maxCols-loops+1,rowNum); write(' '); delay(delayValue);
end;

{ draw horizontally from right to left }
for colNum := maxCols-loops+1 downto loops do
begin
gotoxy(colNum,loops); write(' '); delay(delayValue);
end;

end;

end.
Solution 3: Draw and undraw spirals in decreasing and increasing sizes.

program DrawSpiral;

uses WinCrt;

const maxCols = 70;
maxRows = 24;
delayValue = 5;

var
colNum,rowNum,loops,rectSize:integer;

{ Ignore this if using DOS Pascal }
procedure delay(i:integer);
var j:integer;
k:real;
begin
for j:=1 to i*20 do k:=sqrt(j);
end;

begin

for rectSize := 1 to 12 do
begin

{ increase offset so that draws progressively smaller rectangles }
for loops := rectSize to 12 do
begin

{ draw horizontally from left to right }
for colNum := loops to maxCols-loops+1 do
begin
gotoxy(colNum,loops); write('X'); delay(delayValue);
end;

{ draw vertically from top to bottom }
for rowNum := loops+1 to maxRows-loops+1 do
begin
gotoxy(maxCols-loops+1,rowNum); write('X'); delay(delayValue);
end;

{ draw horizontally from right to left }
for colNum := maxCols-loops downto loops do
begin
gotoxy(colNum,maxRows-loops+1); write('X'); delay(delayValue);
end;

{ draw vertically from bottom to top }
for rowNum := maxRows-loops downto loops+1 do
begin
gotoxy(loops,rowNum); write('X'); delay(delayValue);
end;

end;


{ decrease offset so that draws progressively larger rectangles }
for loops := rectSize downto 1 do
begin

{ draw vertically from top to bottom }
for rowNum := loops+1 to maxRows-loops do
begin
gotoxy(loops,rowNum); write(' '); delay(delayValue);
end;

{ draw horizontally from left to right }
for colNum := loops to maxCols-loops do
begin
gotoxy(colNum,maxRows-loops+1); write(' '); delay(delayValue);
end;

{ draw vertically from bottom to top }
for rowNum := maxRows-loops+1 downto loops+1 do
begin
gotoxy(maxCols-loops+1,rowNum); write(' '); delay(delayValue);
end;

{ draw horizontally from right to left }
for colNum := maxCols-loops+1 downto loops do
begin
gotoxy(colNum,loops); write(' '); delay(delayValue);
end;

end;

end;

end.

ข้อสอบครูชำนาญการพิเศษ *
ข้อสอบครูผู้ช่วย,สอบบรรจุครูผู้ช่วย,แนวข้อสอบครูผู้ช่วย,สอบครูผู้ช่วย,แนวข้อสอบครูผู้ช่วย,ครูผู้ช่วย,ข้อสอบบรรจุครู,ข้อสอบครูชำนาญการพิเศษ
*http://www.oopps.bloggang.com*
*
* ฟิสิกส์ ข้อสอบฟิสิกส์ บทเรียนฟิสิกส์ ฟิสิกส์ออนไลน์
โจทย์ฟิสิกส์ แบบฝึกหัดวิชาฟิสิกส์ โจทย์วิชาฟิสิกส์ โจทย์วิชาฟิสิกส์
http://thaiphysics.blogspot.com


* บทเรียนบทรัก ความรัก บทความความรัก ดูดวงความรัก นิยามความรัก
กลอนรัก เพลงรักhttp://love8love9.blogspot.com


* ความรัก บทความความรัก ดูดวงความรัก นิยามความรัก กลอนรัก
เพลงรักhttp://kongkoymusic.blogspot.com

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

Comments.