Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

arranging sorting an array

chineeratchineerat Posts: 4Member
Hi!
i am having trouble sorting an array using a for loop.
the problem occurs when using iterations i and i+1

for example
count:=0
for i:=0 to count do begin
if a[i] > a[i+1] then begin
....

end
count+1
end

problem is when the for loops reaches the LAST array value i , i+1 does not exist hence the problem.
I have tried using
" for i:=0 to count-1 do begin"
this does not help.

any suggestions?

Comments

  • AtexAtex Posts: 268Member
    [code][color=Blue]const array_size=10;

    var t:array[1..array_size] of integer;
    i,j:byte;

    procedure swap(var a,b:integer);
    begin
    a:=a xor b;
    b:=a xor b;
    a:=a xor b;
    end;

    begin
    for i:=1 to array_size do begin
    write('Enter element no. ',i:2,': ');readln(t[i]);
    end;

    for i:=array_size downto 2 do {Simple bubble sort}
    for j:=1 to i do
    if t[i]>t[j] then swap(t[i],t[j]);

    for i:=1 to array_size do
    writeln(i:2,': ',t[i]);
    readln;
    end.[/color][/code]
  • chineeratchineerat Posts: 4Member
    hi!
    i have listed the entire code for reference at the bottom.

    however I have tried something similar to what you suggested
    I am still having the trouble of the sorting 'jumping ahead'.
    the problem part is
    [code]

    count :=1
    begin
    ... values are entered


    for i:=0 to count do begin

    if CustomerSalary[i] > CustomerSalary[count] then begin

    tmpCustomerName:= CustomerName[i];
    tmpCustomerLoan:= CustomerLoan[i];
    tmpCustomerSalary:= CustomerSalary[i];

    CustomerName[i]:= CustomerName[count];
    CustomerLoan[i]:= CustomerLoan[count];
    CustomerSalary[i]:= CustomerSalary[count];

    CustomerName[count]:= tmpCustomerName;
    CustomerLoan[count]:= tmpCustomerLoan;
    CustomerSalary[count]:= tmpCustomerSalary;

    end;
    end;
    count is incremented here

    end.
    [/code]

    [code]
    Program ;


    var CustomerName: array[1..25] of string;
    var CustomerLoan: array[1..25] of integer;
    var CustomerSalary: array[1..25] of integer;
    var i,j: integer;
    var tmpCustomerName:string;
    var tmpCustomerLoan:integer;
    var tmpCustomerSalary:integer;
    var count: integer;
    const array_size =25;
    var YesOrNo: char;
    Begin
    count:=0;
    FOR i:=0 TO 25 DO

    begin

    Writeln('Please enter Customer Name');
    Readln(CustomerName[i]);
    Writeln('Please enter ', CustomerName[i],' loan');
    Readln(CustomerLoan[i]);
    Writeln('Please enter ', CustomerName[i],' Salary');
    Readln(CustomerSalary[i]);



    Writeln('ALL CUSTOMERS');
    FOR i:=0 TO count DO
    begin
    Writeln(CustomerName[i],'----Loan:',CustomerLoan[i],'-----Salary:',CustomerSalary[i]);
    end;
    Writeln('---------------------------------------------------');

    Writeln('Customers with Loans equal to 1');
    FOR i:=0 TO count DO
    begin
    if CustomerLoan[i]= 1 then begin
    Writeln('Loan:',CustomerLoan[i],'---Name---',CustomerName[i],'-----Salary:',CustomerSalary[i]);
    end;
    end;
    Writeln('---------------------------------------------------');


    for i:=0 to count do begin

    if CustomerSalary[i] > CustomerSalary[count] then begin

    tmpCustomerName:= CustomerName[i];
    tmpCustomerLoan:= CustomerLoan[i];
    tmpCustomerSalary:= CustomerSalary[i];

    CustomerName[i]:= CustomerName[count];
    CustomerLoan[i]:= CustomerLoan[count];
    CustomerSalary[i]:= CustomerSalary[count];

    CustomerName[count]:= tmpCustomerName;
    CustomerLoan[count]:= tmpCustomerLoan;
    CustomerSalary[count]:= tmpCustomerSalary;

    end;
    end;
    end;

    Writeln('5 Customers with highest salaries');
    FOR i:=0 TO 5 DO
    begin
    Writeln('Salary:',CustomerSalary[i],'---Name----',CustomerName[i],'----Loan:',CustomerLoan[i]);
    end;
    Writeln('---------------------------------------------------');



    count :=count +1;
    end;



    end.


    [/code]
  • chineeratchineerat Posts: 4Member
    sorry for the double post
  • AtexAtex Posts: 268Member
    : i have listed the entire code for reference at the bottom.
    :
    : however I have tried something similar to what you suggested
    : I am still having the trouble of the sorting 'jumping ahead'.

    Your program has way too many errors to list, it doesn't even compile. Just adapt my code to your needs...


  • quikcarlquikcarl Posts: 59Member
    I use QuickSort to sort any array that needs
    sorting and multiple arrays that either need
    ascending or descending order. I've made my
    QuickSort into a unit. Just follow what I've
    done in my zip file with the example.
Sign In or Register to comment.