Board index » delphi » 2D array

2D array

Hi all

I stuck
I am trying to sort a 2D array by the amount of 'time purchased', the other
part of the array is a 'phone number'
The array should then be displayed, highest time purchased first.
I have done a sort which does sort the time purchased. but it does not keep
the phone numbers with the correct
time purchased. (does that make since!!!!)

I have included my source code (don't laugh programming is not my strong
point!!!!!)

Anyone have any suggestions??

Many Thanks
Dave
____________________________________________________________________________
____
program telephone;
uses  crt;
{
     Name  :
     Date  :  6/12/01
     File  :  Telephone 2002

Quote
}

type
    phonebook       = array [1..20, 1..20] of longint;
var
   phone           : phonebook;
   count           : integer;
   choice          : char;
   coin            : longint;
   money           : longint;
   calllength      : longint;
   time            : longint;
   number          : longint;
   exitprog        : string;
   i               : integer;
   mins            : integer;
   secs            : integer;

        procedure welcombe;
        begin
             clrscr;
             gotoXY(35,6);
             writeln('WELCOME');
             gotoXY(35,7);
             writeln('=======');
             gotoXY(32,12);
             writeln('TELEPHONE 2002');
             gotoXY(23,20);
             write('Programmer : David Cox - 12345678');
             gotoXY(28,23);
             write('HIT "RETURN" TO CONTINUE');
             readln;
        end;

       procedure menu;
       begin
             repeat
             clrscr;
             gotoXY(35,6);
             writeln('SERVICE RATES');
             gotoXY(20,12);
             writeln('A : Local call         (1p for 30 secs)');
             gotoXY(20,13);
             writeln('B : Short Distance     (1p for 20 secs)');
             gotoXY(20,14);
             writeln('C : Lond Distance call (1p for 10 secs)');
             gotoXY(10,20);
             write('Please make your choice form the list : ');
             readln(choice);
             choice := upcase(choice);
                    case choice of
                         'A' : calllength := 30;
                         'B' : calllength := 20;
                         'C' : calllength := 10;
                    end;

             until (choice='A') or (choice='B') or (choice='C');
       end;

       procedure entermoney;
       begin
            money := 0;

            repeat
                  clrscr;
                  gotoXY(5,20);
                  writeln('Total amount entered : ',money);
                  gotoXY(35,6);
                  writeln('GIVE ME MONEY');
                  gotoXY(12,10);
                  writeln('Please enter one coin at a time, followed by
"ENTER"');
                  gotoXY(25,11);
                  writeln('When finished enter a 0 (zero)');
                  gotoXY(32,13);
                  write('Coin entered : ');
                  readln(coin);

                  if (coin<>5) and (coin<>10) and (coin<>20) and (coin<>50)
and (coin<>100) and (coin<>0) then
                  begin
                       clrscr;
                       gotoXY(10,10);
                       writeln('     COIN REJECTED. 5,10,20,50,100 coins
only please');
                       readln;
                       gotoXY(10,10);
                       writeln('
');
                  end
                  else
                  begin
                       money := money + coin;
                       gotoXY(5,20);
                       writeln('Total amount entered : ',money);
                  end;

            until (coin = 0);
             clrscr;
             gotoXY(35,4);
             writeln('SERVICE RATES');
             gotoXY(20,10);
             writeln('A : Local call         (1p for 30 secs)');
             gotoXY(20,11);
             writeln('B : Short Distance     (1p for 20 secs)');
             gotoXY(20,12);
             writeln('C : Lond Distance call (1p for 10 secs)');
             gotoXY(10,18);
             write('You have chosen service rate : ',choice);
             time := money * calllength;
             mins := (money * calllength) div 60;
             secs := (money * calllength) mod 60;
             gotoXY(10,19);
             write('Your have a total time of    : ',mins,' minutes ',secs,'
seconds');
             readln;
       end;

       procedure dial;
       begin

             clrscr;
             gotoXY(10,10);
             write('Please enter the number to be dialed (no spaces) : ');
             readln(number);
             phone[count,1] := number;
             phone[count,2] := time;
             clrscr;
             gotoXY(25,8);
             writeln(number);
             gotoXY(20,12);
             writeln('Dialing..............');
             gotoXY(20,14);
             write('Connected..');
             readln;
             end;

       procedure final;
       begin
            clrscr;
            gotoXY(12,5);
            write('Phone number dialed         Time Purchased');
            gotoXY(12,6);
            write('===================         ==============');
            writeln;

            for i := 1 to count do
            begin
                  gotoXY(10,(i+7));
                  writeln(i,'    ',phone[i,1]);
                  gotoXY(45,(i+7));
                  write(phone[i,2]);
            end;
            readln;
       end;

       procedure sort;

       var
          n,index : integer;
          sorted  : boolean;

          procedure SWAP (var Val1, Val2:integer);
          var
             temp : integer;
             begin
                  temp := Val1;
                  Val1 := Val2;
                  Val2 := temp;
          end;

          begin n:=count;
          repeat
                sorted := true;
                n := n-1;
                for index := 1 to n do
                begin
                     if phone[index,2] < phone[index+1,2] then
                     begin
                     sorted := false;
                     SWAP(phone[index,2], phone[index+1,2]);
                     end;
                end;
          until sorted;
            clrscr;
            gotoXY(12,5);
            write('Phone number dialed         Time Purchased');
            gotoXY(12,6);
            write('===================         ==============');
            writeln;

            for i := 1 to count do
            begin
                  gotoXY(10,(i+7));
                  writeln(i,'    ',phone[i,1]);
                  gotoXY(45,(i+7));
                  write(phone[i,2]);
            end;
            readln;

       end;

begin   {}
           count :=1;
        repeat

              welcombe;
              menu; {call menu procedure}
              entermoney; {call money entry procedure}
              dial;

              gotoXY(26,20);
              write('Anthor number Y / N : ');
              readln(exitprog);
              exitprog := upcase(exitprog);

              if (exitprog = 'Y') then
              begin
                   count := count+1
              end;

        until (exitprog = 'N');
               final;
               sort;
end.

--
===============================
Live Long & Prosper :-)
===============================

 

Re:2D array


Without looking at your code: every time you swap purchase dates, you must swap the according phone numbers as well!

Greetings

Markus

Re:2D array


Quote
"Dave Cox" <d...@cox146.freeserve.co.uk> wrote:

> Hi all

> I stuck
> I am trying to sort a 2D array by the amount of 'time purchased', the other
> part of the array is a 'phone number'
> The array should then be displayed, highest time purchased first.
> I have done a sort which does sort the time purchased. but it does not keep
> the phone numbers with the correct
> time purchased. (does that make since!!!!)

> I have included my source code (don't laugh programming is not my strong
> point!!!!!)

> Anyone have any suggestions??

> Many Thanks
> Dave
> ____________________________________________________________________________
> ____
> program telephone;
> uses  crt;
> {
>      Name  :
>      Date  :  6/12/01
>      File  :  Telephone 2002
> }
> type
>     phonebook       = array [1..20, 1..20] of longint;
> var
>    phone           : phonebook;

[snip a lot of code]

- Show quoted text -

Quote
>           procedure SWAP (var Val1, Val2:integer);
>           var
>              temp : integer;
>              begin
>                   temp := Val1;
>                   Val1 := Val2;
>                   Val2 := temp;
>           end;

>           begin n:=count;
>           repeat
>                 sorted := true;
>                 n := n-1;
>                 for index := 1 to n do
>                 begin
>                      if phone[index,2] < phone[index+1,2] then
>                      begin
>                      sorted := false;
>                      SWAP(phone[index,2], phone[index+1,2]);

You are only SWAP()ing the second field in 'phone'. What about the other 19?
They have to be swapped too.

Furthermore you have another problem. 'phone' is a defined as an array of
'longint' and you swap 'integer'. 'integer' and 'longint' are not always equal.
Try compiling this very short program:

    begin
      WriteLn('SizeOf(Integer) = ', SizeOf(Integer));
      WriteLn('SizeOf(LongInt) = ', SizeOf(LongInt));
    end.

If the result (as I suspect) is two different values, you have to change the
SWAP() function too.

But then why do you use a two dimensional array in the first place? I'ld have
defined phonebook as:

    type
      phonebook_entry = record
                          time_purchased,
                          phone_number : longint;
                        end;
      phonebook       = array [1..20] of phonebook_entry;

This way you can use phone[10].time_purchased instead of phone[10,1], and when
you sort you only have to swap two phonebook_entry's.

Regards, Ren
--
Using Virtual Access
http://www.vamail.com

Re:2D array


JRS:  In article <3C471C98.3000...@freenet.de>, seen in
news:comp.lang.pascal.borland, Markus Humm <markus.h...@freenet.de>
wrote at Thu, 17 Jan 2002 19:48:56 :-

Quote
>Without looking at your code: every time you swap purchase dates, you must swap
>the according phone numbers as well!

Yes and no.  Indeed, that would be needed as part of fixing it.  But, if
the process is to be efficient (that may not be necessary initially, but
it is important in principle), one should in sorting almost never swap
the data.  Instead, use an index to the data, and swap that.

Looking at the code :

type
    phonebook       = array [1..20, 1..20] of longint;

but phone is only being used 1..2;  The second index is used non-
arithmetically; it would be clearer as an Enumerated.

SWAP works on integer variables; unless the phone numbers are below
32768, a run-time error should be given with proper numbers.  I guess
run-time error-checking is turned off, an elementary error.  OP: read
the mFAQ, read the FAQ, read my site.

The maximum number which a longint will store is 2,147,483,648; in
equivalent format, my phone number is 02,0ab,cde,fgh which would fit.
But if I lived in Ballycastle, my number would be, in that form,
02,820,7de,fgh, which would not fit.  Telephone numbers should be stored
as strings.

Consider something like
type
    TPhoneEntry = record Number : string [28] ; Time : word end ;
    Tphonebook = array [1..20] of PhoneEntry ;
    TIndex = array [1..20] of 1..20  ;
var
    PhoneEntry : TPhoneEntry ;
    phonebook : Tphonebook ;
    Index : TIndex ;

Access the data indirectly as PhoneEntry[Index[J]], and then to swap
entries just swap the values in Index.

For
            until (choice='A') or (choice='B') or (choice='C');

            until choice in ['A'..'C'] ;

is nicer.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

Re:2D array


hi dave ?

"Dave Cox" <d...@cox146.freeserve.co.uk> schrieb im Newsbeitrag
news:a26v2s$lna$1@newsg2.svr.pol.co.uk...

Quote
> Hi all

> I stuck
> I am trying to sort a 2D array by the amount of 'time purchased', the
other
> part of the array is a 'phone number'

i might be wrong, but only having a brief look at your code ...

do your realy need a 2d array of 20 x 20 ?

not only a list ???

or lets say a list of (record) entrys   [number (type string); money :
(type real)]?

those are very different struvtures and as i did not understand your
coding (i must say, i didn't try very hard) ... this was my first guess
..

regards
jochen

Re:2D array


If you're doing the sort manually, when you change positions of the
purchased cell, you also have to change the phone number cell at the same
time.  On second itteration, if you're going to sort the phone numbers too,
only sort between the ranges that the dates are exactly the same.

Quote
"Dave Cox" <d...@cox146.freeserve.co.uk> wrote in message

news:a26v2s$lna$1@newsg2.svr.pol.co.uk...
Quote
> Hi all

> I stuck
> I am trying to sort a 2D array by the amount of 'time purchased', the
other
> part of the array is a 'phone number'
> The array should then be displayed, highest time purchased first.
> I have done a sort which does sort the time purchased. but it does not
keep
> the phone numbers with the correct
> time purchased. (does that make since!!!!)

> I have included my source code (don't laugh programming is not my strong
> point!!!!!)

> Anyone have any suggestions??

> Many Thanks
> Dave

____________________________________________________________________________

- Show quoted text -

Quote
> ____
> program telephone;
> uses  crt;
> {
>      Name  :
>      Date  :  6/12/01
>      File  :  Telephone 2002
> }
> type
>     phonebook       = array [1..20, 1..20] of longint;
> var
>    phone           : phonebook;
>    count           : integer;
>    choice          : char;
>    coin            : longint;
>    money           : longint;
>    calllength      : longint;
>    time            : longint;
>    number          : longint;
>    exitprog        : string;
>    i               : integer;
>    mins            : integer;
>    secs            : integer;

>         procedure welcombe;
>         begin
>              clrscr;
>              gotoXY(35,6);
>              writeln('WELCOME');
>              gotoXY(35,7);
>              writeln('=======');
>              gotoXY(32,12);
>              writeln('TELEPHONE 2002');
>              gotoXY(23,20);
>              write('Programmer : David Cox - 12345678');
>              gotoXY(28,23);
>              write('HIT "RETURN" TO CONTINUE');
>              readln;
>         end;

>        procedure menu;
>        begin
>              repeat
>              clrscr;
>              gotoXY(35,6);
>              writeln('SERVICE RATES');
>              gotoXY(20,12);
>              writeln('A : Local call         (1p for 30 secs)');
>              gotoXY(20,13);
>              writeln('B : Short Distance     (1p for 20 secs)');
>              gotoXY(20,14);
>              writeln('C : Lond Distance call (1p for 10 secs)');
>              gotoXY(10,20);
>              write('Please make your choice form the list : ');
>              readln(choice);
>              choice := upcase(choice);
>                     case choice of
>                          'A' : calllength := 30;
>                          'B' : calllength := 20;
>                          'C' : calllength := 10;
>                     end;

>              until (choice='A') or (choice='B') or (choice='C');
>        end;

>        procedure entermoney;
>        begin
>             money := 0;

>             repeat
>                   clrscr;
>                   gotoXY(5,20);
>                   writeln('Total amount entered : ',money);
>                   gotoXY(35,6);
>                   writeln('GIVE ME MONEY');
>                   gotoXY(12,10);
>                   writeln('Please enter one coin at a time, followed by
> "ENTER"');
>                   gotoXY(25,11);
>                   writeln('When finished enter a 0 (zero)');
>                   gotoXY(32,13);
>                   write('Coin entered : ');
>                   readln(coin);

>                   if (coin<>5) and (coin<>10) and (coin<>20) and
(coin<>50)
> and (coin<>100) and (coin<>0) then
>                   begin
>                        clrscr;
>                        gotoXY(10,10);
>                        writeln('     COIN REJECTED. 5,10,20,50,100 coins
> only please');
>                        readln;
>                        gotoXY(10,10);
>                        writeln('
> ');
>                   end
>                   else
>                   begin
>                        money := money + coin;
>                        gotoXY(5,20);
>                        writeln('Total amount entered : ',money);
>                   end;

>             until (coin = 0);
>              clrscr;
>              gotoXY(35,4);
>              writeln('SERVICE RATES');
>              gotoXY(20,10);
>              writeln('A : Local call         (1p for 30 secs)');
>              gotoXY(20,11);
>              writeln('B : Short Distance     (1p for 20 secs)');
>              gotoXY(20,12);
>              writeln('C : Lond Distance call (1p for 10 secs)');
>              gotoXY(10,18);
>              write('You have chosen service rate : ',choice);
>              time := money * calllength;
>              mins := (money * calllength) div 60;
>              secs := (money * calllength) mod 60;
>              gotoXY(10,19);
>              write('Your have a total time of    : ',mins,' minutes
',secs,'
> seconds');
>              readln;
>        end;

>        procedure dial;
>        begin

>              clrscr;
>              gotoXY(10,10);
>              write('Please enter the number to be dialed (no spaces) : ');
>              readln(number);
>              phone[count,1] := number;
>              phone[count,2] := time;
>              clrscr;
>              gotoXY(25,8);
>              writeln(number);
>              gotoXY(20,12);
>              writeln('Dialing..............');
>              gotoXY(20,14);
>              write('Connected..');
>              readln;
>              end;

>        procedure final;
>        begin
>             clrscr;
>             gotoXY(12,5);
>             write('Phone number dialed         Time Purchased');
>             gotoXY(12,6);
>             write('===================         ==============');
>             writeln;

>             for i := 1 to count do
>             begin
>                   gotoXY(10,(i+7));
>                   writeln(i,'    ',phone[i,1]);
>                   gotoXY(45,(i+7));
>                   write(phone[i,2]);
>             end;
>             readln;
>        end;

>        procedure sort;

>        var
>           n,index : integer;
>           sorted  : boolean;

>           procedure SWAP (var Val1, Val2:integer);
>           var
>              temp : integer;
>              begin
>                   temp := Val1;
>                   Val1 := Val2;
>                   Val2 := temp;
>           end;

>           begin n:=count;
>           repeat
>                 sorted := true;
>                 n := n-1;
>                 for index := 1 to n do
>                 begin
>                      if phone[index,2] < phone[index+1,2] then
>                      begin
>                      sorted := false;
>                      SWAP(phone[index,2], phone[index+1,2]);
>                      end;
>                 end;
>           until sorted;
>             clrscr;
>             gotoXY(12,5);
>             write('Phone number dialed         Time Purchased');
>             gotoXY(12,6);
>             write('===================         ==============');
>             writeln;

>             for i := 1 to count do
>             begin
>                   gotoXY(10,(i+7));
>                   writeln(i,'    ',phone[i,1]);
>                   gotoXY(45,(i+7));
>                   write(phone[i,2]);
>             end;
>             readln;

>        end;

Re:2D array


Here is where subtypes come in handy.

TYPE
   PhoneNumberType = ARRAY [1 .. 20] OF longint;
   PhoneBookType = ARRAY [1 .. 20] OF PhoneNumberType;
VAR
   PhoneBook : PhoneBookType;

This reduces what appears to be a 2D array (square, even, so you can't tell the
first from the second index) into an array-of-arrays.  You clearly want to order
the elements of the phone book (each of which is a phone number) -- your
comparison routine would examine phone numbers, and your outer (calling) routine
would manipulate the elements of the phone book.

If you want to keep the explicit 2D structure, you could do something like this:

PhoneNumberIndex = 1 .. 20;
PhoneEntryIndex = 1 .. 20;
PhoneBookType = ARRAY [PhoneEntryType, PhoneNumberType] OF longint;

[You can also code it as [PhoneNumberType, PhoneEntryType] ...]

Again, keeping the types named and separate will allow a good Pascal compiler to
help you prevent silly coding errors (like swapping phone numbers instead of
phone entries).

Bob Schor
Pascal Enthusiast

P.S. -- if I offend anyone by "pushing style", I apologize in advance.  What I
love about Pascal is that it allows you to have a style, and one that (in my
experience) enforces good programming habits by helping to make code
"self-documenting".

Quote
Dave Cox wrote:
> Hi all

> I stuck
> I am trying to sort a 2D array by the amount of 'time purchased', the other
> part of the array is a 'phone number'
> The array should then be displayed, highest time purchased first.
> I have done a sort which does sort the time purchased. but it does not keep
> the phone numbers with the correct
> time purchased. (does that make since!!!!)

> I have included my source code (don't laugh programming is not my strong
> point!!!!!)

> Anyone have any suggestions??

> Many Thanks
> Dave
> ____________________________________________________________________________
> ____
> program telephone;
> uses  crt;
> {
>      Name  :
>      Date  :  6/12/01
>      File  :  Telephone 2002
> }
> type
>     phonebook       = array [1..20, 1..20] of longint;
> var
>    phone           : phonebook;
>    count           : integer;
>    choice          : char;
>    coin            : longint;
>    money           : longint;
>    calllength      : longint;
>    time            : longint;
>    number          : longint;
>    exitprog        : string;
>    i               : integer;
>    mins            : integer;
>    secs            : integer;

>         procedure welcombe;
>         begin
>              clrscr;
>              gotoXY(35,6);
>              writeln('WELCOME');
>              gotoXY(35,7);
>              writeln('=======');
>              gotoXY(32,12);
>              writeln('TELEPHONE 2002');
>              gotoXY(23,20);
>              write('Programmer : David Cox - 12345678');
>              gotoXY(28,23);
>              write('HIT "RETURN" TO CONTINUE');
>              readln;
>         end;

>        procedure menu;
>        begin
>              repeat
>              clrscr;
>              gotoXY(35,6);
>              writeln('SERVICE RATES');
>              gotoXY(20,12);
>              writeln('A : Local call         (1p for 30 secs)');
>              gotoXY(20,13);
>              writeln('B : Short Distance     (1p for 20 secs)');
>              gotoXY(20,14);
>              writeln('C : Lond Distance call (1p for 10 secs)');
>              gotoXY(10,20);
>              write('Please make your choice form the list : ');
>              readln(choice);
>              choice := upcase(choice);
>                     case choice of
>                          'A' : calllength := 30;
>                          'B' : calllength := 20;
>                          'C' : calllength := 10;
>                     end;

>              until (choice='A') or (choice='B') or (choice='C');
>        end;

>        procedure entermoney;
>        begin
>             money := 0;

>             repeat
>                   clrscr;
>                   gotoXY(5,20);
>                   writeln('Total amount entered : ',money);
>                   gotoXY(35,6);
>                   writeln('GIVE ME MONEY');
>                   gotoXY(12,10);
>                   writeln('Please enter one coin at a time, followed by
> "ENTER"');
>                   gotoXY(25,11);
>                   writeln('When finished enter a 0 (zero)');
>                   gotoXY(32,13);
>                   write('Coin entered : ');
>                   readln(coin);

>                   if (coin<>5) and (coin<>10) and (coin<>20) and (coin<>50)
> and (coin<>100) and (coin<>0) then
>                   begin
>                        clrscr;
>                        gotoXY(10,10);
>                        writeln('     COIN REJECTED. 5,10,20,50,100 coins
> only please');
>                        readln;
>                        gotoXY(10,10);
>                        writeln('
> ');
>                   end
>                   else
>                   begin
>                        money := money + coin;
>                        gotoXY(5,20);
>                        writeln('Total amount entered : ',money);
>                   end;

>             until (coin = 0);
>              clrscr;
>              gotoXY(35,4);
>              writeln('SERVICE RATES');
>              gotoXY(20,10);
>              writeln('A : Local call         (1p for 30 secs)');
>              gotoXY(20,11);
>              writeln('B : Short Distance     (1p for 20 secs)');
>              gotoXY(20,12);
>              writeln('C : Lond Distance call (1p for 10 secs)');
>              gotoXY(10,18);
>              write('You have chosen service rate : ',choice);
>              time := money * calllength;
>              mins := (money * calllength) div 60;
>              secs := (money * calllength) mod 60;
>              gotoXY(10,19);
>              write('Your have a total time of    : ',mins,' minutes ',secs,'
> seconds');
>              readln;
>        end;

>        procedure dial;
>        begin

>              clrscr;
>              gotoXY(10,10);
>              write('Please enter the number to be dialed (no spaces) : ');
>              readln(number);
>              phone[count,1] := number;
>              phone[count,2] := time;
>              clrscr;
>              gotoXY(25,8);
>              writeln(number);
>              gotoXY(20,12);
>              writeln('Dialing..............');
>              gotoXY(20,14);
>              write('Connected..');
>              readln;
>              end;

>        procedure final;
>        begin
>             clrscr;
>             gotoXY(12,5);
>             write('Phone number dialed         Time Purchased');
>             gotoXY(12,6);
>             write('===================         ==============');
>             writeln;

>             for i := 1 to count do
>             begin
>                   gotoXY(10,(i+7));
>                   writeln(i,'    ',phone[i,1]);
>                   gotoXY(45,(i+7));
>                   write(phone[i,2]);
>             end;
>             readln;
>        end;

>        procedure sort;

>        var
>           n,index : integer;
>           sorted  : boolean;

>           procedure SWAP (var Val1, Val2:integer);
>           var
>              temp : integer;
>              begin
>                   temp := Val1;
>                   Val1 := Val2;
>                   Val2 := temp;
>           end;

>           begin n:=count;
>           repeat
>                 sorted := true;
>                 n := n-1;
>                 for index := 1 to n do
>                 begin
>                      if phone[index,2] < phone[index+1,2] then
>                      begin
>                      sorted := false;
>                      SWAP(phone[index,2], phone[index+1,2]);
>                      end;
>                 end;
>           until sorted;
>             clrscr;
>             gotoXY(12,5);
>             write('Phone number dialed         Time Purchased');
>             gotoXY(12,6);
>             write('===================         ==============');
>             writeln;

>             for i := 1 to count do
>             begin
>                   gotoXY(10,(i+7));
>                   writeln(i,'    ',phone[i,1]);
>                   gotoXY(45,(i+7));
>                   write(phone[i,2]);
>             end;
>             readln;

>        end;

> begin   {}
>            count :=1;
>         repeat

>               welcombe;
>               menu; {call menu procedure}
>               entermoney; {call money entry procedure}
>               dial;

>               gotoXY(26,20);
>               write('Anthor number Y / N : ');
>               readln(exitprog);
>               exitprog := upcase(exitprog);

>               if (exitprog = 'Y') then
>               begin
>                    count := count+1
>               end;

>         until (exitprog = 'N');
>                final;
>                sort;
> end.

> --
> ===============================
> Live Long & Prosper :-)
> ===============================

Re:2D array


On Fri, 18 Jan 2002 16:35:51 -0000, "Dave Cox"

Quote
<d...@cox146.freeserve.co.uk> wrote:
>Hi all

>I stuck
>I am trying to sort a 2D array by the amount of 'time purchased', the other
>part of the array is a 'phone number'
>The array should then be displayed, highest time purchased first.
>I have done a sort which does sort the time purchased. but it does not keep
>the phone numbers with the correct
>time purchased. (does that make since!!!!)

>I have included my source code (don't laugh programming is not my strong
>point!!!!!)

>Anyone have any suggestions??

>Many Thanks
>Dave

--------------- snip ------------------------

Program SortPhoneRecords;
{Turbo v6.0   In a 2D array the variable type must be the same for
each array element. Since phone numbers sometimes use "cute" words
to make it easy to remember their numbers, char strings are used
here for the phone numbers and integers are used for the paid seconds.
Records are made containing these fields and another field that
converts any phone alphabetic chars to the proper numeric char.
Since an array of 20 of these records can be stored in memory and
can be sorted rapidly, even when swapping records, that is done here.

The keyboard entries are left to you. For convenience, constant arrays
are used here to load the record fields.}

USES CRT;
CONST  max = 9;  {number of records for this demo, 20 allowed}
phone:array[1..max] of string[30] = ('1010-811-1-(214) 567-3456',
   '(572) 972-5555', '222-3456', '1-(218) 654-3333', '222-7890',
   '1-(800) Dell4Me', '1010-802-1-(720) 638-9376', '222-7777',
   '1-(888) GoForIt');
callTime:array[1..max] of Integer = (300, 600, 1800, 800, 2400,
                       2500, 420, 1830, 1080);
TYPE
data = Record
          phoneNum:String[30];
          dial:String[30];   {numeric char string}
          paidTime:Integer;
       End;
Queue = Array[1..20] of data;   {not all used, but memory space used}

VAR
Que:Queue;

Function Convert(phone:String):String;
VAR tmp:String[30];
    j:Integer;
    ch:Char;
Begin
     tmp := '';
     For j := 1 to Length(phone) Do
     Begin
        ch := UpCase(phone[j]);
        Case ch of
           '0'..'9': tmp := tmp + ch;
           'A'..'C': tmp := tmp + '2';
           'D'..'F': tmp := tmp + '3';
           'G'..'I': tmp := tmp + '4';
           'J'..'L': tmp := tmp + '5';
           'M'..'O': tmp := tmp + '6';
           'P'..'S': tmp := tmp + '7';
           'T'..'V': tmp := tmp + '8';
           'X'..'Z': tmp := tmp + '9';
        End;
     End;
     Convert := tmp;
End;

Procedure Initialize(VAR Q:Queue);  {Loads record array}
VAR j:Integer;
Begin
     For j := 1 to max Do
     Begin
          Q[j].phoneNum := phone[j];
          Q[j].dial := Convert(Q[j].phoneNum);
          Q[j].paidTime := callTime[j];
     End;
End;

Procedure Show(Q:Queue);
VAR j, x, y:Integer;
Begin
     For j := 1 to max Do
     Begin
          Write(Q[j].paidTime:4, '    ', Q[j].phoneNum);
          x := WhereX; y := WhereY;
          While x < 40 Do Inc(x);     {cosmetic}
          GotoXY(x, y);
          Writeln('Dial: ', Q[j].dial);
     End;
End;

Procedure SortRecords(VAR Q:Queue);  {swaps records in array}
VAR j, k, n:Integer;
    buf:data;
Begin
     For j := 1 to n-1 Do
     Begin
          n := j;  {to identy larget time field found on this pass}
          For k := j+1 to max do
              If Q[k].paidTime > Q[n].paidTime then n := k;
          If n <> j then  {Q[n] is out of order, swap with Q[j]}
          Begin
               buf := Q[j];  Q[j] := Q[n];  Q[n] := buf;
          End;
     End;
End;

BEGIN
     ClrScr;   Writeln('ORIGINAL ORDER':30);
     Writeln('Secs', 'Phone Number':20);
     Initialize(Que);
     Show(Que); Writeln;

     Writeln; Writeln('SORTED RECORDS':30);
     Writeln('Secs', 'Phone Number':20);
     SortRecords(Que);
     Show(Que);
Readln;
END.

Other Threads