Sorting a 2D Array? Please Help Me?????
Author |
Message |
Hobz
|
Posted: Sat Apr 10, 2004 4:40 pm Post subject: Sorting a 2D Array? Please Help Me????? |
|
|
I really need help ASAP! I am very lost
I need to write a programme that reads a data file (data2a.t) that contains last names, provinces, and phone numbers. Determine the size of the file by counting the number of items in the file. Sort (bubble sort) the elements in the file in descending order. The output willl be the unsorted data and the sorted dat, and it needs to be written to a file. Sort the items by last name.
PLEASE HELP!!! I AM VERY LOST. THAT IS DA EXACT QUESTION FROM MY PAGE.
THX ALOT TO THOSE WHO HELP.[/code]
PS. I ATTACHED THE DATA FILE IN CASE
Description: |
|
Download |
Filename: |
data2a.t |
Filesize: |
771 Bytes |
Downloaded: |
279 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Paul
|
Posted: Sat Apr 10, 2004 5:33 pm Post subject: (No subject) |
|
|
sorting by what? alphabetical?
And do you have to do it in 2D array?
and also tell us what you don't understand, or we'll just end up writing it
for you. I suggest not to do this in a 2D array, but if you wish to...
[edit] oh what the heck, I'll explain.
I'll list some essential things you'll prolly need:
1. the getting stuff from text file function
code: |
var textfile:string:="data2a.txt"
|
2. putting the stuff into a variable
code: |
var input: int
open: input, textfile, get
|
3. outputting it into a text file
code: |
var outfile:string:="outfile.txt"
var output: int
open: output, outfile, put
|
4.
putting the things into a 2D array
code: |
var data: array 1..33, 1..3 of string
for a: 1..33
for b: 1..3
get: input, data(a, b)
end for
end for
|
sorting can be done by > and < I think letters can be compared in turing.
But I strongly suggest you don't use 2D array here for your own sake. Its much easier to separate the name, province and telephone into 3 different arrays, then sort them. Because if you move the name, you have to move the corresponding info.
|
|
|
|
|
|
Hobz
|
Posted: Sat Apr 10, 2004 6:06 pm Post subject: Thanks for help Paul Bian |
|
|
I have to sort it by last name. I have my code attached, so if u or anyone else wants to check it for me and tell me whats wrong, that would be great.
THANKS A LOT FOR THE HELP!!! LOOKING FORWARD TO A REPLY THAT WILL FIX MY PROBLEMS.
PS. IT MUST USE 2D ARRAYS AND FUNCTIONS/PROCEDURES
Description: |
|
Download |
Filename: |
Sorting Problem 2D.t |
Filesize: |
8.06 KB |
Downloaded: |
263 Time(s) |
Description: |
|
Download |
Filename: |
Sorting Problem 2D.t |
Filesize: |
8.06 KB |
Downloaded: |
220 Time(s) |
|
|
|
|
|
|
Paul
|
Posted: Sat Apr 10, 2004 6:08 pm Post subject: (No subject) |
|
|
Sorry, Im only half way thru grade 10, dunno exactly how local variables and functions work. And your programming style is way different than mine. Sorry can't help you.
|
|
|
|
|
|
Delos
|
Posted: Sat Apr 10, 2004 6:41 pm Post subject: (No subject) |
|
|
You've got an error w/ your upper bounds.
Here's an idea:
Load the file into memory.
Store each entry as a record:
code: |
type person:
record
name : string
num : string
end record
var book : 1..20 of person
% Of course the actual upper of this array will be determined by your file input.
|
Now, copy all the 'name' fields into an array.
Sort that array.
Match the original array (w/ names + nums) with the sorted array, comparing the 'name' field. Possibly copy to a third array of person.
Output this final array into a file.
Of course, all of this can be procedurized/functionized (word?) as much as you want...
|
|
|
|
|
|
AsianSensation
|
Posted: Sun Apr 11, 2004 1:51 pm Post subject: (No subject) |
|
|
what is the text file like? is it for every single person' last name, exactly 1 province and 1 telephone number corresponds with it? I could see the 2D array of a structure if there are more than 1 person in a province, but otherwise, I don't even think a 2D array is needed. But, whatever tickles your teachers fancy.
Try to upload the text file, so we can work with the style your teacher wants to.
|
|
|
|
|
|
Paul
|
Posted: Sun Apr 11, 2004 7:04 pm Post subject: (No subject) |
|
|
The text file IS the first .t program lol, there isn't a program in there, just the text file. This would be much simpler with 3 different arrays, simpler in a way that it is easy to rearrange the data when you change the position of a name.
|
|
|
|
|
|
|
|