-2

I would like to know how I could achieve a simple function "length" that returns the length of a given array in a recursive manner.

This seems pretty easy with Prolog, but I'm not sure how to do it with C++, precisely, how to divide the arrays in an efficient way.

Example could be:

int lenght(*int arr)
{
...
}

int main()
{
    int arr[5] = {1,2,3,4,5};
    cout << "Length is: " <<length(arr)
}

In Prolog I would use something like tow cases, one for the base case, and another for the recursive case

length([], 0).
length([Head|Tail], Length) :- size(Tail,TailListLength), Length is TailListLength+1.
OnlyDavies
  • 123
  • 2
  • 8
  • 3
    This function exists in standard library: `::std::size`, it doesn't involve any recursion though. Also i should mention that prolog example deals with list, not array, so your question should probably be about lists. – user7860670 Sep 28 '21 at 09:07
  • 8
    You cannot got size from pointer, information is lost (unless you use a sentinel value or use another system to keep that information) – Jarod42 Sep 28 '21 at 09:08
  • 2
    Why not simply use `std::array` instead. It provides that function already. – πάντα ῥεῖ Sep 28 '21 at 09:08
  • @Jarod42 so I need to add a new parameeter with the current length – OnlyDavies Sep 28 '21 at 09:10
  • 3
    Whereas some languages encourage recursion, in C++, it is just a way to solve problem. iteration is generally simpler/more idiomatic. – Jarod42 Sep 28 '21 at 09:11
  • @πάντα ῥεῖ of course I could use, it just curiosity if this could be achievable or not – OnlyDavies Sep 28 '21 at 09:11
  • 3
    In Prolog, you're using a list, not an array, which is a completely different structure. Get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start at the beginning - there are very few similarities between C++ and Prolog (or Haskell or Java). – molbdnilo Sep 28 '21 at 09:12
  • @user7860670 ,molbdnilo you both are right, thanks, I'll check that – OnlyDavies Sep 28 '21 at 09:13
  • A pointer passed to a function, on its own, is not associated with the length of an array passed - since the function only receives the address of the first element with no information about how many elements (if any) follow. If the size is passed (e.g. as an additional argument) it is unnecessary to calculate size (obviously! duh!). If size is not passed size may be calculated if there is a sentinel (a chosen value that is deemed to mark the end) - and, in that case, an iterative or recursive solution is trivial. If no sentinel is specified and size is not supplied, no solution is possible. – Peter Sep 28 '21 at 10:48

1 Answers1

0

The array in C++ is not a recursive data structure (but a multiple of consecutive storage slots), in contrary to a list, with its head and tail. You could take the recursive length on its iterator using its has-next.

As length is a linear property, so is not that interesting.

A binary search or any divide-and-conquer would do nicer.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138