Help with Fortran: Reading/Sorting a List of Integers

  • Thread starter falcon0311
  • Start date
  • Tags
    Fortran
In summary, The speaker is seeking help with a Fortran program that reads and sorts a list of integers from a file. They are having trouble with the output, which is showing incorrect values and not writing to the new file. They suspect the issue may be with the "form" aspect of the program, but are unsure of how to fix it. They are seeking assistance and have tried removing the "form" aspect but it did not work.
  • #1
falcon0311
29
0
I know this isn't really physics related, but I was wondering if anyone here knows Fortran.

I'm trying to read a list of integers from a file and sort them. Well, I'm reading them and sorting them, but my problem is the list looks something like:

23
253
23
45
75
4
856
45
234
54
34
12
21

It's printing the total number of items as being 10, then it's printing this list:

0
165584
1633955692
1668244013
1697591601
1702047597
0
1731162226
0
925854305

Then it won't even write to the new file.
Here's my open statement,

open( unit = unitnum, file = infile, status = "old", action = "read", &
form = "binary", iostat = status )

I don't feel right about it, and if I try to change the form=, the program doesn't read the file altogether. Any help is much appreciated, and if you need to see anything else, by all means, let me know. Thank you.
 
Physics news on Phys.org
  • #2
try
open( unit = unitnum, file = infile, form = "binary" )
do i = 1,13
read(unit, 10 ) A(i)
enddo
10 format (F10.4)

is the file binary? I would suspect that your trying to read a file incorrectly. Try rewriting the file and saving in a *.dat naming convention. it has been a while since I wrote fortran, so my skills are rusty.
 
  • #3
The binary aspect was my main question I guess. I figured binary meant one character at a time, I want the whole number, but my teacher isn't a very understanding guy and doesn't like to explain stuff. My book doesn't even mention binary, it says formatted or unformatted, and these two make the program not work at all. Binary is the only thing I can even get a read for so far, so I'm using it.
 
  • #4
Binary refers to the file format. I don't think your file is in binary. Just drop the 'form="binary",' part and you should be OK.
 
  • #5
Tried that and for some reason it wouldn't read the list of integers at all.
 

1. How do I read a list of integers in Fortran?

To read a list of integers in Fortran, you can use the READ statement. This statement reads data from an external file or from the keyboard. For example, to read a list of 10 integers from the keyboard, you can use the following code:

READ*, (integer_variable, i = 1, 10)

2. How do I sort a list of integers in Fortran?

To sort a list of integers in Fortran, you can use the SORT function. This function sorts the elements of an array in ascending order. For example, to sort an array named "my_array", you can use the following code:

my_array = SORT(my_array)

3. How do I use the DO loop to read and sort a list of integers in Fortran?

To use the DO loop to read and sort a list of integers in Fortran, you can combine the READ and SORT statements within the DO loop. This will allow you to read multiple values from an external file or from the keyboard and then sort them. For example, to read and sort 10 integers from the keyboard, you can use the following code:

DO i = 1, 10READ*, integer_variable(i)END DOinteger_variable = SORT(integer_variable)

4. How do I store a list of integers in an array in Fortran?

To store a list of integers in an array in Fortran, you can declare an array using the DIMENSION statement. This statement specifies the size of the array and the type of data it will contain. For example, to declare an array named "my_array" that can hold 10 integers, you can use the following code:

DIMENSION my_array(10)

5. Can I use a different sorting algorithm in Fortran?

Yes, you can use a different sorting algorithm in Fortran if you prefer. The SORT function in Fortran uses the QuickSort algorithm, but you can also implement other sorting algorithms such as Bubble Sort, Selection Sort, or Insertion Sort. However, the built-in SORT function is typically more efficient and should be used unless there is a specific reason to use a different algorithm.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
8
Views
827
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
19
Views
5K
Back
Top