L
lovecreatesbeauty
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){
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) == 0){
break;
}
}
}
return i;
}
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){
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) == 0){
break;
}
}
}
return i;
}