Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

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

Report
arranging sorting an array Posted by chineerat on 2 Feb 2010 at 11:46 PM
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?

Report
Re: arranging sorting an array Posted by Atex on 3 Feb 2010 at 5:18 PM
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.

Report
Re: arranging sorting an array Posted by chineerat on 4 Feb 2010 at 11:47 AM
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

count :=1
begin
... values are entered


for i:=0 to count do begin

if CustomerSalary[i] > CustomerSalary[count] then begin
<arrays are sorted here>
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.


                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.



Report
Re: arranging sorting an array Posted by Atex on 4 Feb 2010 at 6:25 PM
: 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...


Report
Re: arranging sorting an array Posted by chineerat on 4 Feb 2010 at 11:50 AM
sorry for the double post
Report
Re: arranging sorting an array Posted by quikcarl on 21 May 2010 at 8:17 PM
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.
Attachment: QuickSort.zip (43296 Bytes | downloaded 225 times)



 

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.