Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

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

Report
help w/ program (arrays and sorts) Posted by prophesized on 29 Nov 2001 at 6:43 PM
im in desperate need of help. for school i have to write a program that reads 5 names of students and a grade for each then writes the names of the students in order of grades starting with the highest. i missed part of the class when this was taught so any help will do. i have to use an array and a bubble sort for this.
Report
Re: help w/ program (arrays and sorts) Posted by zibadian on 30 Nov 2001 at 3:17 AM
: im in desperate need of help. for school i have to write a program that reads 5 names of students and a grade for each then writes the names of the students in order of grades starting with the highest. i missed part of the class when this was taught so any help will do. i have to use an array and a bubble sort for this.
:
Well, a buble sort is basically a nested for-do loop, in which you compare two items in order from an array. In code it looks something like this:
for i:=1 to 4 do
  for j:=i to 5 do begin
    { Compare the items here }
  end;

This way iterates through the entire array several times and moves one item up or down the sorting order. The code above has been written for 5 items in an array[1..5].
The comparison is a simple if-then statement, which sets the order for the two items:
for i:=1 to 4 do
  for j:=i to 5 do begin
    if Names[j]>Names[j+1] then
      { exchanges names };
  end;

Names is an array of strings. In this code, if name[j] ought to come after name[j+1], and thus be exchanged.
The exchange part of the code is quite simple:
for i:=1 to 4 do
  for j:=i to 5 do begin
    if Names[j]>Names[j+1] then begin
      TempName:=Names[j];
      Names[j]:=Names[j+1];
      Names[j+1]:=TempName;
    end;
  end;

This pseudo-code sorts an array of five names alphabetically. To sort a list of grades, a similar code is used.
Report
how would i input names into the array??? (nt) Posted by prophesized on 30 Nov 2001 at 5:56 PM
: : im in desperate need of help. for school i have to write a program that reads 5 names of students and a grade for each then writes the names of the students in order of grades starting with the highest. i missed part of the class when this was taught so any help will do. i have to use an array and a bubble sort for this.
: :
: Well, a buble sort is basically a nested for-do loop, in which you compare two items in order from an array. In code it looks something like this:
:
: for i:=1 to 4 do
:   for j:=i to 5 do begin
:     { Compare the items here }
:   end;
: 

: This way iterates through the entire array several times and moves one item up or down the sorting order. The code above has been written for 5 items in an array[1..5].
: The comparison is a simple if-then statement, which sets the order for the two items:
:
: for i:=1 to 4 do
:   for j:=i to 5 do begin
:     if Names[j]>Names[j+1] then
:       { exchanges names };
:   end;
: 

: Names is an array of strings. In this code, if name[j] ought to come after name[j+1], and thus be exchanged.
: The exchange part of the code is quite simple:
:
: for i:=1 to 4 do
:   for j:=i to 5 do begin
:     if Names[j]>Names[j+1] then begin
:       TempName:=Names[j];
:       Names[j]:=Names[j+1];
:       Names[j+1]:=TempName;
:     end;
:   end;
: 

: This pseudo-code sorts an array of five names alphabetically. To sort a list of grades, a similar code is used.
:

Report
Re: how would i input names into the array??? (nt) Posted by zibadian on 5 Dec 2001 at 2:20 AM
Simply place a for-do loop around the input statement, like this:
for i:=1 to 5 do begin
  write('Give ',i,'th name: ');
  readln(Names[i]);
end;

Report
Re: help w/ program (arrays and sorts) Posted by iby on 8 Dec 2001 at 5:05 PM
: : im in desperate need of help. for school i have to write a program that reads 5 names of students and a grade for each then writes the names of the students in order of grades starting with the highest. i missed part of the class when this was taught so any help will do. i have to use an array and a bubble sort for this.
: :
: Well, a buble sort is basically a nested for-do loop, in which you compare two items in order from an array. In code it looks something like this:
:
: for i:=1 to 4 do
:   for j:=i to 5 do begin
:     { Compare the items here }
:   end;
: 

: This way iterates through the entire array several times and moves one item up or down the sorting order. The code above has been written for 5 items in an array[1..5].
: The comparison is a simple if-then statement, which sets the order for the two items:
:
: for i:=1 to 4 do
:   for j:=i to 5 do begin
:     if Names[j]>Names[j+1] then
:       { exchanges names };
:   end;
: 

: Names is an array of strings. In this code, if name[j] ought to come after name[j+1], and thus be exchanged.
: The exchange part of the code is quite simple:
:
: for i:=1 to 4 do
:   for j:=i to 5 do begin
:     if Names[j]>Names[j+1] then begin
:       TempName:=Names[j];
:       Names[j]:=Names[j+1];
:       Names[j+1]:=TempName;
:     end;
:   end;
: 

: This pseudo-code sorts an array of five names alphabetically. To sort a list of grades, a similar code is used.
:


Actually there is a small error in code. You should
use i instead of j+1. So the code should
look like this:

 ListSize:=5;
 for i:=1 to (ListSize-1) do
   for j:=i to ListSize do begin
     if Names[j]>Names[i] then begin
       TempName:=Names[j];
       Names[j]:=Names[i];
       Names[i]:=TempName;
     end;
   end;
 



Iby
Report
how do i read the names and scores so... Posted by prophesized on 11 Dec 2001 at 9:49 AM
...the names stay with the scores when the scores are sorted?
Report
Re: how do i read the names and scores so... Posted by iby on 11 Dec 2001 at 7:35 PM
: ...the names stay with the scores when the scores are sorted?
:

Simple:
Your sorting routine swaps array records.
If the record is only one element no problem.
If the record has more elemets (any number)
everything remains the same except that you
have to swap the WHOLE RECORD and not just
the element you use as sorting criteria.

Example:
type student=record
        first_name:string;
        last_name:string;
        mothers_maiden_name:string;
        shue_size:real;
        grade:real;
        cell_phone:string;
        IQ:byte;
        popularity:longint;
        got_laid:boolean; 
     end;
 
var class:array[1..25] of student;
    temp:student; { we need one extra for swapping }

    i,j:byte;
    

{ ....  }


 for i:=1 to 19 do               { search all records }
   for j:=i to 20 do              
     if class[i].grade<class[j].grade then   { compare grades }

       begin                     { swapp the whole record }
         temp:=class[i];
         class[i]:=class[j];
         class[j]:=temp;
       end;




Iby
Report
Re: how do i read the names and scores so... Posted by prophesized on 12 Dec 2001 at 9:24 AM
sop how would i put that into my program? here is what i have so far:

PROGRAM FiveGrades (input, output);

TYPE

grades=array [1..5] of integer;
names=array [1..5] of string;

CONST

length=5;

VAR

continue:char;
data:names;
intone:integer;
inttwo:integer;
intthree:integer;
intfour:integer;
intfive:integer;
score:grades;
tempone:integer;
temptwo:integer;
temp:integer;
////////////////////////////////////////////////////////////////
//GET DATA
////////////////////////////////////////////////////////////////
PROCEDURE GetData (var score:grades;
var data:names);

//GIVEN: nothing
//TASK: asks for names and grades
//RETURN: score and data
Var

temp:integer;

Begin

{?}

End;
////////////////////////////////////////////////////////////////
//SORT
////////////////////////////////////////////////////////////////
PROCEDURE Sort (var intfour:integer;
var intfive:integer);


//GIVEN: score
//TASK: sorts grades
//RETURN: sorted list

Var

intone:integer;
inttwo:integer;

Begin

For inttwo:= 1 to length-1 Do
begin
intfive:=inttwo;
for intone:=inttwo+1 to length do
if score[intone] > score[intfive] then
intfive:=intone;
intfour:=score[intfive];
score[intfive]:=score[inttwo];
score[inttwo]:=intfour;
end;


End;
/////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//PRINT DATA
////////////////////////////////////////////////////////////////
PROCEDURE PrintData (var score:grades;
var data:names);

//GIVEN: score and grades
//TASK: prints final data
//RETURN: nothing
Var

temp:integer;
tempone:integer;
temptwo:integer;

Begin

{?}



End;
////////////////////////////////////////////////////////////////
BEGIN

REPEAT

GetData (score, data);
Sort (intfour, intfive);
PrintData (score, data);
writeln;
writeln;
write ('Do you want to continue?(y or n)');
writeln;
readln (continue);
writeln;
writeln;

UNTIL (continue='n') or (continue='N');

END.

Report
Gee....So messy program :-) Posted by iby on 12 Dec 2001 at 12:58 PM
type student=record
        name:string;
        grade:real;
     end;

var class:array[1..5] of student;
    temp:student; { we need one extra for swapping }

    i,j:byte;

procedure get_data;
begin
  for i:=1 to 5 do begin
    writeln;
    write('Student #',i,' name:');readln(class[i].name);
    write('Student #',i,' score:');readln(class[i].grade);
  end;
end;

procedure sort_by_grade;
begin
  for i:=1 to 4 do               { search all records }
    for j:=i to 5 do
      if class[i].grade<class[j].grade then   { compare grades }

        begin                     { swapp the whole record }
          temp:=class[i];
          class[i]:=class[j];
          class[j]:=temp;
        end;
end;

procedure display_data;
begin
  writeln('Results are sorted by grade:');
  writeln('Nr. Grade   Student Name');
  writeln('---------------------------');
  for i:=1 to 5 do
    writeln(i,'    ',class[i].grade:2:2,'   '+class[i].name);
end;


  { main program }

begin
  get_data;
  sort_by_grade;
  display_data;
end.

Report
sorry i still cant get it to work (nt) Posted by prophesized on 17 Dec 2001 at 9:21 AM
:
: type student=record
:         name:string;
:         grade:real;
:      end;
: 
: var class:array[1..5] of student;
:     temp:student; { we need one extra for swapping }
: 
:     i,j:byte;
: 
: procedure get_data;
: begin
:   for i:=1 to 5 do begin
:     writeln;
:     write('Student #',i,' name:');readln(class[i].name);
:     write('Student #',i,' score:');readln(class[i].grade);
:   end;
: end;
: 
: procedure sort_by_grade;
: begin
:   for i:=1 to 4 do               { search all records }
:     for j:=i to 5 do
:       if class[i].grade<class[j].grade then   { compare grades }
: 
:         begin                     { swapp the whole record }
:           temp:=class[i];
:           class[i]:=class[j];
:           class[j]:=temp;
:         end;
: end;
: 
: procedure display_data;
: begin
:   writeln('Results are sorted by grade:');
:   writeln('Nr. Grade   Student Name');
:   writeln('---------------------------');
:   for i:=1 to 5 do
:     writeln(i,'    ',class[i].grade:2:2,'   '+class[i].name);
: end;
: 
: 
:   { main program }
: 
: begin
:   get_data;
:   sort_by_grade;
:   display_data;
: end.
: 

:

Report
WHAT??? (Re: sorry i still cant get it to work (nt)) Posted by iby on 19 Dec 2001 at 8:24 PM
What do you mean "you cannot make it work"? It's full
program and it works just fine. Do you know how to
compile it? Or you still have problem understanding sorting?

The idea in any sorting algorythm is to:
1. go through all records,
2. compare and then
3. swap them where needed (three step swap - a,b,c).

Difference between sorting algorythms is only in loops.
Rest is the same. It doesn't matter if you are
sorting one array or multiple arrays.

Check the following procedure which was used in previous
post:

procedure sort_by_grade;
begin
  for i:=1 to 4 do               { (1) search all records }
    for j:=i to 5 do
      if class[i].grade<class[j].grade then   { (2) compare grades }

        begin                     { (3) swapp the record(s) }
          temp:=class[i];         { A  copy one variable to temp } 
          class[i]:=class[j];     { B  copy second variable to first one }
          class[j]:=temp;         { C  copy temp to second variable }  
        end;
end;


It is doing exactly what was mentioned in the begining:
It has for loop(s) (i and j),
it has one comparison (if), and it has swap
section (begin ..... end;) with the famous
three step rotation (ABC). Remember this well since
we will use them later.

Ok let's do some spagetti code:
{ create arrays and one temp variable of same type for each array }

var   grade           :   array[1..5] of real;
      temp_grade      :   real;

      name            :   array[1..5] of string;
      temp_name       :   string;   

      last_name       :   array[1..5] of string;
      temp_last_name  :   string;

      smart           :   array[1..5] of boolean;
      temp_smart      :   boolean;

      i,j             :   byte;  { these are needed for loops }


{ etc.  }


  for i:=1 to 4 do                 { (1) same loops as before }   
    for j:=i to 5 do

      if grade[i]<grade[j] then    { (2) same compare as before }
        
        begin                      { (3) swap ALL records!!! }   
          temp_grade     := grade[i];      { A1     remember ABC from swap thing above ?? }  
          temp_name      := name[i];       { A2 }
          temp_last_name := last_name[i];  { A3 }
          temp_smart     := smart[i];      { A4 }
             ;
             ;
          grade[i]       := grade[j];      { B1 }  
          name[i]        := name[j];       { B2 }
          last_name[i]   := last_name[j];  { B3 }
          smart[i]       := smart[j];      { B4 }
             ;
             ;
          grade[j]       := temp_grade;    { C1 } 
          name[j]        := temp_name;     { C2 }
          last_name[j]   := temp_last_name;{ C3 }
          smart[j]       := temp_smart;    { C4 } 
        end;  


If you still don't get it, try to find someone else to
help you since I'm quite exhausted...

Iby
Report
Re: WHAT??? (Re: sorry i still cant get it to work (nt)) Posted by Jmunee on 19 Dec 2001 at 8:56 PM
i understand it but when i run it in the compiler i keep getting errors. i got the program in school ill post it in a day or .
Report
that was under friends s/n Posted by prophesized on 20 Dec 2001 at 8:18 AM
: i understand it but when i run it in the compiler i keep getting errors. i got the program in school ill post it in a day or .
:




 

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.