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!

Doubly Linked List? 1

Status
Not open for further replies.

Mercfh

Programmer
Oct 26, 2009
13
US
I was given an assignment for class (new class just started a week ago) for fortran.
I've done some reading on fortran and get the general syntax but had a question of what the "best" way to go about this would be.
here's the inquery:
"Write a program in Fortran that (1) reads in an integer n, (2) computes the sine of 1, 2, ..., n (in degrees), generating n floats, (3) inserts those n floats, one at a time, into an ordered doubly-linked list, then (4) prints the contents of the list in reverse order.

You may assume that n is less than 10,000. You must compute the sine values by using these relations:

sin (1) = 0.017452406437284
cos (1) = 0.999847695156391
sin (a + b) = sin (a) cos (b) + cos (a) sin (b)
cos (a + b) = cos (a) cos (b) - sin (a) sin (b)
By an ordered doubly-linked list I mean a list with nodes having pointers both to the next and the previous node, and with a dummy head node, ordered so that following the "next" pointers leads to non-decreasing values of the sines."

Problem is...I thought you couldn't do pointers in fortran?
either way I assume my best bet would be to create 3 linked lists? one to insert them. then sort, then insert the sorted list into another and reverse it?

Sorry I wish I had more ideas but i have never used a language like fortran before? so any ideas?

thanks
 
Mercfh said:
...but now I get a segmentation fault?
when you compile the program with the option bounds-check
Code:
g77 mercfh.f -o mercfh  -fbounds-check
then by attempt to run the executable you will get
Code:
$ mercfh
 Input n for Number of sin Computations
10
[COLOR=red]
Subscript out of range on file line 37, procedure mercfh.f/MAIN.
Attempt to access the 2009291924-th element of variable sdata.
[/color]
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
So the compiler says you all: you have an error on line 37, it's
Code:
     if(sData(j)-sData(ptr))11,12,12
because your loops are bad. Instead of something like this
Code:
      do 20 integer j = 1, nmax
      integer ptr=1
      do 30 integer h = 1, nmax
you need first to declare the variables j, h, ptr as integer and then use them in a loops.
I mean - declate first
Code:
      integer nmax, i, j, h, ptr
and then before line 37
Code:
      do 20 j = 1, nmax
      ptr=1
      do 30 h = 1, nmax
      if(sData(j)-sData(ptr))11,12,12
      ...
The other error is this on line 60
Code:
   15    j=next(j)
The compiler says:
Code:
mercfh2.f:60: (continued):
      15    j=next(j)
            2
Attempt to modify variable `j' at (2) while it serves as DO-loop iterator at (1)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top