n*c 发帖数: 22 | 1 I was asked the following?
Complete the body of the following function:
/** Return the index in array data of the element closest to or
equal to value. The array data has exactly data_count
elements. */
size_t find_nearest (int value, const int* data, size_t data_count);
Any quick code? thanks a lot |
l*n 发帖数: 529 | 2 为什么这种code也来求啊?是作业么?
就是个简单循环一遍的事情,都不涉及到多指针,自己还搞不定的话,以后能干活儿么?
【在 n*c 的大作中提到】 : I was asked the following? : Complete the body of the following function: : /** Return the index in array data of the element closest to or : equal to value. The array data has exactly data_count : elements. */ : size_t find_nearest (int value, const int* data, size_t data_count); : Any quick code? thanks a lot
|
n*c 发帖数: 22 | 3 I'm not computer guy, try to get similar C/C+ code, any suggestion is
appreciated. Thanks. |
n*c 发帖数: 22 | 4 Can anyone show me a quick code?
many thanks. |
n*c 发帖数: 22 | 5 do i need 指针? or just a array?
many thanks. |
n*c 发帖数: 22 | 6 please help me with a quick code, i need online submit.
thanks. |
A*****i 发帖数: 3587 | 7 size_t find_nearest(int value, const, int * data, size_t data_count) {
size_t res = 0;
int tmp1 = 0;
int tmp2 = value;
if (0 == data_count)
return data_count;
for (size_t i = 0; i < data_count; i++) {
tmp1 = value >= data[i]? (value - data[i]):(data[i] - value);
if (tmp1 < tmp2){
tmp2 = tmp1;
res = i;
}
}
return res;
}
老朽多年不写C和C++了,也不知道对不,nodejs害人不浅啊 |
f*****2 发帖数: 141 | 8 献丑了,不知道理解对不,请lz自己调试一下
int fabs(int var1, int var2);
size_t find_nearest(int value, const int* data, size_t data_count);
{
int index,indexValue, temp;
temp = fabs(value, data[0]);
indexValue = 0;
for(index=0; index < data_count; index++)
{
if(temp > fabs(value, data[index]))
indexValue = index;
}
return (size_t)indexValue;
}
int fabs(int var1, int var2)
{
retrun var1 >= var2? (var1-var2): -(var1-var2);
} |
n*c 发帖数: 22 | 9 this is great, i'm trying understanding your code.
thanks for your kindness. |
n*c 发帖数: 22 | 10 appreciated Avenssi (蛋疼帝) and fit2012 (fit2012).
Thanks so much. |