Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on Consecutive numbers

Status
Not open for further replies.

okoye

Programmer
Dec 4, 2013
7
US
Hi,
I am very new to programming using FORTRAN. Please, you guys should help me out in the problem I will describe below:

I want to write a FORTRAN program that arranges the numbers in consecutive pairs. That is AABBAABBAABB, etc. Where A is positive and B is negative but both A and B are of the same magnitude. The program should be that should the number following immediately is not of the same sign that it changes the sign to be the same such that they occur in pairs as I showed below.

Thank you very much for your help.

 
This is not very clear...does A represent the whole number or AA? Please post an example with actual numbers...

...even then we still will not help, around here, we do not write programs from scratch...YOU need to write the program, show that you are putting your own effort in trying to learn Fortran and THEN, if it gets tricky, we can help.

 
Thanks for your kind response. Kindly see below the pseudo code I have written. The output for the V(jc), the last column of the output I want the signs to be occurring in pairs. For e.g.: ...-2,-2,2,2,2,2,-2,-2...

In essence no element should occur in singles.
Thanks for your assistance.

program AABB
! the program generate fibonacci numbers
! f(0) = 0
! f(1) = 1
! f(n) = f(n-1) + f(n-2) for n>1
implicit none
integer :: i, jc,iseed
real:: V0,V(20),Vt

V0 = 2
iseed=88901
do i=1,20
V(i)=V0*sign(1.0,ran(iseed)-0.5)
end do

i=0
do jc=1,20
if(i) then
Vt = -V(jc)
end if
i=i+1
if(i.and.Vt.eq.-V(jc)) then
Vt = -V(jc)
else !if(i.eq.i.and.Vt.eq.V(jc)) then
Vt = V(jc)
end if

write (*,102) jc, i,V(jc)
end do

102 format(i3, i12, f9.4)

stop
end
 
To start, your variable 'i' is an integer and not a logical and, so, it cannot be used all by itself as a boolean; you do that in two places
Code:
if (i) then
and
Code:
if(i.and.Vt.eq.-V(jc)) then
What did you mean by that?

Instead, you need to do something line (i > 0) if that is what you meant or possibly use the mod() function if you would like to test for odd/even i...
 
Hi,
Thanks for your explanations. I am using the "i" as a counter which should be increased by 1 each time. Then, I wan to check if the initial "i" and the "i+1" have the same sign. If not, I want to make them the same so that the sign occur in pairs as I explained before.

 
I am not putting my thoughts into this nor compiling, just trying to help...I leave it to you to verify this.
Code:
    i=0
    do jc=1,20
        if ( i > 0 ) then
            Vt = -V(jc)
        end if
        
        if ( ( i > 0 ).and.( Vt .eq. -V(jc) ) ) then
            Vt = -V(jc)
        else !if(i.eq.i.and.Vt.eq.V(jc)) then
            Vt = V(jc)
        end if 
        
        i=i+1    
        write (*,102) jc, i,V(jc)
    end do
 
Oh, by the way, just noticed that your program does not anything as it never changes the sign of V(jc) and it does not do anything with Vt...at the end, it simply writes out V(jc) (which was never changed).
 
Thanks for your assistance. As you said, nothing gets done yet. In fact, the counter incrementation is not implemented for the V(jc).

 
Maybe you should simply inspect every second entry in the arrays and make the next one the same sign or the other way around. Also, you need to be careful about testing REAL numbers with the .eq. operator...it just does not fly well.

Here is this working version, as far as I can tell
Code:
program AABB
    ! the program generate fibonacci numbers
    ! f(0) = 0
    ! f(1) = 1
    ! f(n) = f(n-1) + f(n-2) for n>1
    implicit none
    integer :: i, jc,iseed
    real:: V0,V(20),Vt

    V0 = 2.0
    iseed=88901    
    call srand(iseed)
    do i=1,20
        V(i)=V0*sign(1.0,rand()-0.5)
    end do

    do jc=2,20,2
        if ( V(jc-1)*V(jc)<0.0 ) then
            V(jc-1) = V(jc)
        end if
        write (*,102) jc-1, V(jc-1)
        write (*,102) jc  , V(jc)
    end do
    102	format(i3, f9.4)
    stop
end
 
Thank you very much. I will test it very soon.
 
Thank you very much for your help. I have modified it a bit within my code and it works perfectly. I am very grateful.

Best.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top