Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Procedure problem

Posted on 1997-11-20
1
Medium Priority
?
164 Views
Last Modified: 2010-05-18
How do i write a procedure which takes an array of ten integer account codes and sorts them into ascending order. The only info i have been givenis:-
TYPE  numbers=array[1..10] of integer
Var   accounts : numbers

i = a var used to index the array  
j = a var used to index the array
figures = an array used to hold integers to be sorted
temp = used as a temporary store when elements of the array
       are to be interchanged
lowest = this holds the index of the current of the current
         lowest element of the array,figures

Top down
  for ten times
    initialize variables
  for the unsorted accounts
     if this account<lowest in unsorted part
     then
       set index of lowest to index of this account
     end if
    end for
set temp var to lowest account number
set lowest to value of first in unsorted part
set first in unsorted part to value of temp var
end for

I know that you will probably find this incredably easy but it is the first procedure i have come up against as i have only been doing this for a month.
any help you can give me will be much appreciated.
0
Comment
Question by:Scarhand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
mitchell042997 earned 400 total points
ID: 1216917
OK, if you have the globally defined type numbers as the array you are passing, then you will setup your procedure like so:

procedure SortAccounts(var someArray:numbers);
begin
  ...
end;

you will call this procedure as follows:

  SortAccounts(accounts);

Of course, if accounts is global you don't need to do all this hoopla, (although globally defined variables are trouble waiting to happen).  Now, for the sort, if you know that the accounts is an array of ten, just use a bubble sort.  It's not the fastest, but it will run good enough for an array of size 10.

so, here is what your code will look like (I have not tested this, so some syntactical errors may exist:

  procedure SortAccounts(var AccountArray:numbers);
  var done:boolean;
      loop:byte;
      dummy:integer;
  begin
    repeat
      done:=true;
 
     for loop:=1 to 9 do   {9 => 10 - 1}
        if AccountArray[loop]<AccountArray[loop+1] then begin
          dummy:=AccountArray[loop+1];
          AccountArray[loop+1]:=AccountArray[loop];
          AccountArray[loop]:=dummy;
          { This swaps the values in the array }

          done:=false;
        end;
    until done;
  end;

If this produces the variables in the wrong order (for example, ascending instead of descending), just change the less than sign in the if statement to a greater than sign.  Basically, this sort algorithm does the following:

start at the beginning of an indexed list (an array)
go from 1 to n-1 (9 in our case, since size of array = 10)
so... i = 1 .. n-1
if Array[i]<Array[i+1] Swap Array[i] with Array[i+1]
if you do swap, set the done flag to false, because we know we're not done until we traverse through the entire list without making any changes.
Exit the sort procedure once we go through the entire array without making any changes.

Now the array will be returned sorted.  So, if you were to do:

procedure SomeProc;
var A:numbers;
    loop:byte;
begin
  A[1]=5; A[2]=3; A[3]=7;
  A[1]=1; A[2]=9; A[3]=13;
  A[1]=8; A[2]=33; A[3]=4;
  A[10]=11;

  for loop:=1 to 10 do
    write(A[loop],'  ');

  SortAccounts(A);

  writeln; writeln;

  for loop:=1 to 10 do
    write(A[loop],'  ');
end;

your output would be:

5  3  7  1  9  13  8  33  4  11

1  3  4  5  7  8  9  11  13  33

Neat, eh?  If you have anymore questions, or if this doesn't help solve your problem, let me know, and I will explain further!  Thanks!!  :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With the evolution of technology, we have finally reached a point where it is possible to have home automation features like having your thermostat turn up and door lock itself when you leave, as well as a complete home security system. This is a st…
What monsters are hiding in your child's room? In this article I will share with you a tech horror story that could happen to anyone, along with some tips on how you can prevent it from happening to you.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question