D
desktop
How do I find the last element in the list numsx defined below?
int* myfind(int* arr_start, int* arr_end, int& s) {
int not_found = 666;
int* result = ¬_found;
while (arr_start != arr_end){
if (*(arr_start) == s)
{
result = arr_start;
}
arr_start++;
}
return result;
}
int main() {
int numsx[] = { 1, 2, 3, 4, 5, 7, 8, 9};
int* resultx;
int endx = (sizeof(numsx)/4)-1;
int sx = 9;
resultx = ::myfind( numsx, numsx + endx,sx);
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.
Is there someway to make sure that the last element in a list is read
and only terminate afterwards?
int* myfind(int* arr_start, int* arr_end, int& s) {
int not_found = 666;
int* result = ¬_found;
while (arr_start != arr_end){
if (*(arr_start) == s)
{
result = arr_start;
}
arr_start++;
}
return result;
}
int main() {
int numsx[] = { 1, 2, 3, 4, 5, 7, 8, 9};
int* resultx;
int endx = (sizeof(numsx)/4)-1;
int sx = 9;
resultx = ::myfind( numsx, numsx + endx,sx);
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.
Is there someway to make sure that the last element in a list is read
and only terminate afterwards?