×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Help on Consecutive numbers

Help on Consecutive numbers

Help on Consecutive numbers

(OP)
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.

RE: Help on Consecutive numbers

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.

RE: Help on Consecutive numbers

(OP)
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

RE: Help on Consecutive numbers

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...

RE: Help on Consecutive numbers

(OP)
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.

RE: Help on Consecutive numbers

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 

RE: Help on Consecutive numbers

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).

RE: Help on Consecutive numbers

(OP)
Thanks for your assistance. As you said, nothing gets done yet. In fact, the counter incrementation is not implemented for the V(jc).

RE: Help on Consecutive numbers

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 

RE: Help on Consecutive numbers

(OP)
Thank you very much. I will test it very soon.

RE: Help on Consecutive numbers

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close