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

My question is not so simple:

I have a WindowsForm with a diffrent controls like: textboxes, comboboxes, checkboxes, datetimepickers, listview and so on...

How can i create temporary data ...

Hi guys,

I have developed a windows application using vb.net 2012 and sqlserver express edition 2012. the application is able to access the database on a local hot computer. but ...