Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tsu

Pascal problem...

This topic is 6522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

as you'v probably guessed, i have a problem. we are supposed to make a program (for my computer science class) that picks a bunch of random numbers, sorts them, and displays both the sorted ones, and unsorted ones on the screen (so you dont just assign list[count]:=list[count-1]+1...) anyway, my program doesnt exactly work correctly... i'l put the code below... (i hope the tabs work)
program lastone;
uses
    wincrt;
const
     max = 5;
var
   list:array[1..max] of integer;
   old:array[1..max] of integer;
   count:integer;
   bigest:integer;
   counter:integer;

begin
     randomize;

     {assign random numbers to the array}
     for count:=1 to max do
          begin
               list[count]:=random(100) + 1;
               old[count]:=list[count];
          end;

     count:=0;
     counter:=0;
     bigest:=1;

     {sort the list}
     for bigest:=1 to max do
     for count:=bigest to max do
     begin
     if (list[count] > list[bigest]) then
     begin
         list[bigest]:=list[count]
     end;
              end;
     end;

     {print out both on the screen}
     for count:=1 to max do
          begin
               writeln (list[count]:3,' - ',old[count]:3);
          end;

     
end.    
 
_________
Tsutomegi
EDIT: Fixed tabs... Edited by - tsu on January 11, 2001 11:01:42 AM

Share this post


Link to post
Share on other sites
Advertisement
Generally bigest goes from 1 to max - 1 and count goes from bigest + 1 to max. Those are not exactly errors, but wasted work since the first time through the inner loop you compare bigest to itself and the last time through the outer loop that is all the inner loop does. Other than that you are sorting the list in descending order. Except for the first time through the inner loop count is greater than bigest so you are checking if the entry further down the list is greater than the entry closer to the start and exchanging it if it is. Those are the only errors that stand out to me.

Share this post


Link to post
Share on other sites
ok, i fizzled, er i mean fixed it, and it works better than before, but, well, heres the output:

88 - 88
71 - 71
21 - 21
21 - 1
21 - 21

and then
99 - 15
99 - 44
99 - 57
99 - 99
7 - 7

my updated code:

program lastone2;
uses
wincrt;
const
max = 5;
var
list:array[1..max] of integer;
old:array[1..max] of integer;
count:integer;
bigest:integer;
counter:integer;
begin
randomize;
{assign random numbers to the array}
for count:=1 to max do
begin
list[count]:=random(100) + 1;
old[count]:=list[count];
end;
count:=0;
counter:=0;
bigest:=1;
{sort the list}
for bigest:=1 to max-1 do
for count:=bigest+1 to max do
begin
if (list[count] > list[bigest]) then
begin
list[bigest]:=list[count]
end;
end;
{print out both on the screen}
for count:=1 to max do
begin
writeln (list[count]:3,'' - '',old[count]:3);
end;

end.


I figured that i could re-write the sorter portion of the program, to take the first 2 numbers, check which one is larger, and then put that one on top. it didnt exactly work to well... same problem. i dont get it...

the code i replaced with is:

function check:boolean;
var
fcount:integer;
boo:boolean;
begin
for fcount:=1 to max do
begin
if (list[fcount] > list[fcount+1]) then
begin
boo:=TRUE;
end else
boo:=FALSE;

if (boo=TRUE) then
check:=TRUE;
end;
end;

...

{sort the list}
repeat
count:=count+1;
x:=list[count];
y:=list[count+1];
if (x begin
sx:=x;
x:=y;
y:=sx;
end;
until (check = TRUE);


I''m not sure if "check()" works properly... but i think it should still do something... it doesnt change anything in the list of numbers... i changed the < to a > (or the other way around, i dont really remember)

_________
Tsutomegi

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Here''s a program to do what you want (I think). It''s
untested, and I''m used to Delphi, so it may be wrong.

program sorted;

uses
wincrt;
const
max = 5;
var
list: array[1..max] of Integer;
count, temp: Integer;
finished: Boolean;
begin
Randomize;

{First of all, generate random numbers and display them}
for count := 1 to max do
begin
list[count] := Random(100) + 1;
WriteLn(list[count]); {make this good yourself }
end;

{Now sort the numbers}
{REALLY cheesy bubble sort used here. Make it good yourself, dammit :p }
repeat
Finished := True;

for count := 1 to Max - 1 do
if list[count] > list[count + 1] then
begin
{Swap the two numbers around}
temp := list[count];
list[count] := list[count + 1];
list[count + 1] := temp;
Finished := False;
end;

until Finished = True;

for count := 1 to Max do
WriteLn(list[count]); {make this good yourself }
end.

Alistair Keys

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!