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!

JohnHull
0
Newbie Poster

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!

Jump to PostYou know we're not going to do it for you, right?

Dogtree
23
Posting Whiz in Training

You know we're not going to do it for you, right?

JohnHull
0
Newbie Poster

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] >>

JohnHull
0
Newbie Poster

<< moderator edit: threads merged >>

hi there,

Write C functions to perform the following tasks:

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] >>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.

Broken Link

**You're trying to visit a URL that doesn't currently exist on the web.**
Most likely, a member posted a link a long time ago to a web page that has since been removed.
It's also possible that there was a typo when posting the URL.
We redirect you to this notice instead of stripping out the link to preserve the integrity of the post.