Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with sort and file I/O

Status
Not open for further replies.

bahester

Programmer
Nov 8, 2009
2
US
Hi, I'm new to this website, and also very new with FORTRAN and programming in general but I have an assignment I'm very confused about and having lots of problems with. If anyone smarter than me could help me out or give me some tips at where to go that would be greatly appreciated.

Heres basically what I have to do:

Your job is to take the list of any store's employee IDs (the data file will always be called
"empids.dat"), sort them in order from lowest to highest, and then put the newly sorted list of IDs in a
new file of the user's choice. This newly constructed file will now be compatible with the payroll system,
and will save the company a lot of man hours in these troubling times. Naturally this will justify a few
layoffs, but luckily for us there is not a company, at least to my knowledge, that would do anything
similar to this in real life.

Here is a step-by-step breakdown of what your program should do:
? OPEN up the file "empids.dat" and use READ statements along with a counter and
'ierror' to find out how many lines of data are in the file. After all, each store does have
a different number of employees in it.
? ALLOCATE an allocatable array so that it has a number of elements that is the same as
the number of lines in the input file.
? READ the values from the input file into the array.
? Sort the values from lowest to highest.
? Ask the user what s/he would like to call the output file.
? CLOSE the old file; OPEN the new one.
? WRITE the sorted values to the output file.
? CLOSE the new file, and let the user know that the values were stored to the file.



In a nutshell. This is what I've got so far, past this, I'm pretty lost:

WRITE(*,'(A)',ADVANCE="NO") "Enter the employee file name: "
READ(*,*) fname

OPEN (UNIT = 10, FILE = 'fname', STATUS = "OLD", ACTION = "READ", IOSTAT = ierror)
IF (ierror /= 0) STOP "Error opening file. Aborting."

i = 1
DO
READ(10,*,IOSTAT=ierror) nums(i)
IF (ierror /= 0) EXIT
i=i+1

END DO

ALLOCATE (list(i), STAT = ierror)
IF (ierror /= 0) STOP "Error allocating array."


Any tips would be greatly appreciated.
 
EDIT: Realized I needed 2 open statements so this is where I'm sitting currently:


OPEN (UNIT = 10, FILE = 'fname', STATUS = "OLD", ACTION = "READ", IOSTAT = ierror)
IF (ierror /= 0) STOP "Error opening file. Aborting."

i = 1
DO
READ(10,*,IOSTAT=ierror) nums(i)
IF (ierror /= 0) EXIT
i=i+1

END DO
CLOSE(10)
ALLOCATE (list(i), STAT = ierror)
IF (ierror /= 0) STOP "Error allocating array."

WRITE(*,'(A)',ADVANCE="NO") "Enter the employee file name: "
READ(*,*) fname

OPEN (UNIT = 11, FILE = 'fname', STATUS = "REPLACE", ACTION = "WRITE", IOSTAT = ierror)
IF (ierror /= 0) STOP "Error opening file. Aborting.
 
You're on the right track, you have the elements in an array in code. You must have studied the techniques for sorting, heap/ binary, etc.

That would be your next step, to sort the elements from list() to a new aray, slist().

THen write out that array to the new file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top