欢迎评论两个顺序搜索C函数 [英] Comments are welcome for two sequential search C functions

查看:39
本文介绍了欢迎评论两个顺序搜索C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欢迎任何评论关注整数和字符串版本的两个顺序搜索C

函数。虽然它们很简单,但是请不要嘲笑它:)你能给出好的建议吗?

吗?非常感谢。


/ *按顺序搜索整数数组以获取值。 val:整数

将在数组中搜索,数组:整数数组,len:item

数组中的数。返回在数组中找到的值的索引,-1

表示失败。函数的算法效率是O(n)。 - jhl,

2006年7月* /


#include< stddef.h>


int lookup( int val,const int array [],int len)

{

int i = -1;


if(array != NULL&& len!= 0){

for(i = 0; i< len; ++ i){

if(val == array [i]){

休息;

}

}

}

返回i;

}


/ *依次在字符串数组中搜索单词。单词:单词是在数组中搜索
,数组:字符串数组,len:item

数组中的数。返回在数组中找到的值的索引,-1

表示失败。函数的算法效率是O(n)。 - jhl,

2006年7月* /


#include< string.h>


int slookup( const char * word,const char * array [],int len)

{

int i = -1;


if(word!= NULL&& array!= NULL&& len!= 0){

for(i = 0; i< len; ++ i){

if(strcmp(word,array [i])== 0){

break;

}

}

}

返回i;

}

解决方案

lovecreatesbeauty说:


欢迎任何评论关注整数和字符串版本的两个顺序搜索C

函数。虽然它们很简单,但是请不要嘲笑它:)你能给出好的建议吗?

吗?非常感谢。


/ *按顺序搜索整数数组以获取值。 val:整数

将在数组中搜索,数组:整数数组,len:item

数组中的数。返回在数组中找到的值的索引,-1

表示失败。函数的算法效率是O(n)。 - jhl,

2006年7月* /


#include< stddef.h>


int lookup( int val,const int array [],int len)

{

int i = -1;


if(array != NULL&& len!= 0){

for(i = 0; i< len; ++ i){

if(val == array [i]){

休息;

}

}

}

返回i;

}



(a)要么警告用户len的返回值意味着不是

找到了,或者您应该在退货之前添加支票:


if(i == len)

{

i = -1;

}


(b)这是一种不间断地写它的方法:

int lookup(val,const int array [],int len)

{

int i = -1;

if(array!= NULL&& len!= 0)

{

while(i< len&& val!= array [i])

{

++ i;

}

if(i == len)

{

i = -1;

}

}

返回i;

}


/ *依次搜索a一个单词的字符串数组。单词:单词是在数组中搜索
,数组:字符串数组,len:item

数组中的数。返回在数组中找到的值的索引,-1

表示失败。函数的算法效率是O(n)。 - jhl,

2006年7月* /


#include< string.h>


int slookup( const char * word,const char * array [],int len)

{

int i = -1;


if(word!= NULL&& array!= NULL&& len!= 0){

for(i = 0; i< len; ++ i){

if(strcmp(word,array [i])== 0){

break;

}

}

}

返回i;

}



同上,在所有方面。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Richard Heathfield写道:
< blockquote class =post_quotes>
lovecreatesbeauty说:


欢迎任何评论关注以下两个顺序搜索C

函数整数和字符串版本。虽然它们很简单,但是请不要嘲笑它:)你能给出好的建议吗?

吗?非常感谢。


/ *按顺序搜索整数数组以获取值。 val:整数

将在数组中搜索,数组:整数数组,len:item

数组中的数。返回在数组中找到的值的索引,-1

表示失败。函数的算法效率是O(n)。 - jhl,

2006年7月* /


#include< stddef.h>


int lookup( int val,const int array [],int len)

{

int i = -1;


if(array != NULL&& len!= 0){

for(i = 0; i< len; ++ i){

if(val == array [i]){

休息;

}

}

}

返回i;

}



(a)要么警告用户len的返回值意味着不是

找到,或者你应该在返回之前添加一个支票:



哦,len超出了数组的范围。但我接受你的建议。

谢谢。


if(i == len)

{

i = -1;

}


(b)这是一种不间断写它的方法:



不推荐中断,否则会损坏代码,样式,逻辑......

等?


int lookup(val,const int array [],int len)

{

int i = -1;

if(array!= NULL&& len!= 0)

{

while(i< len&& val!= array [i])

{

++ i;

}

if(i == len)

{

i = -1;

}

}

返回i;

}


你永远不应该使用break来逃避if-check。这构成了糟糕的

编程。


" lovecreatesbeauty" < lo *************** @ gmail.com写信息

新闻:11 *************** ****** @ b28g2000cwb.googlegro ups.com ...


Richard Heathfield写道:


lovecreatesbeauty说:


欢迎任何评论跟随两个顺序搜索C / /
整数和字符串版本的函数。虽然它们很简单,但是请不要嘲笑它:)你能给出好的建议吗?

吗?非常感谢。

>

/ *按顺序搜索整数数组以获取值。 val:



整数


将在数组中搜索,数组:整数数组,len:





数组中的数。返回在数组中找到的值的索引,-1

表示失败。函数的算法效率是O(n)。 - jhl,

2006年7月* /

>

#include< stddef.h>

> ;

int lookup(int val,const int array [],int len)

{

int i = -1;

>

if(array!= NULL&& len!= 0){

for(i = 0; i< len; ++ i){

if(val == array [i]){

break;

}

}

}

返回i;

}



(a)或者你应该警告你的用户len的返回值意味着



" not


found',或者你应该在返回之前添加一个支票:



哦,len超出了数组的范围。但我接受你的建议。

谢谢。


if(i == len)

{

i = -1;

}


(b)这是一种不间断写它的方法:



不推荐中断,否则会损坏代码,样式,逻辑......

等?


int lookup(val,const int array [],int len)

{

int i = -1;

if(array!= NULL&& len!= 0)

{

while(i< len&& val!= array [i])

{

++ i;

}

if(i == len)

{

i = -1;

}

}

返回i;

}



Any comments are welcome for following the two sequential search C
functions of both integer and string versions. Though they are simple,
please do not laugh at it :) Could you give your great suggestion to
it? Thanks a lot.

/*sequentially search a array of integers for a value. val: the integer
is to be searched for in the array, array: array of integers, len: item
count in the array. return the index of the value found in array, -1
for failure. the algotithm efficience of the function is O(n). - jhl,
Jul 2006*/

#include <stddef.h>

int lookup(int val, const int array[], int len)
{
int i = -1;

if (array != NULL && len != 0){
for (i = 0; i < len; ++i){
if (val == array[i]){
break;
}
}
}
return i;
}

/*sequentially search a string array for a word. word: the word is to
be searched for in the array, array: array of word string, len: item
count in the array. return the index of the value found in array, -1
for failure. the algotithm efficience of the function is O(n). - jhl,
Jul 2006*/

#include <string.h>

int slookup(const char *word, const char *array[], int len)
{
int i = -1;

if (word != NULL && array != NULL && len != 0){
for (i = 0; i < len; ++i){
if (strcmp(word, array[i]) == 0){
break;
}
}
}
return i;
}

解决方案

lovecreatesbeauty said:

Any comments are welcome for following the two sequential search C
functions of both integer and string versions. Though they are simple,
please do not laugh at it :) Could you give your great suggestion to
it? Thanks a lot.

/*sequentially search a array of integers for a value. val: the integer
is to be searched for in the array, array: array of integers, len: item
count in the array. return the index of the value found in array, -1
for failure. the algotithm efficience of the function is O(n). - jhl,
Jul 2006*/

#include <stddef.h>

int lookup(int val, const int array[], int len)
{
int i = -1;

if (array != NULL && len != 0){
for (i = 0; i < len; ++i){
if (val == array[i]){
break;
}
}
}
return i;
}

(a) either you should warn your users that a return value of len means "not
found", or you should add a check before the return:

if(i == len)
{
i = -1;
}

(b) here is a way to write it without break:

int lookup(val, const int array[], int len)
{
int i = -1;
if(array != NULL && len != 0)
{
while(i < len && val != array[i])
{
++i;
}
if(i == len)
{
i = -1;
}
}
return i;
}

/*sequentially search a string array for a word. word: the word is to
be searched for in the array, array: array of word string, len: item
count in the array. return the index of the value found in array, -1
for failure. the algotithm efficience of the function is O(n). - jhl,
Jul 2006*/

#include <string.h>

int slookup(const char *word, const char *array[], int len)
{
int i = -1;

if (word != NULL && array != NULL && len != 0){
for (i = 0; i < len; ++i){
if (strcmp(word, array[i]) == 0){
break;
}
}
}
return i;
}

Ditto, on all counts.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Richard Heathfield wrote:

lovecreatesbeauty said:

Any comments are welcome for following the two sequential search C
functions of both integer and string versions. Though they are simple,
please do not laugh at it :) Could you give your great suggestion to
it? Thanks a lot.

/*sequentially search a array of integers for a value. val: the integer
is to be searched for in the array, array: array of integers, len: item
count in the array. return the index of the value found in array, -1
for failure. the algotithm efficience of the function is O(n). - jhl,
Jul 2006*/

#include <stddef.h>

int lookup(int val, const int array[], int len)
{
int i = -1;

if (array != NULL && len != 0){
for (i = 0; i < len; ++i){
if (val == array[i]){
break;
}
}
}
return i;
}


(a) either you should warn your users that a return value of len means "not
found", or you should add a check before the return:

Oh, len is out of range of the array. But I take your suggestion.
Thanks.

if(i == len)
{
i = -1;
}

(b) here is a way to write it without break:

Is break not recommended, or will it do harm to code, style, logic ..
etc. ?

int lookup(val, const int array[], int len)
{
int i = -1;
if(array != NULL && len != 0)
{
while(i < len && val != array[i])
{
++i;
}
if(i == len)
{
i = -1;
}
}
return i;
}


You should never use break to escape from a if-check. That constitutes bad
programming.

"lovecreatesbeauty" <lo***************@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...

Richard Heathfield wrote:

lovecreatesbeauty said:

Any comments are welcome for following the two sequential search C
functions of both integer and string versions. Though they are simple,
please do not laugh at it :) Could you give your great suggestion to
it? Thanks a lot.
>
/*sequentially search a array of integers for a value. val: the

integer

is to be searched for in the array, array: array of integers, len:

item

count in the array. return the index of the value found in array, -1
for failure. the algotithm efficience of the function is O(n). - jhl,
Jul 2006*/
>
#include <stddef.h>
>
int lookup(int val, const int array[], int len)
{
int i = -1;
>
if (array != NULL && len != 0){
for (i = 0; i < len; ++i){
if (val == array[i]){
break;
}
}
}
return i;
}

(a) either you should warn your users that a return value of len means

"not

found", or you should add a check before the return:


Oh, len is out of range of the array. But I take your suggestion.
Thanks.

if(i == len)
{
i = -1;
}

(b) here is a way to write it without break:


Is break not recommended, or will it do harm to code, style, logic ..
etc. ?

int lookup(val, const int array[], int len)
{
int i = -1;
if(array != NULL && len != 0)
{
while(i < len && val != array[i])
{
++i;
}
if(i == len)
{
i = -1;
}
}
return i;
}



这篇关于欢迎评论两个顺序搜索C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆