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

  • SEARCH
  • Home
  • 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 6867735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:20:47+00:00 2026-05-27T03:20:47+00:00

I write a simple binary search program and meet a problem, see my three

  • 0

I write a simple binary search program and meet a problem, see my three confusions below:

  • If I choose icc -g main.c or icc -O0 main.c the code return wrong answer, but if I use icc -O2 main.c I can have the correct answer.

  • When the code mid = (beg + end) / 2; be defined before the if(beg <= end) { the answer is right no matter what flag I use to compile it.

  • I think the problem is the local var mid in the stack during the recursion.

Help me, thank you!

#include <stdio.h>
#define MAX 6
int a[MAX] = {1,2,3,4,5,6};
int improved_binary_search(int key, int beg, int end) {
    int mid, temp;
    //mid = (beg + end) / 2;
    if(beg <= end) {
             mid = (beg + end ) /2;
             if(a[mid] == key)
                     return mid;
             if(a[mid] < key)
                     return improved_binary_search(key, mid + 1, end);
              if(a[mid] > key)
                      return improved_binary_search(key, beg, mid - 1);
              //printf("test is %d\n", mid);
      }
      if(mid == 0)
              return 0;
      if(mid == MAX - 1)
              return MAX - 1;
      if(a[mid] < key)
              return a[mid+1] - key < key - a[mid] ? mid+1 : mid;
      if(a[mid] > key)
              return a[mid] - key < key - a[mid-1] ? mid : mid-1;
      return -1;
  }

int main(void)
{
     printf("%d\n", improved_binary_search(100, 0, MAX - 1));
     return 0;
}
  • 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-05-27T03:20:48+00:00Added an answer on May 27, 2026 at 3:20 am

    You just declared mid but did not initialize it, it might happen that if the first if condition(if(beg <= end))is not hit then the value of mid is Indeterminate.

    That would result in Undefined Behavior. The behavior you see is because of this UB and not because of compiler configurations.

    You should always initialize your variables to meaningful values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write simple Visual Studio Add-In for code generation. In my
i write a simple webservice code in asp.net when i build the service and
I write a simple piece of code that creates a div and assign it
I am trying to write some simple program to uploading files to my server.
I am trying to write a simple program for this interview question: Write a
Is there a simple way to write binary data into binary file like we
I am trying a reasonably simple program to test binary input/output. I am basically
I want to write two simple utilities: Receives a Binary file, and converts it
I write a simple php to load some binary data from DB then output
I have been asked to write a (very) simple program for a set of

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.