Write C functions to perform the following tasks:

Given a sorted double array x*, i=0 to n and a double t, write a function which uses a binary search algorithm to find k such that x[k ¡ 1] < t <= x[k].*

Many thanks for your help!

0

Hi there,

I am a new comer in C. I do need your help.

Write C functions to perform the following tasks:

Given a sorted double array x*, i=0 to n and a double t, write a function which uses a binary search algorithm to find k such that x[k ¡ 1] < t <= x[k].*

Regarding my question, I can get the result when I defined t and x as integer type, but I couldnt get it through if I defined them as double type. I appreciate your help!!

the code is as follow:

```
#include <stdio.h>
#define MAX 10
#include "SortSrchEx.h"
void main()
{
int i, t, x[MAX] = {8 12, 29, 30, 32, 35, 49 54};
int k = 8; /* no. of items in the array x[] */
printf( " ***Binary Search*** \n\n " );
printf( " The array contains: %d, %d, %d, %d, %d, %d \n", x[0], x[1], x[2],x[3], x[4], x[5] );
printf( " Tpye a numer, EOF to quit: " );
while( scanf( " %d ", &t ) != EOF ){
i = binsrch( x, k, t );
if ( i >=0 )
printf( "%d found, giving %d < %d <= %d\n ", i, x[i-1], t, x[i]);
else
printf( " %d not fouond in array\n ", t);
printf( " Tpye a number, EOF to quit: " );
}
}
```

The "SortSrchEx.h" is:

```
#include <stdio.h>
int binsrch(int x[], int lim, int key)
{
int low, mid, high = lim - 1 ;
low = 0;
while( low <= high ) { /* Is the array exhausted? */
mid = (low + high)/2; /* If no, find middle index */
if ( key > x[mid]) // Is the key here?
low = mid + 1; // otherwise, increase low
else if ( key > x[mid-1])
return (mid); // If so, return index
else
high = mid-1 ;
}
return(-1); // not found
}
```

<< moderator edit: added code tags: [code][/code] >>

0

<< moderator edit: threads merged >>

hi there,

Write C functions to perform the following tasks:

Given a sorted double array x*, i=0 to n and a double t, write a function which uses a binary search algorithm to find k such that x[k ¡ 1] < t <= x[k].*

Regarding my question, I can get the result when I defined t and x as integer type, but I couldnt get it through if I defined them as double type. I appreciate your help!!

the code is as follow:

```
#include <stdio.h>
#define MAX 10
#include "SortSrchEx.h"
void main()
{
int i, t, x[MAX] = {8 12, 29, 30, 32, 35, 49 54};
int k = 8; /* no. of items in the array x[] */
printf( " ***Binary Search*** \n\n " );
printf( " The array contains: %d, %d, %d, %d, %d, %d \n", x[0], x[1], x[2],x[3], x[4], x[5] );
printf( " Tpye a numer, EOF to quit: " );
while( scanf( " %d ", &t ) != EOF ){
i = binsrch( x, k, t );
if ( i >=0 )
printf( "%d found, giving %d < %d <= %d\n ", i, x[i-1], t, x[i]);
else
printf( " %d not fouond in array\n ", t);
printf( " Tpye a number, EOF to quit: " );
}
}
```

The "SortSrchEx.h" is:

```
#include <stdio.h>
int binsrch(int x[], int lim, int key)
{
int low, mid, high = lim - 1 ;
low = 0;
while( low <= high ) { /* Is the array exhausted? */
mid = (low + high)/2; /* If no, find middle index */
if ( key > x[mid]) // Is the key here?
low = mid + 1; // otherwise, increase low
else if ( key > x[mid-1])
return (mid); // If so, return index
else
high = mid-1 ;
}
return(-1); // not found
```

<< moderator edit: added code tags: [code][/code] >>

This topic has been dead for over six months. Start a new discussion instead.

Recommended Topics

**Problem**

**When split msg variable to more lines it not give me values in lines ?
msg variable in debug give me text scanning bellow**

**Details**

i work in windows ...

Hi all,

This is my array of hash

[{"gate_pass_type_id"=>2, "tag"=>0, "total"=>2000}, {"gate_pass_type_id"=>125, "tag"=>0, "total"=>300}, {"gate_pass_type_id"=>661, "tag"=>0, "total"=>750}, {"gate_pass_type_id"=>661, "tag"=>2, "total"=>100}]

I want to convert it into

[2 => { "0"=> ...

My question is how to code search in combobox each time typeing a letter. And the search result in DropDown have to show list items which contains letter or ...