
-----------------------------------
ntba1
Sat Apr 29, 2006 7:23 pm

Multiple Array Sorting (Code included)
-----------------------------------
Hey guys just finishing off an assignment I have but I have one last critical part of my program before I can finish it and be able to clean the code up and make the interface look nice. I have a lottery sales program where people enter their Class number, Tickets sold and number of students. The last part of this assignment involves sorting the classes from most tickets sold to least tickets sold. That's fine and dandy, just sort the array but how would I have the other two array's line up to that one(Since Homeroom, TicketsSold and Students are all separate arrays). Here's the code (Yes I know it could be cleaner and if your wondering what the goto vars are for it's me trying to fake goto, plus I just started with Turing, I bet there is a MUCH easier way to do what I did) Remember I am using Classic Turing 6.5 and I cannot do anything about it my teacher insists we use it.


var Homeroom : array 1 .. 200 of int
var TicketsSold : array 1 .. 200 of int
var Students : array 1 .. 200 of int
var Average : array 1 .. 200 of real

var MainOption, InputOption, Start : string (1)
var Total : int := 0
var Rooms : int := 1
var goto : int := 0
var goto2 : int := 0
var goto3 : int := 0

setscreen ("graphics:vga")
setscreen ("nocursor")

procedure Options


    locate (6, 25)
    put "What would you like to do?"
    locate (10, 25)
    put "Please choose one: "

    locate (11, 25)
    put "A: Input"
    locate (12, 25)
    put "B: Summary"
    locate (13, 25)
    put "C: Exit"
    getch (MainOption)

    if MainOption = "A" or MainOption = "a" then
	cls
	goto2 := 1
	goto3 := 0
    else
	if MainOption = "B" or MainOption = "b" then
	    cls
	    goto2 := 2
	    goto3 := 2
	else
	    if MainOption = "C" or MainOption = "c" then
		goto2 := 0
		goto3 := 3
	    end if
	end if
    end if
end Options

procedure Input
    locate (10, 20)
    put "What would you like to input? (Please choose in order)"
    locate (12, 20)
    put "1: Enter New Rooms"
    locate (13, 20)
    put "2: Enter Ticket Sales data per room"
    locate (14, 20)
    put "3: Enter the number of students per room"
    locate (15, 20)
    put "4: Modify ticket sales of random room"
    locate (16, 20)
    put "5: Return to Main Options"

    getch (InputOption)

    if InputOption = "1" then
	cls
	put "How many rooms do you want to enter?"
	get Rooms
	cls
	for i : 1 .. Rooms
	    put "Enter room number: " ..
	    get Homeroom (i)
	end for
	goto3 := 0
    else
	if InputOption = "2" then
	    cls
	    for j : 1 .. Rooms
		put "Enter amount for room: ", Homeroom (j)
		get TicketsSold (j)
	    end for
	    for n : 1 .. Rooms
		Total := Total + TicketsSold (n)
	    end for



	else
	    if InputOption = "3" then
		cls
		for o : 1 .. Rooms
		    put "Enter the number of students in room: ", Homeroom
			(o)
		    get Students (o)
		end for

		for q : 1 .. Rooms
		    Average (q) := TicketsSold (q) / Students (q)
		end for

	    else
		if InputOption = "4" then

		else
		    if InputOption = "5" then
			goto3 := 1

		    end if
		end if
	    end if
	end if

    end if


end Input

procedure Summary
    locate (1, 1)
    put "Summary:              Rooms:"
    locate (2, 1)
    put "FJ Brennan High School"
    locate (3, 1)
    put "Total Sales: ", Total, " Tickets"


    put "Press any key to go back"
    for l : 1 .. Rooms
	locate (l, 30)
	put Homeroom (l)
    end for
    locate (1, 35)
    put "Tickets Sold:"
    for m : 1 .. Rooms
	locate (m, 50)
	put TicketsSold (m)
    end for
    locate (1, 55)
    put "Students:"
    for p : 1 .. Rooms
	locate (p, 67)
	put Students (p)
    end for

    for x : 1 .. Rooms
	locate (x, 72)
	put Average (x)
    end for





    getch (Start)
    goto3 := 1
end Summary

% Start Of Program
% Welcome Screen

locate (10, 20)
put "Hello and welome to cardinal Lottery!"
locate (11, 20)
put "Press any Key to continue"
getch (Start)
cls
Options

if goto2 = 1 then
    Input
else
    if goto2 = 2 then
	Summary
    else
	if goto2 = 0 then
	end if
    end if
end if

loop
    if goto3 = 0 then
	cls
	Input
    else
	if goto3 = 1 then
	    cls
	    Options
	else
	    if goto3 = 2 then
		cls
		Summary
	    else
		if goto3 = 3 then
		end if
	    end if
	end if
    end if

    if goto2 = 0 then
	exit
    end if
end loop




Thanks and I hope I didn't confuse you guys with my code  :) 


PS. Feel free to reccomend any other changes I can do to make my code better!

-----------------------------------
do_pete
Sat Apr 29, 2006 7:46 pm


-----------------------------------
You should use record instead of having 4 arrays. Also setscreen ("graphics:vga") doesn't do anything because Turing always starts in graphics mode.

-----------------------------------
ntba1
Sat Apr 29, 2006 7:58 pm


-----------------------------------
Well I was praying that I wouldn't have to do something like rewrite everything after spending the whole day writing this. Is it possible to sort the arrays some way without having to do it with records? :(

-----------------------------------
do_pete
Sat Apr 29, 2006 8:06 pm


-----------------------------------
You don't need records, I'm just saying it would be better with them.[/i]

-----------------------------------
ntba1
Sat Apr 29, 2006 8:16 pm


-----------------------------------
OK that's a relief but how would I go about implementing this sort then? I have no clue where to start from. Perhaps you can write some Pseudocode showing me how to write this? Thanks again :)

-----------------------------------
Cervantes
Sat Apr 29, 2006 8:18 pm


-----------------------------------
If you refuse to switch to records (which would by far be easiest, easier than what I'm about to suggest), just modify the other arrays in parallel to the one you're sorting by.


var swapArr (var arr : array 1 .. * of int, i, j : int)
    const temp := arr (i)
    arr (i) := arr (j)
    arr (j) := temp
end swapArr
proc swap (i, j : int)
    swapArr (homeroom, i, j)
    swapArr (ticketsSold, i, j)
    % ...
end swap

% Use some sorting method that uses 'swap'


-----------------------------------
ntba1
Sat Apr 29, 2006 9:29 pm


-----------------------------------
Thanks a bunch! I've learned my lesson time to go read up on records  :wink:

-----------------------------------
MysticVegeta
Sun Apr 30, 2006 10:20 am


-----------------------------------
Heh, well if you dont want to code the whole thing seeing that you have spent a lot of time doing coding, I will tell you how to do it without records, Cervantes' code is pretty useful too its more organized and easier to understand, but look whats going on in the code that I am posting. You see, "arr" array carries random numbers, "arr2" carries a string from "1" to "5", "arr3" carries numbers from 1 to 5, now I use the same old sorting algorithm,  but I create 2 more temp variables, now whenever the "if" condition is met, not only do I change the values of the "arr" array, I also change the values of "arr2" and "arr3". Thats the key. The first 5 lines are the original #s and the other 5 lines after the line break show how the first column is sorted and the values of the second and third column are organized according to it.

var temp, temp3 : int
var temp2 : string

var arr, arr3 : array 1 .. 5 of int
var arr2 : array 1 .. 5 of string

for x : 1 .. 5
    arr (x) := Rand.Int (1, 10)
    arr2 (x) := intstr (x)
    arr3 (x) := x
    put arr (x), " ", arr2 (x), " ", arr3 (x)
end for

put ""

for i : 1 .. 5
    for j : i .. 5
        if (arr (i) > arr (j)) then

            temp := arr (i)
            temp2 := arr2 (i)
            temp3 := arr3 (i)

            arr (i) := arr (j)
            arr2 (i) := arr2 (j)
            arr3 (i) := arr3 (j)

            arr (j) := temp
            arr2 (j) := temp2
            arr3 (j) := temp3
        end if
    end for
end for

for x : 1 .. 5
    put arr (x), " ", arr2 (x), " ", arr3 (x)
end for

