Fortran 77 making an array but changing the increment value

In summary: I'm starting to feel a little better about my decision to learn Fortran again!In summary, the programmer wrote an array from 0 to n, but wanted the array to be in steps of 5 rather than increasing each value by 1. They explained that you can access an element in the original array from the desired array by multiplying the desired array index by 5 and adding 1. Additionally, the programmer mentioned that C++ is a poor language choice for number crunching applications, and that Fortran is still commonly used for scientific applications. They ended with a reminder that Fortran has a tool called Assigned-Go-To which allows you to easily go to a desired element in an array. Finally, they shared that they are not advocates
  • #1
1ytrewq
6
0
Hi
I have an array going from 0 to n (where n has been previously defined) but I want the array to be in steps of 5 rather than increasing each value by 1

eg 0,5,10,15,20,25...n


how can I do this?

Thanks
 
Technology news on Phys.org
  • #2
What makes you use F-77 ? As it name says it is obsolete by 34 years.

But if you really must do that in F77 - either don't care about memory (you have modern computer with GBs of it) and just use only 1 of 5, or be smart F77 prgrammer and write something like
REAL A(number of elements)
A(N/5+1) = whatever
 
Last edited by a moderator:
  • #3
I'm starting a piece of working with an ancient supervisor and he only knows fortran 77 so I've had to start learning it too. Seems a completely ridiculous language from what I've learned so far

but thanks for your help
 
  • #4
Fortran is still commonly used for scientific applications, I use it regularly myself - in F77 flavor. Some co-workers use Basic. Newfangled languages aren't relevant to simple scientific number-crunching applications, the odds of screwing up your results far outweighs any advantages in efficiency, interfacing, sexiness, whatever.

For every element i in your original array [0,1,2,...,n], you can access it from your preferred array [0,5,10,...,5n] element j through i = (j+1)/5.
 
  • #5
JeffKoch said:
(j+1)/5.
?

Either :
J/5 - if you number arrays starting from 0,
or
J/5+1 - if you are 11 years more obsolete and use F-66 (Fortran-4...) convention of numbering arrays from 1

I am not much younger (probably) than your suprevisor, but I switched to do number crunching in C++ over 20 years ago...
 
  • #6
Seems clear enough. (0+1)/5 = 0, (5+1)/5 = 5 , (10+1)/5 = 2, etc., in standard integer math, which lops off anything after a decimal point. So given j on [0,5,10,...], i on [0,1,2,..] follows the transformation i = (j+1)/5.

C++ in particular is a poor language choice, it's far too easy to make a mistake that won't be caught by a compiler or even crash your program, it will just give you the wrong answer.

xts said:
?
Either :
J/5 - if you number arrays starting from 0,
or
J/5+1 - if you are 11 years more obsolete and use F-66 (Fortran-4...) convention of numbering arrays from 1

I am not much younger (probably) than your suprevisor, but I switched to do number crunching in C++ over 20 years ago...
 
  • #7
The general format for a DO loop in FORTRAN is the following:

Code:
DO 10 I=1,N,INC
          . . .
          DO SOMETHING
           . . .
   10     CONTINUE

The control variable (I in this case) is assigned an initial value of 1.
N is the limiting value, so the program will break out of the loop after executing for I = N (basically, if I > N).
INC is the increment value. If INC is not given in a program, it defaults to 1. If you give it the value 5, I will increase by INC each time through the loop.

Hope this helps.
 
  • #8
JeffKoch said:
Seems clear enough. (0+1)/5 = 0, (5+1)/5 = 5 , (10+1)/5 = 2,
(0)/5 = 0, (5)/5 = 5 (should be 1, I guess, mistype..) , (10)/5 = 2,
(0+3)/5 = 0, (5+3)/5 = 1, (10+3)/5 = 2,
(0+4)/5 = 0, (5+4)/5 = 1, (10+4)/5 = 2...
Why have you chosen +1 rather than +3?

C++ in particular is a poor language choice, it's far too easy to make a mistake that won't be caught by a compiler or even crash your program, it will just give you the wrong answer.
I am far from being an advocate of C++, it is awful language, encouraging you to make programming errors, but don't say that Fortran is safer!
We both are not quiche eaters!
(Real Programmers do not use Pascal, 1983: http://www.pbm.com/~lindahl/real.programmers.html)

ADDED>
Yes, Duncan, DO-loop is my favourite semantics for loop. Anyway, I am not advocating for
Code:
for (i=1; i<=n; i+=inc) {...}
- that is equally awful ;)
 
Last edited:
  • #9
xts said:
Why +1 ?

Just good, safe habit - you're guaranteed to add some numbers after the decimal that will be lopped off, so you're always on the same side of the integer that you want.

xts said:
We both are not quiche eaters!

:biggrin: And I even learned Pascal in college, though I've never actually used it except for that one class in like 1984.
 
  • #10
Well, Pascal is even more obsolete than F-77... But in 1984 it was much more elegant than F77... Actually - it is much more elegant than any modern language, but, of course, something a bit more geeky and efficient had to won...

Anyway: answer to OP is
Code:
REAL A(1:N)
DO 10 I=1, 5*N, 5
   do something to A(I/5+1)
10 CONTINUE
ADDED>
I just read the "Real Programmers..." again after 20 years... I forgot Fortran had had a very useful tool: Assigned-Go-To... I really miss that feature in C++ - if I only had it now it would really help me to avoid C++ programming pitfalls!
 
Last edited:
  • #11
xts said:
I forgot Fortran had had a very useful tool: Assigned-Go-To... I really miss that feature in C++.
C / C++ version of this is switch - case, you can even create a multiple entry point loop inside the switch, using one case for each entry point, called Duff's device:

http://en.wikipedia.org/wiki/Duff's_device

C / C++ also has pointers to functions, which is similar in concept to assigned goto's, using functions instead of code fragments. C++ extended this concept with overriding of member functions in structures or classes.

Getting back to the OP, if the array starts with 0, then normally it's indexes are described as going from 0 to (n-1), where n is the number of elements in the array. Previous posts already explained how to handle arrays that start at zero or start at one.
 
Last edited:

1. What is an array in Fortran 77?

An array in Fortran 77 is a collection of elements of the same data type that are stored in a contiguous block of memory. It allows for efficient storage and manipulation of data.

2. How do I declare an array in Fortran 77?

To declare an array in Fortran 77, you must specify the data type, the number of elements, and the name of the array. For example:
INTEGER A(10)
This declares an array named A with 10 elements of type INTEGER.

3. Can I change the increment value of an array in Fortran 77?

Yes, you can change the increment value (also known as the stride) of an array in Fortran 77. This can be done by using the STRIDE attribute when declaring the array or by using the STRIDE function to change the increment value at run time.

4. How do I change the increment value of an existing array in Fortran 77?

To change the increment value of an existing array in Fortran 77, you can use the STRIDE function. This function takes the array name, the new increment value, and the dimension of the array as parameters. For example:
CALL STRIDE(A, 2, 1)
This will change the increment value of array A to 2 for the first dimension.

5. What is the purpose of changing the increment value of an array in Fortran 77?

Changing the increment value of an array in Fortran 77 allows for more flexibility in accessing and manipulating data. It can be useful in situations where you want to skip certain elements or process data in a different order. It can also improve the performance of your code by reducing the number of loops needed for certain operations.

Similar threads

  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
6
Views
789
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
7
Replies
235
Views
9K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top