FORTRAN conversion from int to char OR zero suppression

  • Fortran
  • Thread starter sergegirard
  • Start date
  • Tags
    Fortran Zero
In summary: Because you may not want to print every '0' in your numeric array. For example, if you have an integer array of 5 integers and 3 of those integers are '0's, you may only want to print the 2 non-zero integers. In summary, Serge is looking for a way to suppress printing the zeroes from an integer array, but is having difficulty finding a solution. He has tried converting the integers to a character array, but this process does not work correctly. He is looking for a way to do the conversion automatically.
  • #1
sergegirard
1
0
Hello,

I am new with FORTRAN but experienced with PLI/I, Cobol (mainframe), Clipper (PC) etc.

As an exercise I'm writing some small pograms.

Now I have one problem/question:

I have an INTEGER array (ARPAS). When printing this array I want to suppress printing the zeroes; only # 0. This option I can't find.

Then I tried to make a turn-a-round: convert those INTEGERs to CHARs (CHARPAS):

CHARPAS (RIJ , KOL) = ACHAR(ARPAS (RIJ , KOL) ) and even
CHARPAS (RIJ , KOL) = ICHAR(ARPAS (RIJ , KOL) )

and then conditioned printed (IF char .EQ. '0' print a space..)

Result is a lots of ☺ etc.

Input is : 0 1 0 7 0 21 0 35 0 35 0 21 0 7 0 1 0
and I want is : bb 1 bb 7 bb 21 bb 35 bb 35 bb 21 bb 7 bb 1 bb

where bb is blanks/spaces.

Any idea what goed wrong?

Thanks,
Serge/Belgium
 
Technology news on Phys.org
  • #2
Hi sergegirard,

I don't think I really understand exactly how you want your output formatted, so I may not be giving you the right information. But if you want to convert from a numeric to a character array here is one way.

The functions ichar and achar do not convert values from integer to character and vice versa, at least not in the sense of of taking the number 35 and converting it to the string '35'. They convert between a symbol and its ASCII code, so for my system I get:

print*,ichar('a') -----> prints out the number 97
print*,achar(97) -----> prints out the letter 'a'

because 97 is the ASCII code for 'a'.

To convert from integer to character you can use an internal file. I threw together a quick program to show you how the internal files might work; it works for the particular case you wrote in your posting, so maybe you can get some ideas for your particular problem. (It would need heavy editing to be a general purpose program.) It prints out your integer array, the character array with zeros, and the character array with zeros converted to an empty string (and therefore filled with blanks).

Code:
program internal_files
implicit none

integer, dimension(17)::arpas
character(len=3),dimension(17)::charpas
integer::i

arpas = (/ 0,1,0,7,0,21,0,35,0,35,0,21,0,7,0,1,0 /)

print*,arpas                               

do i=1,size(arpas)
 write(charpas(i),*) arpas(i)          !internal file used here
enddo

print*,charpas                           !character array with zeros

where (charpas.eq.' 0')
 charpas = ''
end where

print*,charpas                           !character array without zeros

end program internal_files


The dimension of the character array elements must be at least 1 greater than the number of digits of your integers (to handle the space for the sign).

Anyways, like I said I was not clear as to how you wanted to output the data since you did not include any details of the program in your posting, but maybe the above will give you some ideas.
 
  • #3
I guess I don't understand ...

If I understand you correctly, your willing to expend the cost of converting the numeric to character, testing the character for '0' and then conditionally print with two different formats depending on the outcome of the the test.

If that's the case, then why not simply check for numeric zero and then proceed with your printing options.
 

What is FORTRAN?

FORTRAN (FORmula TRANslation) is a high-level programming language used primarily for scientific and mathematical computing. It was first developed in the 1950s and has undergone many revisions since then.

What is the purpose of converting from int to char in FORTRAN?

Converting from int to char in FORTRAN allows for the manipulation and storage of characters, such as letters and symbols, in addition to numbers. This is useful for creating more complex and versatile programs.

How do you convert from int to char in FORTRAN?

In FORTRAN, the CHAR function can be used to convert an integer value to its corresponding character. For example, CHAR(65) would return the character "A". Another option is to use the ACHAR function, which converts a numeric value to its corresponding ASCII character.

What is zero suppression in FORTRAN?

In FORTRAN, zero suppression refers to the process of removing unnecessary zeros from a numeric value. This is commonly used to improve the readability and efficiency of output, especially in scientific and engineering applications.

How do you suppress zeros in FORTRAN?

To suppress zeros in FORTRAN, the I0 format descriptor can be used in a WRITE statement to specify the number of digits to be displayed. This will automatically remove any trailing zeros. Additionally, the TRIM function can be used to remove leading and trailing spaces from a string, including zeros.

Similar threads

  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
14
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Programming and Computer Science
Replies
4
Views
15K
  • Programming and Computer Science
Replies
2
Views
30K
  • Atomic and Condensed Matter
Replies
3
Views
863
  • Programming and Computer Science
Replies
2
Views
8K
Back
Top