Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8082779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:13:56+00:00 2026-06-05T17:13:56+00:00

I’m using Schaum’s Outline of Programming With Fortran 77 book, and there’s an example

  • 0

I’m using Schaum’s Outline of Programming With Fortran 77 book, and there’s an example about binary search using bracketing group of values method. First of all here’s the code :

    INTEGER X(100)
    INTEGER RANGE
    INTEGER START , FINISH

    PRINT *, 'Number of values ?'
    READ *, N
    DO 10 I = 1, N
        READ *, X(I)
    END DO 

    PRINT *, 'Enter Value'
    READ *, VAL

    START =  1 
    FINISH = N
    RANGE = FINISH - START 
    MID = (START + FINISH) /2

    DO WHILE( X(MID) .NE. VAL .AND. RANGE .NE. 0)
      IF (VAL .GT. X(MID))THEN
        START = MID 
      ELSE 
        FINISH = MID
      END IF
      RANGE = FINISH - START
      MID = (START + FINISH)/2
    END DO 

    IF( X(MID) .NE. VAL) THEN
      PRINT *, VAL, 'NOT FOUND'
    ELSE
      PRINT *, 'VALUE AT' , MID
    END IF
    END

The problem is, when i enter 7 values array like

2 | 9 | 11 | 23 | 49 | 55 | 66

And search for 66 for example, when

MID = 5

, the new MID for the next loop becomes 6 . But when it’s 6, it can’t get incremented for the next loop because

MID = (START + FINISH)/2 = (6+7)/2 = 6

Where it should be 7 of course.

It still on 6. And my program never give me an output of course.
What shall I do here ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:13:58+00:00Added an answer on June 5, 2026 at 5:13 pm

    This is just a typo, or maybe someone got confused when they translated it from a C version and had to to change indexing.

    The key invariant in the loop is that the value, if it’s in the list, must fall in the array somewhere from indices start to finish, inclusive. But once you’ve excluded mid, it should be taken out the list. But it’s not here, so the list is always too long and you run into the problem you see.

    A correct version sets start to mid+1, or finish to mid-1, to exclude mid. The corrected code, written in a Fortran 90 style:

    program binarysearch
    
        implicit none
        integer, allocatable, dimension(:) ::  x
        integer :: range, start, finish, mid
        integer :: i, n, val
    
        print *, 'Number of values ?'
        read *, N
        allocate( x(N) )
    
        do i = 1, n
            READ *, x(i)
        end do
    
        print *, 'Enter Value'
        read *, VAL
    
        start =  1
        finish = N
        range = finish - start
        mid = (start + finish) /2
    
        do while( x(mid) /= val .and. range >  0)
          if (val > x(mid)) then
            start = mid + 1
          else
            finish = mid - 1
          end if
          range = finish - start
          mid = (start + finish)/2
        end do
    
        if( x(mid) /= val) then
          print *, val, 'NOT FOUND'
        else
          print *, 'VALUE AT' , mid
        end if
    
        deallocate( x )
    
    end program binarysearch
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.