ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi everybody, I'm struggling to find a way out hope you can help me, or at least tell me that is possible to do it. Anyway any suggestion is appreciated.
I'm trying to update a code written by someone else. In this portion of the code I will obtain one final array which is updated with values obtained from a series of calculations
nested in a do loop.
The question is: is it possible to obtain the separate individual values obtained from the calculations
for each step of the loop instead of having the final array, which is actually the result of sum of the individual calculations after every cycle of the loop?
Otherwise, is it possible to create different separate arrays with every cycle of a loop?
Sorry for being unclear, and thank you in advance for every suggestion/advice.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
The general form for doing something like this is, if I understand the question, to use an index value to see an individual element of an array (single- or multidimensional).
So, if you had an array of 1,000 elements and wanted to substitute or view the 565th element you wound refer to it as
Code:
x = array (565)
or
Code:
array(565) = x
(No, FORTRAN does not have to be upper case.)
In a loop you'd do the same thing, just looping until you hit the right element.
If you're working with a multidimensional array, you'd want to look at (or replace) the 565th row and second column
Code:
array (i, j) = x
where i = 565 and j = 2, and
Code:
x = array (i, j)
to use the value.
Keep in mind that FORTRAN arrays start at 1 versus C arrays that start at 0.
Thanks for your reply tronayne but is not what I'm trying to do. I'll try to be more clear.
I have an array (f_array) which is updated at every cycle of a loop, briefly the code looks like this:
Code:
...
do i =1,20
[...operations to obtain "number1", "number2" etc.]
f_array(i) = f_array(i) + number1
f_array(i+x) = f_array(i+x) + number2
f_array(i+2*x) = f_array(i+2*x) + number3
end do
So as you can see, in the end i'll get f_array which includes the result of all of the iterations of the loop.
But, I'd like to be able to separate and look at each single contribution for each cycle of the loop.
Is it possible to do that, for example, creating every time a separate array (different from f_array,
let's say from 1 to 20) for each cycle of the loop?
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Sure. Just create another array, same size, same type. Call it, say, s_array.
After the end of your existing loop, add another loop
Code:
do i = 1, 20
s_array(i) = f_array(i)
end do
Then print it, write it to a file or something so you can look at it.
There's probably some function to do an array copy but I haven't done FORTRAN for... well, a long time and I don't recall but it's simple enough to just copy.
Fortran supports array slicing, so if you have n values for this iteration in s_array, you can add them to corresponding values in f_array using a single statement:
Code:
f_array(1:n) = f_array(1:n) + s_array(1:n)
Note that you can use a slicing to initialize an array, too:
Code:
f_array(1:n) = 0.0
If your calculation is done using only PURE functions, for example using mycalculation(), then you can use
Code:
FORALL (i = 1:n)
s_array(i) = mycalculation(arguments...)
END FORALL
f_array(1:n) = f_array(1:n) + s_array(1:n)
which turns out to compile to quite efficient code.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.