lower number

T

temper3243

i,

If i have an array like {1,4, 10, 15 , 20 , 30 } of size n , now if i
want to search for number

25 , i should get 20 , if i search for number 11 i hould get 10 , if i
search for 4 i should get 4, if i search for a number and it doesn't
exist i should get the lower number between which it lies. All numbers
are bound to lie between 1 and n.

Can you tell me a easy way to do it.
 
B

bert

i,

If i have an array like {1,4, 10, 15 , 20 , 30 } of size n , now if i
want to search for number

25 , i should get 20 , if i search for number 11 i hould get 10 , if i
search for 4 i should get 4, if i search for a number and it doesn't
exist i should get the lower number between which it lies. All numbers
are bound to lie between 1 and n.

Can you tell me a easy way to do it.

Just write some C code to do it. If it doesn't work,
post the code, then somebody here will help you.
--
 
C

Clever Monkey

i,

If i have an array like {1,4, 10, 15 , 20 , 30 } of size n , now if i
want to search for number

25 , i should get 20 , if i search for number 11 i hould get 10 , if i
search for 4 i should get 4, if i search for a number and it doesn't
exist i should get the lower number between which it lies. All numbers
are bound to lie between 1 and n.

Can you tell me a easy way to do it.
Brute force: Iterate through the array (pre-sorted or not, depending on
how scalable you need this to be). Does the current pass the <= test?
Save it, but only if it is larger than the previously saved value.
Continue to the next item and repeat until done. Your saved value has
the last item that matched your criteria.

Of course, since the exact test is a special case, it would be smarter
to short-circuit if you find that value at any point.

I'm sure there are much better and more refined algorithms.
 
P

pete

i,

If i have an array like {1,4, 10, 15 , 20 , 30 } of size n , now if i
want to search for number

25 , i should get 20 , if i search for number 11 i hould get 10 , if i
search for 4 i should get 4, if i search for a number and it doesn't
exist i should get the lower number between which it lies. All numbers
are bound to lie between 1 and n.

Can you tell me a easy way to do it.

/* BEGIN new.c */

#include <stdio.h>

int comparison(const void *arg1, const void *arg2);
void *HW_search(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
int main (void)
{
int x, *found;
int array[] = {1,4, 10, 15 , 20 , 30 };
int targets[] = {25, 11, 4, 1, 2, 3, 23};

for (x = 0; x != sizeof targets / sizeof *targets; ++x) {
printf("Searching array for %d\n", targets[x]);
found = HW_search(
targets + x,
array,
sizeof array / sizeof *array,
sizeof *array,
comparison);
printf("found %d\n\n", *found);
}
return 0;
}

int comparison(const void *arg1, const void *arg2)
{
return *(int*)arg2 > *(int*)arg1 ? -1
: *(int*)arg2 != *(int*)arg1;
}

void *HW_search(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
int comp;
size_t odd_mask, bytes, middle, high, low;
const unsigned char *array, *found;

found = NULL;
if (nmemb != 0) {
odd_mask = size ^ size - 1;
array = base;
low = 0;
high = nmemb * size;
do {
bytes = high - low;
middle = (bytes & odd_mask ? bytes - size : bytes) / 2
+ low;
base = middle + array;
comp = compar(key, base);
if (comp > 0) {
low = middle;
} else {
high = middle;
if (comp == 0) {
found = array + high;
}
}
} while (bytes != size);
}
return (void *)(found == NULL ? array + middle : found);
}

/* END new.c */
 
J

jaks.maths

Assuming that array is sorted one.

#include<stdio.h>
int main()
{
int a[20],i,j,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a);
printf("\n enter the value to be searched");
scanf("%d",&j);
for(i=1;i<=n;i++)
{
if(a<=j)
continue;
else
break;
}
printf("%d",a[i-1]);
return 0;
}
i,

If i have an array like {1,4, 10, 15 , 20 , 30 } of size n , now if i
want to search for number

25 , i should get 20 , if i search for number 11 i hould get 10 , if i
search for 4 i should get 4, if i search for a number and it doesn't
exist i should get the lower number between which it lies. All numbers
are bound to lie between 1 and n.

Can you tell me a easy way to do it.

/* BEGIN new.c */

#include <stdio.h>

int comparison(const void *arg1, const void *arg2);
void *HW_search(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
int main (void)
{
int x, *found;
int array[] = {1,4, 10, 15 , 20 , 30 };
int targets[] = {25, 11, 4, 1, 2, 3, 23};

for (x = 0; x != sizeof targets / sizeof *targets; ++x) {
printf("Searching array for %d\n", targets[x]);
found = HW_search(
targets + x,
array,
sizeof array / sizeof *array,
sizeof *array,
comparison);
printf("found %d\n\n", *found);
}
return 0;
}

int comparison(const void *arg1, const void *arg2)
{
return *(int*)arg2 > *(int*)arg1 ? -1
: *(int*)arg2 != *(int*)arg1;
}

void *HW_search(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
int comp;
size_t odd_mask, bytes, middle, high, low;
const unsigned char *array, *found;

found = NULL;
if (nmemb != 0) {
odd_mask = size ^ size - 1;
array = base;
low = 0;
high = nmemb * size;
do {
bytes = high - low;
middle = (bytes & odd_mask ? bytes - size : bytes) / 2
+ low;
base = middle + array;
comp = compar(key, base);
if (comp > 0) {
low = middle;
} else {
high = middle;
if (comp == 0) {
found = array + high;
}
}
} while (bytes != size);
}
return (void *)(found == NULL ? array + middle : found);
}

/* END new.c */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top