Not Yet Answered # My question with the data type in C

Discussion Starter JohnHull Discussion Starter JohnHull OK, so HostGator for some reason no longer allows gcc/g++ access unless you have a Designated Server account, which is a lot of money to spend just to compile my "Hello World" program. Thus I figured I'd compile at home, then upload. Program is your regular old bare-bones Hello World ...

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 article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`

I don’t want at this stage work on a big separate project as I've already got plenty ...