Adding/Subtracting arrays in F77

  • Thread starter Thread starter PCSL
  • Start date Start date
  • Tags Tags
    Arrays
Click For Summary

Discussion Overview

The discussion revolves around adding elements of arrays in Fortran 77, specifically addressing the confusion between operations on character arrays versus numerical arrays. Participants explore the implications of data types and the operations that can be performed on them.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in adding elements of an array and seeks assistance.
  • Another participant requests to see the code to provide help, emphasizing the need for context.
  • A participant shares a simple Fortran code snippet and asks if specific elements can be added.
  • Some participants confirm that it is possible to add specific elements of an array, but caution against trial and error without understanding the underlying concepts.
  • There is a discussion about whether the elements being added are strings or numbers, with one participant questioning the data type of the array.
  • Clarifications are made regarding the nature of the elements, with some participants noting that character strings cannot be added like numbers in Fortran.
  • Concatenation of strings is suggested as an alternative operation, along with the need to declare the array as a numerical type for arithmetic operations.
  • A participant reflects on a previous misunderstanding regarding their code and acknowledges a successful operation after further clarification.

Areas of Agreement / Disagreement

Participants generally agree that the operation of adding elements depends on the data type of the array. However, there is no consensus on the initial confusion regarding the operations on character strings versus numerical values.

Contextual Notes

Limitations include the initial ambiguity in the participant's question regarding whether they were referring to elements or arrays, as well as the potential for misunderstanding the operations applicable to character strings versus numerical types.

PCSL
Messages
146
Reaction score
0
Hey, I'm new to fortran and I don't know how to add arrays. I opened a data file and assigned a different array to each number but if I type something like x(2)+x(3) it will not yield a result. This is probably a stupid question but thanks for any help you can offer.
 
Technology news on Phys.org
Please post your code. Can't help without seeing what the code is doing...can't take your word for it...sorry.
 
I just have a very simple code as follows:

Code:
        program test
        integer i,j
        character*14 x(10,7)
        open (unit=3, file='dstest')
        open (unit=8, file='dstestnew.data')
        read (3,*) ((x(i,j), i=1,10), j=1,7)
cccc [operation]
        close(unit=3)
        close(unit=8)
        end

My question is can i add specific arrays i.e. x(1,3)+x(4,4) or can you only use an array to perform operations on x(i,j). I have the dstestnew.data file open so I can write whatever I calculate to that file if that wasn't obvious.. thanks!
 
Last edited:
Short answer: Yes, you can do x(1,3)+x(4,4)

Long answer: You say that you are trying to see what operations you can do...why waste your time? How about you do a little bit of reading on fortran arrays? And find out for sure what it is that you can and cannot do? Please do not attempt to learn to program cluelessly by trial an error; sure, when something is not too clear, you can try variations of what they seem to be saying until you get it right and understand it.
 
gsal said:
Short answer: Yes, you can do x(1,3)+x(4,4)

Long answer: You say that you are trying to see what operations you can do...why waste your time? How about you do a little bit of reading on fortran arrays? And find out for sure what it is that you can and cannot do? Please do not attempt to learn to program cluelessly by trial an error; sure, when something is not too clear, you can try variations of what they seem to be saying until you get it right and understand it.

The reason I posted here is because I finished a schaum's outline on fortran and a book on F77 by Etter and neither addressed my question in the section on arrays. Googling did not help me figure out how to add specific arrays either. I tried variations using do-loops and could not successfully add them together. Also I tried assigning a variable to each array but that did not work either. I posted here because I was out of ideas, not because I didn't make an effort to figure it out on my own.

edit: I have no idea why I wrote trying to figure out specific operations in my code... all I care about is the operation addressed in my question.
 
PCSL said:
I just have a very simple code as follows:

Code:
        program test
        integer i,j
        character*14 x(10,7)
        open (unit=3, file='dstest')
        open (unit=8, file='dstestnew.data')
        read (3,*) ((x(i,j), i=1,10), j=1,7)
cccc [operation]
        close(unit=3)
        close(unit=8)
        end

My question is can i add specific arrays i.e. x(1,3)+x(4,4) or can you only use an array to perform operations on x(i,j). I have the dstestnew.data file open so I can write whatever I calculate to that file if that wasn't obvious.. thanks!

Sorry to butt in, but could you please clarify something? Are you trying to add element x(1,3) to element x(4,4)? Looking at the data type for x, are you reading a 14 character string into each element? If so, does FORTRAN perform automatic type conversion to some data type that supports addition? Or is the '+' operator overloaded to (say) concatenate strings?

Or ... from the wording of your question ("add specific arrays") you seem to imply that x(1,3) and x(4,4) are themselves arrays? Is this so or did you mean elements?
 
NemoReally said:
are you trying to add element x(1,3) to element x(4,4)
^ Yes.

You're right, I used poor vocab. I should have asked how to add specific elements of an array . Thanks for clarifying that. I understand that this is a fairly basic question but I don't think it is bad to seek help after I spent a few hours working on it.

NemoReally said:
Looking at the data type for x, are you reading a 14 character string into each element? If so, does FORTRAN perform automatic type conversion to some data type that supports addition? Or is the '+' operator overloaded to (say) concatenate strings?
Yes to the first question. I don't know the answer to the last two.
 
Last edited:
Sorry, but I still don't understand what you want to do.

If you show us some code you wrote that doesn't work, and a few lines of your data file, that mgiht help us work out what your problem is.

Just saying
Code:
cccc [operation]
doesn't give us many clues!
 
Your array contains character strings. You can't "add" character strings like you do numbers, in Fortran.

Do you want to concatenate the strings together, that is, combine e.g. 'abcde' and 'fghij' to produce 'abcdefghij'? In that case, you would use something like

result = x(1,3) // x(4,4)

Or do the strings contain a character-string representation of numbers (integers or reals)? In that case, you should have declared the array as INTEGER or REAL and read the numbers that way. There's no simple way to add numbers that are stored as digits in character strings.
 
  • #10
jtbell said:
Your array contains character strings. You can't "add" character strings like you do numbers, in Fortran.

Do you want to concatenate the strings together, that is, combine e.g. 'abcde' and 'fghij' to produce 'abcdefghij'? In that case, you would use something like

result = x(1,3) // x(4,4)

Or do the strings contain a character-string representation of numbers (integers or reals)? In that case, you should have declared the array as INTEGER or REAL and read the numbers that way. There's no simple way to add numbers that are stored as digits in character strings.

...I don't know what to say. I swear I did that before and it did not work but this time there was no problem. There must have been an error somewhere else in my code that I misdiagnosed.

Here's the operation I was trying to explain if any of you are curious and thanks for helping:
Code:
	program test
	integer i,j
	real x(10,7)
	open (unit=3, file='dstest')
	open (unit=8, file='dstestnew.data')
	read (3,*) ((x(i,j), i=1,10), j=1,7) 
	sum=x(1,1)+x(2,2)
	write (8,*), 'x(2,2)=',x(2,2)
	write (8,*), 'x(1,1)=',x(1,1)
	write (8,*), 'x(1,1)+x(2,2)=',sum
	close(unit=3)
	close(unit=8)
	end
 
  • #11
Well...this time, you have declared your matrix x as a REAL, as opposed to CHARACTER*14!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 15 ·
Replies
15
Views
5K
Replies
17
Views
2K
Replies
235
Views
14K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K