Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to do a slug? Posted by SHO1 on 2 Oct 2002 at 3:38 AM
I have another question(it's hard to explain but I'll try).
With 2d array I need to do this:

for i:=1 to n do
begin
writeln;
for j:=1 to n do write(' ',a[i,j]);
end;
if the answer fo thet will be 11 22 33 44 55
66 77 88 99 12
13 14 15 16 17
18 19 20 21 22
23 24 25 26 27
Then I need that the answer would look like this:

11 22 33 44 55 12 17 22 27 26 25 24 23 18 13 66 77 88 99 16 21 20 19 14 15

Hope you understood my question





Report
Re: How to do a slug? Posted by SHO1 on 2 Oct 2002 at 8:47 AM
This message was edited by SHO1 at 2002-10-2 8:51:23

Oops havn't wrote the question right what I ment is is:

for i:=1 to n do
begin
writeln;
for j:=1 to n do write(' ',a[i,j]);
end;
{if the output for thet will be that:
11 22 33 44 55
66 77 88 99 12
13 14 15 16 17
18 19 20 21 22
23 24 25 26 27
Then I need another output that would look like this:

11 22 33 44 55 12 17 22 27 26 25 24 23 18 13 66 77 88 99 16 21 20 19 14 15
Thanks for trying to understand my english
Ran}



Report
Re: How to do a slug? Posted by iby on 2 Oct 2002 at 6:51 PM
This message was edited by iby at 2002-10-2 18:53:26

: This message was edited by SHO1 at 2002-10-2 8:51:23

: Oops havn't wrote the question right what I ment is is:
:
: for i:=1 to n do
: begin
: writeln;
: for j:=1 to n do write(' ',a[i,j]);
: end;
: {if the output for thet will be that:
: 11 22 33 44 55
: 66 77 88 99 12
: 13 14 15 16 17
: 18 19 20 21 22
: 23 24 25 26 27
: Then I need another output that would look like this:
:
: 11 22 33 44 55 12 17 22 27 26 25 24 23 18 13 66 77 88 99 16 21 20 19 14 15
: Thanks for trying to understand my english
: Ran}
:
:
:
:



Well there are several ways to do it. None of them is really simple
(or I didn't find one). The simplest way of course is to use
a lookup table. If the array size grows, this is less and less
atractive approach. Here is one fairly simple (or at least
readable) method.

type my_arr=array[1..50, 1..50] of integer;

var i,j,n,limit:integer;
    q:my_arr;


{ put some numbers into array so it's easier to follow the numbers }
procedure fill_array;
begin
  for i:=1 to limit do
    for j:= 1 to limit do
      q[i,j]:=i*10+j;
end;


{ display array in standard 2D layout }
procedure display_array;
begin
  writeln('This is the 2D square matrix size ',limit,' :');
  for i:=1 to limit do
  begin
  writeln;
    for j:=1 to limit do
      if q[i,j]>99 then write(q[i,j],' ')
        else write(q[i,j],'  ');
  end;
  writeln;
  writeln;
end;

procedure display_whirl;
begin
 writeln('This is the ''unwrapped'' array... :');
 n:=0;
 i:=1;
 j:=1;

 while ((i<>(limit+1) div 2) and (j<>(limit+1) div 2)) or
       ((limit=2) and (n<i)) do begin

   repeat
     write(q[i,j],' ');
     inc(j);
   until j>(limit-n); dec(j);

   inc(i);

   repeat
     write(q[i,j],' ');
     inc(i);
   until i>(limit-n); dec(i);

   dec(j);

   repeat
     write(q[i,j],' ');
     dec(j);
   until j=n; inc(n);
   inc(j);
   dec(i);
   if limit=2 then exit;

   repeat
     write(q[i,j],' ');
     dec(i);
   until i=n;

   inc(i);
   inc(j);
   
 end;
 write(q[i,j],' ');
end;


begin
  asm mov ax,3; int 16 end;

   write('What size array would you like to see (1..50)? :');
   readln(limit);
   writeln;
   if limit>50 then begin
     writeln('Size above 50 is not supprted...');
     halt(1);
   end;

   fill_array;
   display_array;
   display_whirl;

   writeln;writeln;
   write('Press Enter to quit...');readln;
end.



Iby



Report
Re: How to do a slug? Posted by SHO1 on 2 Oct 2002 at 8:50 PM
Thank you very much for all your answers
without you i was lost


: This message was edited by iby at 2002-10-2 18:53:26

: : This message was edited by SHO1 at 2002-10-2 8:51:23

: : Oops havn't wrote the question right what I ment is is:
: :
: : for i:=1 to n do
: : begin
: : writeln;
: : for j:=1 to n do write(' ',a[i,j]);
: : end;
: : {if the output for thet will be that:
: : 11 22 33 44 55
: : 66 77 88 99 12
: : 13 14 15 16 17
: : 18 19 20 21 22
: : 23 24 25 26 27
: : Then I need another output that would look like this:
: :
: : 11 22 33 44 55 12 17 22 27 26 25 24 23 18 13 66 77 88 99 16 21 20 19 14 15
: : Thanks for trying to understand my english
: : Ran}
: :
: :
: :
: :
:
:

:
: Well there are several ways to do it. None of them is really simple
: (or I didn't find one). The simplest way of course is to use
: a lookup table. If the array size grows, this is less and less
: atractive approach. Here is one fairly simple (or at least
: readable) method.
:
:
: type my_arr=array[1..50, 1..50] of integer;
: 
: var i,j,n,limit:integer;
:     q:my_arr;
: 
: 
: { put some numbers into array so it's easier to follow the numbers }
: procedure fill_array;
: begin
:   for i:=1 to limit do
:     for j:= 1 to limit do
:       q[i,j]:=i*10+j;
: end;
: 
: 
: { display array in standard 2D layout }
: procedure display_array;
: begin
:   writeln('This is the 2D square matrix size ',limit,' :');
:   for i:=1 to limit do
:   begin
:   writeln;
:     for j:=1 to limit do
:       if q[i,j]>99 then write(q[i,j],' ')
:         else write(q[i,j],'  ');
:   end;
:   writeln;
:   writeln;
: end;
: 
: procedure display_whirl;
: begin
:  writeln('This is the ''unwrapped'' array... :');
:  n:=0;
:  i:=1;
:  j:=1;
: 
:  while ((i<>(limit+1) div 2) and (j<>(limit+1) div 2)) or
:        ((limit=2) and (n<i)) do begin
: 
:    repeat
:      write(q[i,j],' ');
:      inc(j);
:    until j>(limit-n); dec(j);
: 
:    inc(i);
: 
:    repeat
:      write(q[i,j],' ');
:      inc(i);
:    until i>(limit-n); dec(i);
: 
:    dec(j);
: 
:    repeat
:      write(q[i,j],' ');
:      dec(j);
:    until j=n; inc(n);
:    inc(j);
:    dec(i);
:    if limit=2 then exit;
: 
:    repeat
:      write(q[i,j],' ');
:      dec(i);
:    until i=n;
: 
:    inc(i);
:    inc(j);
:    
:  end;
:  write(q[i,j],' ');
: end;
: 
: 
: begin
:   asm mov ax,3; int 16 end;
: 
:    write('What size array would you like to see (1..50)? :');
:    readln(limit);
:    writeln;
:    if limit>50 then begin
:      writeln('Size above 50 is not supprted...');
:      halt(1);
:    end;
: 
:    fill_array;
:    display_array;
:    display_whirl;
: 
:    writeln;writeln;
:    write('Press Enter to quit...');readln;
: end.
: 

:
:
: Iby
:
:
:
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.