Your Ad Here Your Ad Here
Results 1 to 9 of 9

Thread: Need a pascal programmer with time on their hands

  1. #1
    99shassan's Avatar Poster BT Rep: +1
    Join Date
    Jul 2003
    Posts
    830
    I have a problem, I was given this code by a teacher of mine for an assignment. The problem is, is that I don't know what the program code means, the program is supposed to arrange ten numbers into ascending or descending order. She only did the ascending code and made the code so that it will accept 3 numbers, so that we can expand it. But I can't understand what the code is doing. Now I went out on a limb and attempted to create the descending code, but that odesn't work as i do not now what the hell this code is doing.

    Code:
    {This is a simple program that will sort 3 numbers, and will allow the user
     to either choose to sort the number in ascending order or descending order.
    
     Created: NAMEHID
     BLAHBLAHBLAH}
    
    Program sortup_sortdown(Input,output);
    Uses Wincrt;
    
    VAR
            Num: array [1..3] of integer;
            Biggest             :integer;                  
            smallnum            :integer;
            currentnum          :integer;
            Choice              :char;
    
    Procedure getnums;
    Var loop :integer;
    
    Begin
            Writeln('Enter The Three Numbers');               {Title, giving instruction}
            for loop:=1 to 3 do                               {when loop appears, it will show
                                                               1 to 3}
    
    Begin
            Write('Enter number ',loop, ':');                {enter the number <loop>, or enter
                                                              number 1}
            readln(Num[loop])                                {Read the first number entered}
          end
          End;
    
    Procedure findthesmallest;                              
    var loop :integer;
    
    Begin
            Smallnum:=Currentnum;
    For     loop:=currentnum to 3 do
    IF      num[loop] <num[smallnum] then smallnum:=loop;
            End;
    
    Procedure findbiggest;
    var loop :integer;
    
    Begin
            Biggest:=currentnum;
    For     loop:=currentnum to 3 do
    IF      Num[loop] >num[biggest] then biggest:=loop;
            end;
    
    Procedure upsort;
    var loop, temp:integer;
    
    Begin
            write('In sort Up Procedure ');
    For     loop:=1 to 3 do                         {For the 3 numbers entered, do the next
                                                     code}
    
    Begin
            Currentnum:=loop;
            findthesmallest;
            temp:=num[smallnum];
            num[smallnum]:=num[currentnum];
            num[currentnum]:=temp;
            End
            End;
    
    Procedure downsort;
    var loop, temp:integer;
    
    Begin
            write('In Sort Down Procedure ');
    For     Loop:=1 to 3 do
    
    Begin
           Currentnum:=loop;
           findbiggest;
           temp:=num[biggest];
           num[biggest]:=num[currentnum];
           num[Currentnum]:=temp;
           End
           End;  
    
    Procedure Shownums;
    var loop :integer;
    
    Begin
            Writeln('The sorted numbers are:');
    For     Loop :=1 to 3 do
    
    Begin
            Writeln(loop:2, ': ',num[loop]);
            End
            End;
    
    Begin {Main Body}
    
            Getnums;
            writeln('Type A=Ascending D=Descending');
            Readln (Choice);
    If      Choice='A' then upsort;
    {If choice='D' the downsort;}
            Shownums
            End.
    I was hoping for someone to do a pseudocode for this? maybe taht could help me understand.
    Last edited by 99shassan; 06-26-2005 at 09:19 PM.
    Changed SPAN settings in sig a YEAR after it was removed

  2. Software & Hardware   -   #2
    99shassan's Avatar Poster BT Rep: +1
    Join Date
    Jul 2003
    Posts
    830
    oh yeah and the comments on the side is all that I understood from what was given
    Changed SPAN settings in sig a YEAR after it was removed

  3. Software & Hardware   -   #3
    Illuminati's Avatar Simple Bystander BT Rep: +7BT Rep +7
    Join Date
    May 2003
    Location
    2008 European Capital of Culture
    Age
    32
    Posts
    2,758
    First thing I'll say is that you really should have a nested approach to the program (e.g. have the loop body on the line below the line header, indented). But I digress.

    I used to do PASCAL, but now I usually code with C. I'll see what I can do with what I remember though.


  4. Software & Hardware   -   #4
    99shassan's Avatar Poster BT Rep: +1
    Join Date
    Jul 2003
    Posts
    830
    hi mate;

    thanks alot, any news yet?
    Changed SPAN settings in sig a YEAR after it was removed

  5. Software & Hardware   -   #5
    tesco's Avatar woowoo
    Join Date
    Aug 2003
    Location
    Canadia
    Posts
    24,069
    I never learnt pascal, but i can do my best to explain part of that code for you.

    VAR
    Num: array [1..3] of integer;
    Biggest :integer;
    smallnum :integer;
    currentnum :integer;
    Choice :char;
    Declaring variables, and one is an array that stores your numbers. You will need to change that to an array of 10 (set to 3 atm) when you make it 10 numbers.

    Procedure getnums;
    Var loop :integer;

    Begin
    Writeln('Enter The Three Numbers'); {Title, giving instruction}
    for loop:=1 to 3 do {when loop appears, it will show
    1 to 3}

    Begin
    Write('Enter number ',loop, ':'); {enter the number <loop>, or enter
    number 1}
    readln(Num[loop]) {Read the first number entered}
    end
    End;
    Does a loop for each number that the program will ask.
    Displays "Enter Number X" where X is the 'loop' variable.

    Procedure findthesmallest;
    var loop :integer;

    Begin
    Smallnum:=Currentnum;
    For loop:=currentnum to 3 do
    IF num[loop] <num[smallnum] then smallnum:=loop;
    End;
    This part finds the smallest integer. It does a FOR loop for each of the numbers.
    First it sets the first of the numbers to the smallest. It then starts the loop and if the number that the loop is on is smaller than the one that is currently set as the smallest, it sets that one to the smallest.

    The biggest one works the same way, only the biggest one is found...obviously.


    I can't explain the sorting tho, i don't understand how that works in pascal.


    Hope that helps you, maybe just a little.
    Last edited by tesco; 06-27-2005 at 12:37 AM.

  6. Software & Hardware   -   #6
    99shassan's Avatar Poster BT Rep: +1
    Join Date
    Jul 2003
    Posts
    830
    Hi, thanks for the help rossco , hows the pseudo code going illuminati?
    Changed SPAN settings in sig a YEAR after it was removed

  7. Software & Hardware   -   #7
    Barbarossa's Avatar mostly harmless
    Join Date
    Jun 2002
    Location
    Over here!
    Posts
    19,631
    Here's a pascal example for a simple bubble-sort, ascending order..

    Code:
    { read numbers to be sorted into the array A, where N is Number of integers }
            repeat
                Flag := true;
                for I := 1 to N - 1 do
                    if (A[I] > A[I+1]) then
                        begin
                        Temp := A[I]; 
                        A[I] := A[I+1]; 
                        A[I+1] := Temp;
                        Flag := false;
                        end;
            until (Flag)
    Basically, it keeps looping through the elements until at no time is the next element less than the current element, not too efficient, but it works well small sorts.

    For sorting into descending order I guess you change the ">" to a "<"

  8. Software & Hardware   -   #8
    Illuminati's Avatar Simple Bystander BT Rep: +7BT Rep +7
    Join Date
    May 2003
    Location
    2008 European Capital of Culture
    Age
    32
    Posts
    2,758
    The great thing about pseudocode is that if done right, the code writes itself

    First things first, sort out the data. You're right in sorting numbers instead of letters (they're a lot easier to compare ) At the start, we're talking about:
    Code:
    List[MAX] - Array of Integers (MAX = number of records)
    Record - Highest/Lowest in the list
    Once you've got your basic data stuff down (don't worry - programs almost always have more data put in ) in let's start at the basic ascending program in one swoop:

    Code:
    - Look up a number in an array "row"
    - If row's number is higher than the Record
     - Record = ID of the row
    The reason of using the IDs instead of the actual data themselves is so the data doesn't get tangled around. The actual data is insignificant - It's the program's job to simply sort them.

    Now that code above will do it for one swoop - The next stage is to add a loop to it

    Code:
    PROCEDURE Ascending_Search
    
    Record = 1
    
    Do a loop of n = 1 to n = MAX
    {
    - Look up a number in an array "row"
    - If row's number is higher than the Record
     - Record = ID of the row
    }
    That is your basic searching algorithm - It will look at each row and will end up recording the ID of the highest number in the list. Now, we have to do the actual switch.

    Unfortunately, switching isn't a case of switching between the two as one line will overwrite one of the data. A third interger, temp, has to be used to hold one of the values during the switch.

    Code:
    PROCEDURE Switch
    {
    - Put list[n] into temp
    - Put list[Record] into list[n]
    - Put temp into list[Record]
    }
    The neat thing about using the IDs is that you don't need seperate procedures for switching ascending/descending - Because all it's doing is switch the data of each ID, it will work either way (yay for optimisation! )

    Now we can put the whole thing together by declaring the switch algorithm and and also adding a record = 1 to reset it each time (a good thing to remember when using global variables)

    Code:
    PROCEDURE Ascending_Search
    
    Record = 1
    
    Do a loop of n = 1 to n = MAX
    {
    - Look up a number in an array "row"
    - If row's number is higher than the Record
     - Record = ID of the row
    }
    
    - Execute procedure "Switch"
    }
    What that will do is reset the record counter, make a loop of looking for the row in the array that holds the highest number, and then make the switch in a three-point-turn fashion.

    The next stage involves looking at the main function, the one that will start the program entirely. By this, you can also modify the search & switch procedures to compensate - Using main means you can set it so it doesn't go back on past territory

    Code:
    PROCEDURE Main
    Do a loop of k = 1 to k = MAX
    {
    - Execute procedure "Ascending_Search" (with m = k)
    }
    
    PROCEDURE Ascending_Search (parameter of m = interger)
    
    Record = m
    
    Do a loop of n = m to n = MAX
    {
    - Look up a number in an array "row"
    - If row's number is higher than the Record
     - Record = ID of the row
    }
    
    - Execute procedure "Switch"
    }
    
    PROCEDURE Switch
    {
    - Put list[m] into temp
    - Put list[Record] into list[m]
    - Put temp into list[Record]
    }
    Once this works, the rest is simple - The descending one is pretty much the same but reversing the condition, and then all that is needed is to add a keyboard input how you want

    Dunno whether it helps, but I've had a stab at writing this program in C. Although it may be a little daunting, there is usually a lot of similarities between programs so should be able to pick up the jist of it

    Code:
    list[5] = 3, 4, 2, 1, 9 
    
    void switch (int m, int record);
    {
    	int temp; 
    
    	temp = list[m];
    	list[m] = list[record];
    	list[record] = temp;
    }
    
    void ascending_search (int m)
    {
    	int record = m;
    
    	for n = m ; n < 5 ; n++)
    	{
    		if list[n] > list[record]
    		{
    			record = n;
    		}
    	}
    
    	switch(m, record);
    }
    
    void descending_search (int m)
    {
    	int record = m;
    
    	for n = m ; n < 5 ; n++)
    	{
    		if list[n] < list[record]
    		{
    			record = n;
    		}
    	}
    
    	switch(m, record);
    }
    
    void main()
    {
    	int k;
    	char key;
    
    	getch(key);
    	
    	if (key = 'a')
    		for (k = 1 ; k < 5 ; k++)
    		{
    			ascending_search(k);
    		}
    	if (key = 'd')
    		for (k = 1 ; k < 5 ; k++)
    		{
    			descending_search(k);
    		}
    }
    Hope that helps


  9. Software & Hardware   -   #9
    99shassan's Avatar Poster BT Rep: +1
    Join Date
    Jul 2003
    Posts
    830

    Smile

    I think I understand, I have added comments to my code, can someone check if the comments are right? thanks

    Code:
    {This is a simple program that will sort 3 numbers, and will allow the
     user to either choose to sort the number in ascending order or descending order
    
     Created: NAMEHIDDEN}
    
    Program sortup_sortdown(Input,output);
    Uses Wincrt;
    
    VAR
            Num: array [1..10] of integer;
            Biggest             :integer;                  
            smallnum            :integer;
            currentnum          :integer;
            Choice              :char;
    
    {The loop is the number of the number it is asking you to enter}
    
    Procedure getnums;
    Var loop :integer;
    
    Begin
            Writeln('Enter The Three Numbers');        {Title, giving instruction}
            for loop:=1 to 10 do                             {when loop appears, it will 
                                                                     show 1 to 10}
    
                                                               
    Begin
            Write('Enter number ',loop, ':');               {enter the number <loop>,
                                                                     or enter number 1}
                                                       
            readln(Num[loop])                                {Read the first number
                                                                    entered}
          end
          End;
    
    Procedure findthesmallest;                            {The title of this procedure is
                                                                   findthesmallest}
    var loop :integer;                                       {the loop is a variable, can be
                                                                  any number}
    
    Begin
            Smallnum:=Currentnum;                    {Set the first current number
                                                                 it is reading as the smallest}
                                                          
    For loop:=currentnum to 10 do                  {Each number it goes through 
                                                               is set to current number}
                                                          
    IF 
    num[loop] <num[smallnum] then smallnum:=loop; {If the current number is                                                                smaller than the current
                                                                      smallest number then  
                                                                      set that number as the 
                                                                      smallest}
    End;
    
    Procedure findbiggest;
    var loop :integer;
    
    Begin
            Biggest:=currentnum;                           {Set the first current number
                                                                    it is reading as the largest}
    For     loop:=currentnum to 10 do                   {Each number it goes  
                                                                    through is set as current
                                                                    number}
    IF      
    Num[loop] >num[biggest] then biggest:=loop;   {If the current number is 
                                                                    bigger than the current
                                                                    biggest number then set  
                                                                    that number as the 
                                                                    biggest}
            end;
    
    Procedure upsort;
    var loop, temp:integer;
    
    Begin
            write('In sort Up Procedure ');
    For     loop:=1 to 10 do                             {For the 10 numbers entered, 
                                                                 do the next code}
    Begin
            Currentnum:=loop;                        
            findthesmallest;                              {Run find the smallest
                                                               procedure}
            temp:=num[smallnum];                     {Display the smallest number 
                                                                that is worked out from the  
                                                                previous procedure}
            num[smallnum]:=num[currentnum];     {Set the current smallest 
                                                                number as the current number}
            num[currentnum]:=temp;                  {Set the current number as the 
                                                                temp, in other words
                                                                display the number}
            End
            End;
    
    Procedure downsort;
    var loop, temp:integer;
    
    Begin
            write('In Sort Down Procedure ');
    For     Loop:=1 to 10 do
    
    Begin
           Currentnum:=loop;
           findbiggest;                                     {Run find the biggest procedure}
           temp:=num[biggest];                         {show the biggest number that 
                                                                is worked out from the
                                                                previous procedure}
           num[biggest]:=num[currentnum];        {set the current biggest number 
                                                                 as the current number}
           num[Currentnum]:=temp;                   {show the current number}
           End
           End;  
    
    Procedure Shownums;
    var loop :integer;
    
    Begin
            Writeln('The sorted numbers are:');     {Display this message when this 
                                                                 procedure is run}
    For     Loop :=1 to 10 do
    
    Begin
            Writeln(loop:2, ': ',num[loop]);
            End
            End;
    
    Begin {Main Body}
     
            Getnums;
            writeln('Type A=Ascending D=Descending');
            Readln (Choice);
            If      Choice='A' then upsort   ;               {If the choice is A then run  
                                                                       Upsort procedure}
            if choice='D' then downsort ;                   {If the choice is D then run 
                                                                      downsort procedure}
    
            Shownums
            End.
    are the comments right?

    thanks a lot guys
    Last edited by 99shassan; 07-01-2005 at 06:13 PM.
    Changed SPAN settings in sig a YEAR after it was removed

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •