0

I need to help my friend. It is simple task. I need to find a smallest element in the array.

program TEST
    ! your code goes here
    integer a(5), n, min
    a = (/2, -5, 3, 5, 8/)
    n = 5
    min = a(1)

    !reading from keyboard
    !do i=1,n
     !read*,a(i)
    !end do

    print*, 'array:'
    do i=1,n
     print*,a(i)
    end do

    !print*, 'отладка, поиск минимального:'
    do i=2,n
     if (min .lt. a(i)) then
      min = a(i)
      !print*, min, a(i), ' '
     end if
    end do

    print*, 'minimal: '
    print*, min

    stop
end

So. I don't have problem with alghoritm at all. It is easy for me to do it on any other language)) but not there. Problem appears when I compare min and a(i).

Programm thinks that min < a(i) all time. Why? So min would be 8, because 8 is the last.

1 Answer 1

7

You want to find the smallest a(i) so your test for the minimum so far is the wrong way around: use if (a(i).lt.min) min=a(i) or instead.

But you may want to consider MINVAL(a).

Sign up to request clarification or add additional context in comments.

3 Comments

I think it was because spaces and wrong order of variable there : if (min .lt . a(i)). I thought its equal min < a(i). About 2nd. I had found it already, so I deleted 2nd part.
@SharikovVladislav min .lt. a(i) is the same as min < a(i), or do I misunderstand your comment? I'll delete my exit part, as it didn't fit, anyway.
Forget) Its okay now, my fail. I misunderstood something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.