×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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!
  • Students Click Here

*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

Jobs

problem with forall

problem with forall

problem with forall

(OP)
I have array and I want to compare elements,how many 100.3 10.0 pairs do I have
100.3 10.0
110.0 10.0
100.3 10.0
I have tried this:
program pck

implicit none
real,dimension(10,2) :: a
integer,dimension(10) :: b
integer :: i,check

open(10,file='pic.txt')

do i=1,10
read(10,*)a(i,:)
end do

forall (i=1:10)
where (a(i,:) == a(i,:))
check=check+1
check=b(i)
end where
end forall
end program

But:
pck.f90:16.1:

check=check+1
1
Warning: The FORALL with index 'i' is not used on the left side of the assignment at (1) and so might cause multiple assignment to this object
pck.f90:17.1:

check=b(i)
1
Warning: The FORALL with index 'i' is not used on the left side of the assignment at (1) and so might cause multiple assignment to this object
pck.f90:16.1:

check=check+1
1
Error: WHERE assignment target at (1) has inconsistent shape
pck.f90:17.1:

check=b(i)
1
Error: WHERE assignment target at (1) has inconsistent shape

RE: problem with forall

Hhhmmm...forall and where constructs are not for everything, you know? You should read up more carefully on how to use them, their usage is not as universal as the 'do' loop.

Were you asked to implement such logic using forall and where? or is that something you figure you would do; otherwise, it can be easily implemented:

Input file

CODE

100.3 10.0
110.0 10.0
100.3 10.0
110.0 10.0
100.3 10.0
100.3 10.0 
Program

CODE

program pcount
    implicit none
    
    real, dimension(10,2) :: a
    integer, dimension(10) :: b
    
    integer :: i, n, check
    real :: r1, r2
        
    open(10, file='pairs.in')    ! input file
    
    n = 6                       ! numbe of lines to read from file
    r1 = 100.3  ; r2 = 10.0     ! pair to look for
    
    do i = 1, n                 ! read data
        read(10,*) a(i,:)
    end do
    write(*,'(f6.1,f6.1)') (a(i,:),i=1,n)   ! confirm data
    
    check = 0
    do i=1,n
        if ( a(i,1)==r1 .and. a(i,2)==r2 ) then
            b(i) = 1
        end if
    end do
    check = sum(b)
    write(*,*) 'number of pairs = ', check
        
end program pcount 

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!

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