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 6891917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:28:11+00:00 2026-05-27T06:28:11+00:00

I am trying to write an iterative search in a C program. The pseudo-code

  • 0

I am trying to write an iterative search in a C program.

The pseudo-code would look like this for it:

while length of list > 0

look at middle of list

if number found, return true

else if number is too high, only consider

left half of list

else if number is too low, only consider

right half of list

return false

I have already implemented a bubble sort (which I assume works). Here is my code right now. The search is currently linear. I need to somehow change it to a binary search (I would prefer to do it iteratively instead of recursively). I’m completely lost on how I should go about doing this though. Can someone please help me?:

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>

#include "helpers.h"

/*
 * Returns true if value is in array of n values, else false.
 */

bool 
search(int value, int array[], int n)
{

// TODO: re-implement as binary search
int min = 0;
int max = n;
int i;

i=(min+max)/2;
    if(array[i] == value)
        {
        return true;
        }
    else if(min>=max)
        {
        return 1;
        }
    else if(array[i] > value)
        {
        max = i-1;
        }    
    else if(array[i] < value)
        {
        min = i+1;
        }
    return false;
}



/*
 * Sorts array of n values.
 */

void 
sort(int values[], int n)
{
//set smade to false to start
//declares the_end as n-1
bool smade = false;
int the_end = n-1;

// TODO: implement an O(n^2) sort
while(the_end > 0)
{
    for(int i = 0; i < the_end; i++)
    {
        int temp;
        if (values[i] > values[i+1])
        {
            temp = values[i];
            values[i] = values[i+1];
            values[i+1] = temp;
            smade=true;
        }
    

}
if(! smade)
    {
    break;
    }
the_end--;
}
return;
}
  • 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-27T06:28:11+00:00Added an answer on May 27, 2026 at 6:28 am

    You just translate your pseudo-code into code. I’ll give you some more refined pseudo-code:

    1) Set minimum possible location for thing we’re looking for (min) to zero.

    2) Set maximum possible location for thing we’re looking for (max) to the highest-possible location.

    3) Compute a location loc as (min+max)/2.

    4) Check if loc holds the object we’re looking for. If so, stop, we found it.

    5) Check if min>=max. If so, stop, it’s not here.

    5) If the object at loc is too high, set max to loc-1. (Because the maximum location at which it could be is one less than loc, since loc is too high.)

    6) If the object at loc is too low, set min to loc+1. (Same logic, the other way around.)

    7) Go to step 3.

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

Sidebar

Related Questions

I was trying to write some code that would check if an item has
I'm trying to write some code to look through an image file for groups
I'm trying to write to a JDO store using this code: PersistenceManager pm =
im trying to write an app that will display a list off lines from
I'm trying to write a blog post which includes a code segment inside a
I'm trying to write a new jQuery plugin. base (this is not my plugin,
I'm trying to write a tool that will take as input some C code
I'm trying to write some code in java to learn more about coding with
I'm trying to write some C# code to get to a specific folder in
I'm currently trying to write a .cmd Windows Shell script that would iterate over

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.