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!

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

Jobs

How to find the highest value in a subset of one column of a two-column array....and more

How to find the highest value in a subset of one column of a two-column array....and more

How to find the highest value in a subset of one column of a two-column array....and more

(OP)
Hi
I’ve been struggling with this problem for a while, and perhaps somebody can help.
I have a 2-column array, e.g. as follows
3 2
6 4
12 0
- - - -
22 3
23 10
. .
. .
. .
66 4
I need to search column 1 (which is in ascending order) until I find a specified number, e.g. 12, then find the maximum value in column 2 up to that point, i.e. 4. I then need to find the number in the first column corresponding to this, i.e. 6. Then continue down the array until we see 4 again in the second column and return the corresponding number in the first column, i.e. 66.
There must be a concise and elegant way to do this, perhaps using intrinsic functions.

RE: How to find the highest value in a subset of one column of a two-column array....and more

I am afraid that a programming language can't simply provide intrinsic functions for everybody's wishes and desires...that's why one writes programs.

CODE --> Fortran90

program max_location
    integer :: i, j, k, num
    integer, dimension(1)    :: max_loc
    integer, dimension(15,2) :: m
    ! example data
    m(:,1) = (/ 3, 6,12,15,17,21,22,23,26,28,33,44,53,66,71/)
    m(:,2) = (/ 2, 4, 0, 3,10,12, 6, 8, 5,14, 1, 7,11, 4 ,3/)
    num = 12
    ! algorithm
    i = 1
    do j = 1, 2
        do while ( m(i,j) /= num )
            i = i + 1
        end do
        if ( j == 2 ) exit
        max_loc = maxloc(m(1:i,2))
        k = max_loc(1)
        num = m(k, 2)
        i = k + 1
    end do
    write(*,*) ' first  number = ', m(k,1)
    write(*,*) ' second number = ', m(i,1)
end program max_location
 

RE: How to find the highest value in a subset of one column of a two-column array....and more

(OP)
Learned a few things here. Many thanks, much appreciated.

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!

Resources

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