Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do you input an array so that it outputs two different arrays (fortran)

+9 votes
asked Mar 13 by Alexander Randall (340 points)
program even_odd_array
    implicit none
    integer, dimension(10)::a
    integer, dimension(10)::even_array, odd_array
    integer:: i, n , epos , opos
    print *, "enter a value of n (maximum = 10)"
    read*, n
    do i=1,n,1
        print*, "enter number " , i
        read*, a(i)
    end do
    do i=1,n,1
        if (mod(a(i),2)==0) then !!!this doesnt work
            a(i) = epos
        else if (mod(a(i),2)/=0)then
            a(i) = opos
        else if(mod(a(i),2)/=0)then
            epos = 0
        else
            opos = 0
        end if
        even_array = even_array(i)+even_array(epos)
        odd_array = odd_array(i)+odd_array(opos)
    end do
    print*, "the even array =" , even_array
    print*, "the odd array = " , odd_array
end program even_odd_array

!this is what I have, it works with no errors but it doesnt display what I am looking for.... How do I fix it?

1 Answer

+1 vote
answered Mar 25 by Peter Minarik (22,320 points)
edited Mar 25 by Peter Minarik

I have fixed your code. I am not a Fortran programmer, I just checked various tutorials and references. So potentially there could be better ways to do this. :)

Problems

I have found a few problems with your code. Please, see them below.

Uninitialized Variables

The variables epos and opos had no initial values and therefore they contained random numbers (a.k.a. memory garbage).

Separating the Evens and Odds

Your doing it wrong. A typical solution would be to initialize the even and odd pointers (epos and opos) to the start of the array and put the evens and odds into the even_array and odd_array indexed by the epos and opos pointers, which also increment every time we add a new value in them.

Printing Out The Result

You print out the whole array of evens and odds up to the maximum possible length. Instead, you must only print as many as you've found to be even and odd (see the opos and epos variables).

A Solution

After modifying your code, this seems to work.

subroutine PrintArray(arrayName, array, length)
implicit none
    character(len = *) :: arrayName
    integer, dimension(10) :: array
    integer :: length
    integer :: i

    print *
    print *, trim(arrayName)
    print *, "========================================"

    do i = 1, length
        print *, array(i)
    end do
end subroutine PrintArray

program even_odd_array
    implicit none
    integer, dimension(10) :: a
    integer, dimension(10) :: even_array
    integer, dimension(10) :: odd_array
    integer :: i
    integer :: n = 0
    integer :: epos = 0
    integer :: opos = 0
    
    ! Read the array
    do while (n <= 0 .or. 10 < n)
        print *, "Enter a value of n (0 < n <= 10)"
        read *, n
    end do
    do i = 1, n
        print *, "enter number " , i
        read *, a(i)
    end do

    ! Separate the values into two different array    
    do i = 1, n
        print *, a(i), i, epos, opos
        if (mod(a(i), 2) == 0) then
            epos = epos + 1
            even_array(epos) = a(i)
        else
            opos = opos + 1
            odd_array(opos) = a(i)
        end if
    end do

    ! Print the results
    call PrintArray("Even numbers:", even_array, epos)
    call PrintArray("Odd numbers:", odd_array, opos)
end program even_odd_array
commented Mar 25 by KKN (1,020 points)
you're such a legend :O
commented Mar 26 by Peter Minarik (22,320 points)
I guess that is my 2nd Fortran program. The first was also for someone asking something about functions in Fortran.

This one I tried to make the underlining in the PrintArray function to be dynamic (based on the length of the "arrayName"), but it was pretty difficult and there wasn't much documentation I could find.

The theory is simple: get the length of the arrayName (I can do that) and print out a character _length_ many times. However, the _print_ method ends up with a line break no matter what. There is a _write_ method, but you have to provide some special formatting for it which I wasn't able to find a good documentation on how to do no line break while printing characters. At least, not in a short time. So I went with a generic (blunt) fixed width underlining. ROFL
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...