Solved

Random numbers

Posted on 2004-04-05
10
3,173 Views
Last Modified: 2010-04-16
i wrote a small example  a program to make it clear for you guys

for x := 1 to 5 do
begin
write('wtite a number : ') ;
readln(number)
end
for x := 1 to 5 do
writeln(number)

i want it to random then number by size after, like if i enter
5  , 1 ,  6 ,  7 , 2 the result will be
1 , 2 , 5 , 6 , 7  

any clue ?
0
Comment
Question by:blasta
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 100

Expert Comment

by:mlmcc
ID: 10762983
You want to SORT the numbers.  This sounds like a home work assignment, is it?

Can you use arrays? I suspect not from the sample code.

Basic algorithm
Compare A & B
Swap if out of order
Compare B & C
Swap if out of order
...
Repeat until no swaps

mlmcc
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 10763727
Something from the examples (with some changes), which came with Borland Pascal:

program sorting;

uses
  wincrt;

var
  numbers : array [1..16384] of integer;
  max : word;
  n : integer;

procedure Sort(var arr : array of integer; l, r: Integer);
var
  i, j, x, y: integer;
begin
  i := l;
  j := r;
  x := arr[(l+r) div 2];
  repeat
    while arr[i] < x do i := i + 1;
    while x < arr[j] do j := j - 1;
    if i <= j then begin
      y := arr[i];
      arr[i] := arr[j];
      arr[j] := y;
      i := i + 1;
      j := j - 1;
    end;
  until i > j;
  if l < j then Sort(arr, l, j);
  if i < r then Sort(arr, i, r);
end;

begin
write('How many numbers? : '); readln(max);
for n := 1 to max do begin
  write('write a number (', n, '): ');
  readln(numbers[n]);
end;
clrscr;
for n := 1 to max do write(numbers[n]:6);
writeln;
sort(numbers, 0, max - 1);
for n := 1 to max do write(numbers[n]:6);
end.
0
 

Author Comment

by:blasta
ID: 10765544
thanks guys but i was looking for a shorter answer , yes i can use array  ... but i'll wait to find if there is a shorter way to do it ...
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 10768911
Much slower example than the previous one, but.. It's shorter ;) and easier to understand, I think.

program sorting;

uses
  wincrt;

var
  max : word;
  arr : array [1..16384] of integer;
  tmp : integer;
  n : integer;

begin
write('How many numbers? : '); readln(max);
for n := 1 to max do begin
  write('write a number (', n, '): ');
  readln(arr[n]);
end;
clrscr;
for n := 1 to max do write(arr[n]:6);
writeln;
for n := 1 to max - 1 do begin {start sorting}
  if arr[n] > arr[n+1] then begin
    tmp := arr[n];
    arr[n] := arr[n+1];
    arr[n+1] := tmp;
    if n > 1 then n := n - 2;
  end;
end; {end sorting}
for n := 1 to max do write(arr[n]:6);
end.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Accepted Solution

by:
Aliev earned 125 total points
ID: 10785267
const
N=5;
var
mas:array[1..N] of integer;
i,j,k:integer;
begin

for i:=1 to N do
read(mas[i]);


for i:=1 to N do begin
     for j:=1 to N do begin
         if mas[i]<mas[j] then begin k:=mas[i]; mas[i]:=mas[j]; mas[j]:=k; end;
     end;      
end;



end.
0
 
LVL 1

Expert Comment

by:xtremecris
ID: 10801238
The shortest&fastest way to sort an array of numbers is not QuickSort, or RadixSort for that matter.

If you have numbers which are smaller than, let's say, 1000 you can sort them like this:
- use an array but not for saving the actuall numbers to memory, instead you save their frequency;
- when you read the number N from the keyboard you update the frequency like this:
    freq[N]:=freq[N]+1;
  or simply:
    inc(freq[N]);
- when you want to print the numbers you simply go throw all the numbers form 0 to 1000 with I in a FOR statement and if freq[I]>0 then you print I once(if freq[I]=i), twice(if freq[I]=2) and so on... Of course that can be done with an other FOR statement.

I do not aprove with the habit of others that solve your homework instead of explaing it to you... I hope the admins will take action in this matter!

Anyway if you have a question regarding what I said simply ask.

Best regards,
Cris.
0
 
LVL 1

Expert Comment

by:xtremecris
ID: 10802380
Okay, I see that you accepted an answer which solved your homework.
I'll give you my program too: it's shorter, and faster if you have a lot of numbers:

const Max=32000;
var
freq:array[1..Max] of integer;
i,j,k:integer;
begin
readln(n);
for i:=1 to N do
 begin
  read(j);
  inc(freq[j]);
 end;

for i:=0 to Max do
     for j:=1 to freq[i] do
       Write(j,' ');  
end;
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 11229488
Netminder:
Why the comment?

Having been an educator and given an assignment very much like this one, I was curious.  I also note he never responded to the question nor did he seem offended by it.

Pascal is a TA fraught with homework questions since Pascal is one of the primary teaching tools.  I realize it is used commercially but its original intent and was as a teaching tool and not for building applications for commercial use.

mlmcc
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Returning BSTR from a Delphi COM .DLL 2 1,819
Pascal code for reading registry 1 348
audio record to file 4 1,030
TEdit.SetFocus trouble 3 170
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now