Re: Can't find last element in list
On Apr 26, 7:43 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
desktop wrote:
How do I find the last element in the list numsx defined below?
int* myfind(int* arr_start, int* arr_end, int& s) {
Why is 's' passed by
reference, I suppose you meant.
Even more important, why is it passed by non-const reference?
Presumably, he wants to change it.
int not_found = 666;
int* result = ¬_found;
while (arr_start != arr_end){
if (*(arr_start) == s)
{
result = arr_start;
}
arr_start++;
}
return result;
Clutter, clutter... Given the interface (forget the 666) you
should implement this as
while (arr_start != arr_end) {
if (*arr_start == s)
break;
++arr_start;
}
return arr_start;
}
Except that that won't pass code review in most serious shops.
It's very confusing to have a loop exit in two different
locations.
What he's implementing is nothing more than a linear search.
And the algorithm for a linear search is well known:
WHILE NOT finished AND NOT found
DO
avance to next
DONE
In C++, in this case:
int*
myFind( int* start, int* end, int searchValue )
{
int* current = start ;
while ( current != end && *current != searchValue ) {
++ current ;
}
return current ;
// Or:
// return current == end ? NULL : current ;
// if that is the convention. Just returning
// the end is more idiomatic in C++.
}
int main() {
int numsx[] = { 1, 2, 3, 4, 5, 7, 8, 9};
int* resultx;
int endx = (sizeof(numsx)/4)-1;
Two mistakes. First, don't use 4, use 'sizeof(int)'. Second,
you're much better off if you don't subtract 1. Use the range
where the upper value is not includede (this is called "an open
range"). IOW
It's called a half open interval. An open interval would be
exclusive at both ends.
Historically, different languages have different traditions; the
half-open interval is very, very idiomatic for C and its
derivatives (C++, Java and I suppose C#). In my experience, it
also works best in general, and I would probably use it in other
languages as well.
int endx = sizeof(numsx) / sizeof(numsx[0]);
int sx = 9;
resultx = ::myfind( numsx, numsx + endx,sx);
Since 'resultx' is a pointer, it could be equal to 'numsx + endx',
which will mean "not found".
That's the modern, idiomatic C++ solution. Much code still
conforms to the traditional solution, still idiomatic in C, of
returning NULL for not found. The mapping is trivial, see my
comments on the return, above.
If you are just learning C++, of course, you're better off
learning to use the idiomatic solutions. Except that if this is
homework (probably, since if not, the obvious solution is
std::find), the prof may have other ideas. (And of course, in a
professional context, you conform to the desires of your
employer.)
std::cout << *resultx << std::endl;
return 0;
}
I have thought about:
while (arr_start != arr_end+1){
...
...
instead, but since "arr_end+1" is unknow territory I guess its bad
style.
No, it's actually better.
But only because C/C++ have special rules which make it legal.
You're allowed to form the address of an element one passed the
end of an array, but not to dereference the resulting pointer.
As I said, it's the idiomatic way of doing things in C++.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34